combox 中的值为
a aaa
b bbbb
c cccccc这个COMBOX是可以输入也可以选择的 当我选择的时候 比如我选择了 a aaa这一项目 那么在combox里显示的时候只显示aaa 如果我输入的时候 当我写上了c 那么就会弹出下拉菜单 c ccc这一项是被先中的`当我点击的时候 CCC会放入combox中 而不是显示 c ccc 请问这个要怎么写`因为下拉框的数量较`所以要从做成方法的角度去考虑~ 在线等`
a aaa
b bbbb
c cccccc这个COMBOX是可以输入也可以选择的 当我选择的时候 比如我选择了 a aaa这一项目 那么在combox里显示的时候只显示aaa 如果我输入的时候 当我写上了c 那么就会弹出下拉菜单 c ccc这一项是被先中的`当我点击的时候 CCC会放入combox中 而不是显示 c ccc 请问这个要怎么写`因为下拉框的数量较`所以要从做成方法的角度去考虑~ 在线等`
{
comboBox1.Text = comboBox1.Text.Split(' ')[1].ToString();
}都不知道这么赋值有什么不对 就是不会变
{
e.Handled = true;//取消键盘输入
}是不是可以在这个事件里面处理输入的值
/// <summary>
/// 对ComboBox控件进行基础数据绑定
/// </summary>
/// <param name="strTable">来源数据表</param>
/// <param name="cb">combobox控件名称</param>
/// <param name="Conditions">条件</param>
/// <param name="i">第几个字段</param>
public void BindDropdownjclist(string strTable, ComboBox cb, string conditions,int i)
{
cn.Open();
//实例化command对象
OleDbCommand cmd = new OleDbCommand("select * from "+strTable+" where treetext='"+conditions+"'",cn);
OleDbDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
cb.Items.Add(odr[i].ToString());
}
cn.Close();
}LZ,我的问题是这样解决的。写个方法供你参考
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Web.UI.WebControls;namespace combox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ListItem list = new ListItem("a aaa", "aaa");
private void com()
{
this.comboBox1.Items.Add(list);
}
private void Form1_Load(object sender, EventArgs e)
{
com();
}
private void a()
{
this.comboBox1.Text = list.Value;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.button2.Text = list.Value;
a();
} }
}
如果我跟踪的话~list.value的值为aaa 也就是说它有值了 可是combox的值不变
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Web.UI.WebControls;namespace combox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ListItem list = new ListItem("a aaa", "aaa");
private void com()
{
this.comboBox1.Items.Add(list);
//改动了这里
this.comboBox1.Text = list.Value;
}
private void Form1_Load(object sender, EventArgs e)
{
com();
}
private void a()
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.button2.Text = list.Value;
} }
}
改成这样就行了~ 郁闷` 期待高手来帮忙
{
System.Threading.Thread Newcomtext = new System.Threading.Thread(new System.Threading.ThreadStart(doit));
Newcomtext.Start();
} private void doit()
{
System.ComponentModel.ISynchronizeInvoke synchronizer = comboBox1;
MethodInvoker invoker = new MethodInvoker(newtext);
synchronizer.Invoke(invoker, null);
} private void newtext()
{
comboBox1.Text = comboBox1.Text.Split(' ')[1].ToString();
}
一下子找不到了,我看看先
public class SelfDefCombo : ComboBox
{
}
using System.Windows.Forms;namespace DataGridComboBoxColumn
{
/// <summary>
/// DataGridComboBox 的摘要说明。
/// </summary>
public class DataGridComboBox:ComboBox
{
private const int WM_KEYUP = 0x101; protected override void WndProc(ref System.Windows.Forms.Message message)
{
if (message.Msg == WM_KEYUP)
{
return;
} base.WndProc(ref message);
} //通过索引取得items的value。
public string GetValueText(int index)
{
if(index < 0 || index >= base.Items.Count)
{
//抛出索引超出异常
throw new IndexOutOfRangeException("无效索引。");
}
else
{
string text = string.Empty;
int memIndex = -1;
try
{
base.BeginUpdate();
memIndex = base.SelectedIndex;
base.SelectedIndex = index;
text = base.SelectedValue.ToString();
base.SelectedIndex = memIndex;
}
catch
{}
finally
{
base.EndUpdate();
}
return text;
}
}
//通过索引取得items的text
public string GetDisplayText(int index)
{
if(index <0 || index >=base.Items.Count)
{
//抛出索引超出异常
throw new IndexOutOfRangeException("无效索引。");
}
else
{
string text = string.Empty;
int memIndex = -1;
try
{
base.BeginUpdate();
memIndex = base.SelectedIndex;
base.SelectedIndex = index;
text = base.Text.ToString();
base.SelectedIndex = memIndex;
}
catch
{}
finally
{
base.EndUpdate();
}
return text;
}
}
//通过value取得items的text
public string GetDisplayText(object value)
{
string text = string.Empty;
int memIndex= -1;
try
{
base.BeginUpdate();
memIndex = base.SelectedIndex;
base.SelectedValue = value.ToString();
text = base.Text.ToString();
base.SelectedIndex = memIndex;
}
catch
{}
finally
{
base.EndUpdate();
}
return text;
}
//循环获取items的text
public string[] GetDisplayText()
{
string[] text = new string[base.Items.Count];
int memIndex = -1;
try
{
base.BeginUpdate();
memIndex = base.SelectedIndex;
for(int i=0;i<base.Items.Count;i++)
{
base.SelectedIndex = i;
text[i] = base.Text.ToString();
}
base.SelectedIndex = memIndex;
}
catch
{}
finally
{
base.EndUpdate();
}
return text;
}
}}
节选自 http://www.cnblogs.com/ami/archive/2006/07/20/455360.html
{
string old = comboBox1.Text;
for (int i = 0; i < comboBox1.Items.Count; i++)
if (comboBox1.Items[i].ToString().IndexOf(comboBox1.Text + " ") == 0)
{
comboBox1.BeginUpdate();
comboBox1.SelectedIndex = i;
comboBox1.DroppedDown = true;
comboBox1.Text = old;
comboBox1.Select(comboBox1.Text.Length, 0);
comboBox1.EndUpdate();
break;
}
} private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
string s=comboBox1.Items[comboBox1.SelectedIndex].ToString();
comboBox1.Text = s.Remove(0, s.IndexOf(" ") + 1);
}
比如按个c 自动下拉而且选中c ccc到状态,然后回车就把ccc赋值回去了
不过还是有点小问题,比如鼠标选择
Mark下~个人认为必须Invoke,这个是正解!!!
运行过程中动态改变控件的属性了,必须invoke做...
子线程是不能操作UI线程的
方法:
用异步委托做个Invoke方法
b bbb
c ccc
当我点击了其中一项时,比如我选了a aaa 那么在combox里面应该显示 a aaa对吧 现在是想下拉框出现时显示a aaa 当我点击选择了以后 在combox里显示 aaa 不知道我说明白了没有
this.button2.Text = list.Value;
这句话是不是搞错了,应该是
this.comboBox1.Text= list.Value;吧?