listBox数据传递给textBox的实现问题!

霜烟若梦 2012-09-26 04:28:19
private void textBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source = 10.60.2.133; uid = sa; pwd =123; database = rkdb_tq";
con.Open();
SqlCommand cmd = new SqlCommand("select top 5 zjhm from dbo.qhry where substring(zjhm,1," + textBox1.Text.Length + ")='" + textBox1.Text + "'");
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmd.CommandText, con);
adp.Fill(ds);

if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
this.listBox1.Visible = true;
this.listBox1.Items.Clear();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
this.listBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
}
}
}
}
private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{

if (this.listBox1.SelectedItems.Count > 0)
{
this.textBox1.Text = this.listBox1.SelectedItem.ToString();
}
if (this.listBox1.Items.Count > 0)
{
for (int i = 0; i < this.listBox1.Items.Count; i++)
{
if (listBox1.SelectedItems != null)
{
this.listBox1.Text = listBox1.SelectedItems[i].ToString();
}
}
this.listBox1.Visible = true;
this.Focus();
}
else
{
this.listBox1.Visible = false;
}
}

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
this.listBox1.Visible = true;
}
}

以上是我写的listBox方法和doubleClick方法。我想实现:双击listBox中记录,textBox中显示这条记录。请各位帮忙看看为什么没有传递数据显示在textBox中?
...全文
212 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
霜烟若梦 2012-09-27
  • 打赏
  • 举报
回复
问题解决了,谢谢大家的帮助!下面是我的Click事件中的方法,大家看看吧。
忘了把Click事件调用写在textbox中了,要不问题会好解决点!
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (this.listBox1.SelectedIndex != -1)
{
if (index != System.Windows.Forms.ListBox.NoMatches)
{
this.textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
}
}
else
MessageBox.Show("选择项不能为空!");
}
熙风 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

十分感谢3楼的热心帮助,可是我试了双击listBox中记录,依然没有反应!不能实现被选中项添加到textBox中。
[/Quote]

你在那个双击事件中打个断点调试下,,看是否进了事件
霜烟若梦 2012-09-27
  • 打赏
  • 举报
回复
十分感谢3楼的热心帮助,可是我试了双击listBox中记录,依然没有反应!不能实现被选中项添加到textBox中。
熙风 2012-09-26
  • 打赏
  • 举报
回复
listBox1_SelectedValueChanged这个事件去掉

  private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.listBox1.Items.Count > 0&&this.listBox1.SelectedItems.Count > 0)
{
textBox1.Text = listBox1.SelectedItem.ToString();
this.listBox1.Visible = true;
}else
{
textBox1.Text="";
}
}
霜烟若梦 2012-09-26
  • 打赏
  • 举报
回复
我晕~~~貌似这难度还不小了!
bdmh 2012-09-26
  • 打赏
  • 举报
回复
你这个代码耦合性太强了,所有this.listBox1.Items的操作(包括add,clear等)基本都会引起 SelectedValueChanged,同样会导致text的变换,你好好跟踪调试吧

110,825

社区成员

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

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

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