有谁知道这个控件的基本用法吗?
比如说显示多列等。
高手们提示一下。
下面的示例是个完整的应用程序,阐释可如何使用 Add 方法向 ComboBox 添加项,如何使用 FindString 方法在 ComboBox 中查找项,以及如何使用 BeginUpdate 和 EndUpdate 方法以高效的方式向 ComboBox 添加大量项。
using System;
using System.Windows.Forms;
namespace Win32Form1Namespace {
public class Win32Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button addGrandButton;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button showSelectedButton;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button findButton;
private System.Windows.Forms.Label label1;
public Win32Form1() {
this.InitializeComponent();
}
[System.STAThreadAttribute()]
public static void Main() {
System.Windows.Forms.Application.Run(new Win32Form1());
}
private void InitializeComponent() {
this.addButton = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.addGrandButton = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.showSelectedButton = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.findButton = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.addButton.Location = new System.Drawing.Point(248, 32);
this.addButton.Size = new System.Drawing.Size(40, 24);
this.addButton.TabIndex = 1;
this.addButton.Text = "Add";
this.addButton.Click += new System.EventHandler(this.addButton_Click);
this.textBox2.Location = new System.Drawing.Point(8, 64);
this.textBox2.Size = new System.Drawing.Size(232, 20);
this.textBox2.TabIndex = 6;
this.textBox2.Text = "";
this.addGrandButton.Location = new System.Drawing.Point(8, 96);
this.addGrandButton.Size = new System.Drawing.Size(280, 23);
this.addGrandButton.TabIndex = 2;
this.addGrandButton.Text = "Add 1,000 Items";
this.addGrandButton.Click += new System.EventHandler(this.addGrandButton_Click);
this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.comboBox1.DropDownWidth = 280;
this.comboBox1.Items.AddRange(new object[] {"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"});
this.comboBox1.Location = new System.Drawing.Point(8, 248);
this.comboBox1.Size = new System.Drawing.Size(280, 21);
this.comboBox1.TabIndex = 7;
this.showSelectedButton.Location = new System.Drawing.Point(8, 128);
this.showSelectedButton.Size = new System.Drawing.Size(280, 24);
this.showSelectedButton.TabIndex = 4;
this.showSelectedButton.Text = "What Item is Selected?";
this.showSelectedButton.Click += new System.EventHandler(this.showSelectedButton_Click);
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Size = new System.Drawing.Size(232, 20);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
this.findButton.Location = new System.Drawing.Point(248, 64);
this.findButton.Size = new System.Drawing.Size(40, 24);
this.findButton.TabIndex = 3;
this.findButton.Text = "Find";
this.findButton.Click += new System.EventHandler(this.findButton_Click);
this.label1.Location = new System.Drawing.Point(8, 224);
this.label1.Size = new System.Drawing.Size(144, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Test ComboBox";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.comboBox1,
this.textBox2,
this.textBox1,
this.showSelectedButton,
this.findButton,
this.addGrandButton,
this.addButton,
this.label1});
this.Text = "ComboBox Sample";
}
private void addButton_Click(object sender, System.EventArgs e) {
comboBox1.Items.Add(textBox1.Text);
}
private void addGrandButton_Click(object sender, System.EventArgs e) {
comboBox1.BeginUpdate();
for (int i = 0; i < 1000; i++) {
comboBox1.Items.Add("Item 1" + i.ToString());
}
comboBox1.EndUpdate();
}
private void findButton_Click(object sender, System.EventArgs e) {
int index = comboBox1.FindString(textBox2.Text);
comboBox1.SelectedIndex = index;
}
private void showSelectedButton_Click(object sender, System.EventArgs e) {
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;
MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "\n" +
"Index: " + selectedIndex.ToString());
}
}
}
[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。
關注Combobox2.0
呵呵﹐沒聽過﹐學習學習
关注中。
什么呀,说的这么全?还有啥好说的呢!
.net framework 1.1中带的吗?
1.0里好像没有
我明白你的意思。以前我用VB5做过,当时做很简单,但到了C#或VB7后,就不好用了。
下面是我的试验,结果到了最后一句,运行出错,我现在都不用它了,改用ListView。
[C#]
this.axComboBox1.ColumnCount = 3;
this.axComboBox1.ColumnWidths = "100;100;100" ;
string []s1 = new string[] {"a", "b", "c"};
int []i1 = new int [] {1, 2, 3};
object []o1 = new object[s1.Length];
for(int i=0;i<s1.Length;i++)
{
o1[i] = (object) s1[i];
}
object []o2 = new object[i1.Length];
for(int i=0;i<i1.Length;i++)
{
o2[i] = (object) i1[i];
}
this.axComboBox1.AddItem( ref o1[0], ref o2[0] );
[VB5]
Sub InitCmbPzClass()
Dim dbTemp As Database, rsTemp As Recordset, sSQL As String
Dim nPzClassCnt As Long, i As Long
sSQL = "select * from pz_class order by code"
Set dbTemp = DBEngine(0).OpenDatabase(sZbDataFullName)
Set rsTemp = dbTemp.OpenRecordset(sSQL, dbOpenSnapshot)
If rsTemp.RecordCount > 0 Then
rsTemp.MoveLast
rsTemp.MoveFirst
nPzClassCnt = rsTemp.RecordCount
cmbPzClass.ColumnCount = 2
cmbPzClass.ColumnWidths = "30;100"
cmbPzClass.ListWidth = 130
cmbPzClass.TextColumn = 2
For i = 0 To nPzClassCnt - 1
cmbPzClass.AddItem rsTemp("code")
cmbPzClass.Column(1, i) = rsTemp("name")
rsTemp.MoveNext
Next i
cmbPzClass.ListIndex = 0
End If
dbTemp.Close: Set dbTemp = Nothing: Set rsTemp = Nothing
End Sub
那是个古老的Com组件阿~~~为什么要用它`~~
你用.net下面的ComboBox的OwnerDraw实际上相信可以实现你的要求。
与其研究那个古董还不如看看新的如何实现~~呵呵~~~
说说你需要什么样的ComboBox~~也许我能帮忙~~~