All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
C#中ComboBox控件应用实例

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:16:02 Browse times: 420 Comment times: 0

C#中ComboBox控件应用实例


Head photo

Go homepage
Upload pictures
Write articles

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窗体代码文件完整程序代码★★★★★




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.