/*****************************************************************
* Copyright(C) 2011 比万科技
* 文件名 :FrmMain ;
* 中文标识 :主窗体 ;
* 文件功能描述 :可以为该事件创建事件处理程序,以确定 ComboBox
* 中选定的索引何时更改。这在需要根据 ComboBox 中
* 的当前选定内容显示其他控件中的信息时非常有用。
* 可以使用该事件的事件处理程序来加载其他控件中的
* 信息。
* 创建标识 :HT20110404
* 日期 :2011-04-04 ,星期一,
* 编者 :beeone
*
* 修改标识 :
* 修改描述 :
*
*****************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace HTWork
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
InitializesComboBox();
}
internal System.Windows.Forms.ComboBox ComboBox1;
private void InitializesComboBox()
{
this.ComboBox1 = new System.Windows.Forms.ComboBox();
string[] employees = new string[]{"张,一", "张,二",
"王,某", "李,一",
"赵,一", "张,一", "姜,某",
"关,某", "隋,某", "孙,一 ",
"李,一", "张,二",
"张,二", "郑,一", "张,一",
"张,二", "郑,一", "孙,一",
"赵,一", "吴,某", "钱,某",
"孙,一 "};
ComboBox1.Items.AddRange(employees);
this.ComboBox1.Location = new System.Drawing.Point(102, 102);
this.ComboBox1.MaxDropDownItems = 25;
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new System.Drawing.Size(136, 81);
this.ComboBox1.TabIndex = 0;
this.Controls.Add(this.ComboBox1);
// Associate the event-handling method with the
// SelectedIndexChanged event.
this.ComboBox1.SelectedIndexChanged +=
new System.EventHandler(ComboBox1_SelectedIndexChanged);
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
// Save the selected employee's name, because we will remove
// the employee's name from the list.
string selectedEmployee = (string)ComboBox1.SelectedItem;
int count = 0;
int resultIndex = -1;
// Call the FindStringExact method to find the first
// occurrence in the list.
resultIndex = ComboBox1.FindStringExact(selectedEmployee);
// Remove the name as it is found, and increment the found count.
// Then call the FindStringExact method again, passing in the
// index of the current found item so the search starts there
// instead of at the beginning of the list.
MessageBox.Show(Convert.ToString(resultIndex));
while (resultIndex != -1)
{
ComboBox1.Items.RemoveAt(resultIndex);
count += 1;
resultIndex = ComboBox1.FindStringExact(selectedEmployee,
resultIndex);
}
// Update the text in Textbox1.
TextBox1.Text = TextBox1.Text + "\r\n" + selectedEmployee + ": "
+ count;
listBox1.Items.Add (listBox1.Text + "\r\n" + selectedEmployee + ":" + count);
}
}
}