listbox数据如何传给textbox?

tiandulan 2012-05-21 12:25:34
我用socket通信,服务器监听接收数据后发到一个listbox上了,但是我想把这些数据再存到一个textbox上,试了半天实现不了,下面是我的代码:
listBox1.Items.Add(sr.ReadLine() + "\n");//数据流中读取字符串存到listbox上。


if (this.lbInfo.SelectedIndex != -1)
{
this.textBox1.Text = this.lbInfo.SelectedItem.ToString();//listbox上的数据传到textbox上。
}

我哪里写错了?
...全文
464 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2012-05-21
  • 打赏
  • 举报
回复
this.textBox1.Text = this.lbInfo.Text;
tiandulan 2012-05-21
  • 打赏
  • 举报
回复
搞定了,非常非常非常感谢![Quote=引用 8 楼 的回复:]
listbox上面的item被select中的话会显示蓝色,你没有用鼠标去选中,所以SelectedItem就不存在
if (this.lbInfo.SelectedIndex != -1)
{
this.textBox1.Text = this.lbInfo.SelectedItem.ToString();
}
上面的代码改成下面这样试试
int count=lbIn……
[/Quote]
EdsionWang 2012-05-21
  • 打赏
  • 举报
回复
listbox上面的item被select中的话会显示蓝色,你没有用鼠标去选中,所以SelectedItem就不存在
if (this.lbInfo.SelectedIndex != -1)
{
this.textBox1.Text = this.lbInfo.SelectedItem.ToString();
}
上面的代码改成下面这样试试
int count=lbInfo.Items.Count-1;
this.textBox1.Text =lbInfo.Items[count].ToString();
[Quote=引用 7 楼 的回复:]

怎么个意思?能详细说一下么?
引用 5 楼 的回复:
好像是listbox没有哪一行被Select上了,如果想得到listbox最新一行,可以这样试试。

C# code

int count=lsbResult.Items.Count-1;
lsbResult.Items[count].ToString();
[/Quote]
tiandulan 2012-05-21
  • 打赏
  • 举报
回复
怎么个意思?能详细说一下么?
[Quote=引用 5 楼 的回复:]
好像是listbox没有哪一行被Select上了,如果想得到listbox最新一行,可以这样试试。

C# code

int count=lsbResult.Items.Count-1;
lsbResult.Items[count].ToString();
[/Quote]
tiandulan 2012-05-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace 服务器
{
public partial class 服务器启动 : Form
{
private Socket s;
private Thread th;
public Socket cSocket;
public NetworkStream ns;
public StreamReader sr;
public StreamWriter sw;
private delegate void SetTextCallback();
private delegate void SetTextHander(string temp);
public 服务器启动()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverIP1 = IPAddress.Parse("192.168.1.106");
IPEndPoint server1 = new IPEndPoint(serverIP1,15);
s.Bind(server1);
s.Listen(10);
try
{
th = new Thread(new ThreadStart(Communication));

th.Start();
label1.Text = "服务器启动成功!";
}
catch (Exception ex)
{
MessageBox.Show("服务器启动失败!" + ex.Message);
}



}
public void Communication()
{
while (true)
{
try
{
cSocket = s.Accept();
if (cSocket.Connected)
{
ns = new NetworkStream(cSocket);//??????
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
test();
sw.WriteLine("收到请求,允许连接");
sw.Flush();
if (this.lbInfo.SelectedIndex != -1)
{
this.textBox1.Text = this.lbInfo.SelectedItem.ToString();
}
}
else
{
MessageBox.Show("连接失败");
}

}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception es)
{
MessageBox.Show("其它异常" + es.Message);
}
}

}
public void send()
{
lbInfo.Items.Add(sr.ReadLine() + "\n");
listBox1.Items.Add(sr.ReadLine() + "\n");
}
public void test()
{
SetTextCallback stcb = new SetTextCallback(send);

if (this.lbInfo.SelectedIndex != -1)
{
this.textBox1.Text = this.lbInfo.SelectedItem.ToString();
}
Invoke(stcb);
}




private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}

private void button2_Click(object sender, EventArgs e)
{

}

private void lbInfo_SelectedIndexChanged(object sender, EventArgs e)
{

}

}

}
我写的代码…有空的话帮忙看一下。谢谢!
[Quote=引用 1 楼 的回复:]
this.textBox1.Text = this.lbInfo.Text;
[/Quote]
EdsionWang 2012-05-21
  • 打赏
  • 举报
回复
好像是listbox没有哪一行被Select上了,如果想得到listbox最新一行,可以这样试试。

int count=lsbResult.Items.Count-1;
lsbResult.Items[count].ToString();
机器人 2012-05-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:
什么错误?这么写没看出什么问题。ListBox 正常显示了么?

呵呵,睡觉了。明天早起看日食咯
[/Quote]

果然是天文爱好者,赞。
threenewbee 2012-05-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
什么错误?这么写没看出什么问题。ListBox 正常显示了么?
[/Quote]
呵呵,睡觉了。明天早起看日食咯
机器人 2012-05-21
  • 打赏
  • 举报
回复
什么错误?这么写没看出什么问题。ListBox 正常显示了么?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧