Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:16:02 C#中ComboBox控件应用实例
C#中ComboBox控件应用实例
ComboBox控件1.功能ComboBox控件用于在下拉组合框中显示数据,它结合了TextBox控件和ListBox控件的功能。使用此控件时,可以在组合框中输入文本项,也可以从列表中选择项。图1为ComboBox控件。图1 ComboBox控件2.属性ComboBox控件常用属性及说明如表1所示。表1 ComboBox控件常用属性及说明下面详细介绍DropDownStyle属性,该属性用来设置ComboBox控件的显示样式。语法: public ComboBoxStyle DropDownStyle { get; set; }属性值:ComboBoxStyle值之一,默认为 DropDown。ComboBoxStyle:用来指定ComboBox样式。DropDownStyle:设置的样式值。值为DropDown时,表示文本部分可编辑,用户必须单击箭头按钮来显示列表部分,这是默认样式;值为DropDownList时,用户不能直接编辑文本部分,用户必须单击箭头按钮来显示列表部分;值为Simple时,文本部分可编辑,而且列表部分可见。示例ComboBox控件样式本示例将ComboBox控件的DropDownStyle属性设置为“DropDownList”,示例运行结果如图2所示。图2 DropDownStyle属性程序主要代码如下: this. cmbDownStyle.DropDownStyle =ComboBoxStyle.DropDownList;3.方法(1)Select方法。该方法从ComboBox中选取指定的项。(2)SelectALL方法。该方法用来选择ComboBox控件的可编辑部分的所有文本。语法: public void SelectAll ()完整程序代码如下:★★★★★主程序文件完整程序代码★★★★★ using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_06{static class Program{/// /// 应用程序的主入口点。/// [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmCombox());}}}★★★★★frmCombox窗体设计文件完整程序代码★★★★★ using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Data.SqlClient;using System.Windows.Forms;namespace _8_06{public partial class frmCombox : Form{public frmCombox(){InitializeComponent();}private void frmCombox_Load(object sender, EventArgs e){this.cmbDownStyle.DropDownStyle = ComboBoxStyle.DropDownList;SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=zhy");con.Open();SqlCommand com = new SqlCommand("select * from student", con);SqlDataReader dr = com.ExecuteReader();this.cmbDownStyle.Items.Clear();while (dr.Read()){this.cmbDownStyle.Items.Add(dr[0].ToString());}dr.Close();con.Close();}private void cmbDownStyle_SelectedIndexChanged(object sender, EventArgs e){//this.comText.Text = this.cmbDownStyle.SelectedItem.ToString();}private void BntSelectALL_Click(object sender, EventArgs e){//this.comText.SelectAll();}}}★★★★★frmCombox窗体代码文件完整程序代码★★★★★
赞