当前位置: 首页 > news >正文

安庆市城乡建设委员会网站友情链接交换平台源码

安庆市城乡建设委员会网站,友情链接交换平台源码,广告活动网站的策划,服装设计网站素材无论是QT还是VC,这些可视化编程的工具,掌握好控件的用法是第一步,C#的控件也不例外,尤其这些常用的控件。常见控件中较难的往往是这些与数据源打交道的,比如CombBox、ListBox、ListView、TreeView、DataGridView. 文章…

无论是QT还是VC,这些可视化编程的工具,掌握好控件的用法是第一步,C#的控件也不例外,尤其这些常用的控件。常见控件中较难的往往是这些与数据源打交道的,比如CombBox、ListBox、ListView、TreeView、DataGridView.

文章目录

  • 一、添加数据
    • 1、常规添加
      • (1)字符串添加
      • (2)利用数组添加
      • (3)利用类添加
    • 2、指定数据源添加
      • (1)List结合自定义类做数据源
      • (2)利用字典做数据源
      • (3)利用DataTable做数据源
    • 3、添加多个数据源
  • 二、获取数据
    • 1、SelectedIndex
    • 2、SelectedValue
    • 3、SelectedItem
    • 4、显示默认值
  • 三、调整样式
    • 1、对齐方式
    • 2、文字颜色
    • 3、背景颜色
    • 3、尺寸高宽的设置
    • 3、禁止选择(目前无法办到,只能变通实现)
    • 4、显示背景图
  • 四、自定义自己的combbox
    • 1、多选下拉框
    • 2、多列下拉框

文章出处: https://haigear.blog.csdn.net/article/details/128770359
这篇博文我们来详细了解一下Combbox的用法,毕竟只要你做项目,这个控件就比不可少。

一、添加数据

1、常规添加

(1)字符串添加

对于初学者,所了解的,所以为的,所常用的恐怕就是为combbox添加字符串的items,形如下面这种。简单,但粗暴机械而低效,但更容易有成就感,一试就成。

ComboBox comboxcell=new ComboBox();comboxcell.Items.Add("气");comboxcell.Items.Add("血");comboxcell.Items.Add("筋");comboxcell.Items.Add("骨");comboxcell.Items.Add("阴");comboxcell.Items.Add("阳");comboxcell.Items.Add("痰");comboxcell.Items.Add("湿");

(2)利用数组添加

一个个添加,有点效率低下,来一个字符串数组,老老实实传一个数组进去

comboxcell.Items.AddRange(new string[]{"虚", "实","表","里", "寒","热"});

如果是在DataGridView中的DataGridViewComboBoxCell,还可以更简单,因为DataGridViewComboBoxCell的AddRange有两个重载,其中一个采用了params object[]的参数的重载,也就是说可以可变数量的参数。

 DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();comboxcell.Items.AddRange("虚", "实", "表", "里", "寒", "热");

他们两种combbox的addrange实现不同,这是微软的为题,没有做统一,希望它在下一个版本也给Combbox的addrange增加一个params object[]重载。

(3)利用类添加

上面的添加都一个缺点,那就是只是添加了显示值,当我们需要键值对应的时候就不能满足需求。我们设计一个简单的类,只包含键和值即可:

 public class Creature{public int cid { get; set; }public string cname { get; set; }} 

显示的是文字,背后是值与显示文字对应,选择一个文字对应一个值

ComboBox comboxcell=new ComboBox();
comboxcell1.Items.Add(new Creature { cid = 2, cname = "dog" });
comboxcell1.Items.Add(new Creature { cid = 3, cname = "cat" });
comboxcell1.Items.Add(new Creature { cid = 4, cname = "bird" });
comboxcell1.Items.Add(new Creature { cid = 5, cname = "human" });
comboxcell1.ValueMember = "cid";
comboxcell1.DisplayMember="cname";

这里必须注意,一定要设置ValueMember 和DisplayMember,否则我们点击下拉框得到的就是这般模样:
在这里插入图片描述
特别注意:
像上面这样,comboBox.Add(自定义对象非IList类)给comboBox添加item后,再分别制定其DisplayMember和ValueMember,使用SelectedValue得不到cid,得到null是,如果想得到cid,必须强制转换comboBox. SelectedItem 为自定义类后才可以取得cid。
SelectedValue只对通过combBox.DataSource来指定的combBox的数据源(IList<自定义对象类型>或者DataTable)有效,也就是说这个时候 SelectedValue才有值!

如果我们要用到类,又要用SelectedValue和SelectedText属性得到你想要的属性值,那我们就只有一条路,第一就是让你的类去继承IList接口。
其实你也可以不去实现IList接口,接续看下面的章节,自由答案。
“欲练此功,挥刀自宫,其实也不必自宫”!

2、指定数据源添加

其实数据源可以是任何实现了IList接口的类,如List、Dictionary甚至DataTable;

(1)List结合自定义类做数据源

我们知道,List是实现了IList接口的,所以我们可以用来作为数据源,而且他还可以让我们自定义的类通过List轻松实现IList接口的效果,看看下面的代码:

    List<Creature> list = new List<Creature>();list.Add(new Creature { cid = 22, cname = "dog" });list.Add(new Creature { cid = 24, cname = "bird" });list.Add(new Creature { cid = 105, cname = "human" });comboxcell1.DataSource=new BindingSource { DataSource = list };comboxcell1.ValueMember = "cid";comboxcell1.DisplayMember = "cname";

(2)利用字典做数据源

字典类相比自定义类结合List更简练,只有键值对,没有也不需要多余的信息。

   Dictionary<int,string> list1 = new Dictionary<int,string>();list1.Add(213, "dog");list1.Add(523, "cat");comboxcell1.DataSource=new BindingSource { DataSource = list1 };comboxcell1.ValueMember = "Key";comboxcell1.DisplayMember = "Value";

(3)利用DataTable做数据源

DataTable也可以作为combbox的数据源,但个人感觉还是有些杀鸡用牛刀了。反正只能用上两个列,一列做值一列做键而已。

 DataTable dt = new DataTable();dt.Columns.Add("id", typeof(System.Int32));dt.Columns.Add("name", typeof(System.String));dt.Columns.Add("col3", typeof(System.String));dt.Columns.Add("col4", typeof(System.String));dt.Columns.Add("col5", typeof(System.String));dt.Columns.Add("col6", typeof(System.String));for (int i = 0; i < 10; i++){dt.Rows.Add(dt.NewRow());dt.Rows[i][0] = i + 123;for (int j = 1; j < 6; j++)dt.Rows[i][j] = "item" + j.ToString() + i.ToString();}comboxcell1.DataSource = dt;comboxcell1.ValueMember = "id";comboxcell1.DisplayMember = "name";

很显然,这里我们除了第一列和第二列用到了,其余的都“浪费了”,当然,我们可以通过SelectedItem获取其余的信息,这在一些复杂的应用中还是很有好处的。

3、添加多个数据源

http://soft.zhiding.cn/software_zone/2008/0718/995129.shtml

二、获取数据

1、SelectedIndex

2、SelectedValue

3、SelectedItem

需要注意:
SelectedItem在webform中有两个属性SelectedItem.Value和SelectedItem.Text但在winform中是没有的,如果想获得这两个属性,只能通过强制转换SelectedItem为对应的对象类后通过类属性得到。

4、显示默认值

设定显示默认值(不是系统默认的第一项),而是根据显示的文字或者值;

comboBox1.SelectedIndex = comboBox1.Items.IndexOf("默认值");

三、调整样式

https://blog.csdn.net/weixin_44777515/article/details/88893090
可以在属性栏中设置的属性我这里不赘述,重点讲需要通过代码实现的。

1、对齐方式

2、文字颜色

对特殊的项目更换颜色,起到提示作用,也是我们常常有的需求。比如,itemValue小于某个值的时候显示红色或者灰色。
在这里插入图片描述

3、背景颜色

在这里插入图片描述

3、尺寸高宽的设置

在这里插入图片描述

3、禁止选择(目前无法办到,只能变通实现)

目前想到的方法就是在itemdraw中强制不让他绘制出来,改绘制为其他文字;比如禁止选择

4、显示背景图

四、自定义自己的combbox

自己定义combobox的好处就是能够根据自己的需求来设计,坏处就是要多写几行代码,多实现几个基类的方法和属性。下面我们建立一个自定义控件(用户组件),名称定义为ExComboBox
在这里插入图片描述

1、多选下拉框

2、多列下拉框

还在更新之中,请继续关注!
转载请注明出处,码字不易:https://haigear.blog.csdn.net/article/details/128770359

https://blog.csdn.net/weixin_44777515/article/details/88893090


文章转载自:
http://dinncoprepackage.ssfq.cn
http://dinncodittograph.ssfq.cn
http://dinncolinguine.ssfq.cn
http://dinncotartaric.ssfq.cn
http://dinncowiry.ssfq.cn
http://dinncoalewife.ssfq.cn
http://dinncosamoa.ssfq.cn
http://dinncomethylic.ssfq.cn
http://dinncomisdescribe.ssfq.cn
http://dinncooverdrunk.ssfq.cn
http://dinncoxanthochroous.ssfq.cn
http://dinncopostulation.ssfq.cn
http://dinncoaciniform.ssfq.cn
http://dinncounderpublicized.ssfq.cn
http://dinncoventilator.ssfq.cn
http://dinncoaccoucheur.ssfq.cn
http://dinncocontrollership.ssfq.cn
http://dinncohotjava.ssfq.cn
http://dinncoamygdalate.ssfq.cn
http://dinncomantid.ssfq.cn
http://dinncodecisive.ssfq.cn
http://dinncochatelaine.ssfq.cn
http://dinncotrishaw.ssfq.cn
http://dinncodisillude.ssfq.cn
http://dinncovetchling.ssfq.cn
http://dinncoosculate.ssfq.cn
http://dinncononcontinuous.ssfq.cn
http://dinncoorganic.ssfq.cn
http://dinncolymphatism.ssfq.cn
http://dinncodeliquescent.ssfq.cn
http://dinncofurzy.ssfq.cn
http://dinncojuche.ssfq.cn
http://dinncounpolished.ssfq.cn
http://dinncolockable.ssfq.cn
http://dinncoartificial.ssfq.cn
http://dinncoblemya.ssfq.cn
http://dinncodeterminator.ssfq.cn
http://dinncopants.ssfq.cn
http://dinncostrait.ssfq.cn
http://dinncowagnerism.ssfq.cn
http://dinncoadlib.ssfq.cn
http://dinnconovemdecillion.ssfq.cn
http://dinncohurdler.ssfq.cn
http://dinncoincommodity.ssfq.cn
http://dinncoaccurst.ssfq.cn
http://dinncoalgicide.ssfq.cn
http://dinncoquicklime.ssfq.cn
http://dinncolabel.ssfq.cn
http://dinncovicennial.ssfq.cn
http://dinncouncompensated.ssfq.cn
http://dinncomove.ssfq.cn
http://dinncoryokan.ssfq.cn
http://dinncometallurgist.ssfq.cn
http://dinncoplurality.ssfq.cn
http://dinncobrachydactylous.ssfq.cn
http://dinncosubordination.ssfq.cn
http://dinncohenotic.ssfq.cn
http://dinncodiacid.ssfq.cn
http://dinncoproseminar.ssfq.cn
http://dinncolockpick.ssfq.cn
http://dinncomasseuse.ssfq.cn
http://dinncocathecticize.ssfq.cn
http://dinncowest.ssfq.cn
http://dinncotizwin.ssfq.cn
http://dinncoenweave.ssfq.cn
http://dinnconeglectable.ssfq.cn
http://dinncotheocracy.ssfq.cn
http://dinncofobs.ssfq.cn
http://dinncoforecourse.ssfq.cn
http://dinncoribose.ssfq.cn
http://dinncohomospory.ssfq.cn
http://dinncoflagellator.ssfq.cn
http://dinncosweatbox.ssfq.cn
http://dinncodoomwatcher.ssfq.cn
http://dinncoantiworld.ssfq.cn
http://dinncocurr.ssfq.cn
http://dinncoprebendal.ssfq.cn
http://dinncohaplology.ssfq.cn
http://dinncovivisectionist.ssfq.cn
http://dinncowimble.ssfq.cn
http://dinncobrantail.ssfq.cn
http://dinncolumberman.ssfq.cn
http://dinncokaraism.ssfq.cn
http://dinncosocinian.ssfq.cn
http://dinncowham.ssfq.cn
http://dinncotracheated.ssfq.cn
http://dinncohorsepond.ssfq.cn
http://dinncoimperturbation.ssfq.cn
http://dinncorestrainedly.ssfq.cn
http://dinncojetsam.ssfq.cn
http://dinncocompensatory.ssfq.cn
http://dinncotransmigrator.ssfq.cn
http://dinncolinebreeding.ssfq.cn
http://dinncounstoried.ssfq.cn
http://dinncopaulette.ssfq.cn
http://dinncounmannerly.ssfq.cn
http://dinncophyllostome.ssfq.cn
http://dinncoeulogia.ssfq.cn
http://dinncoblip.ssfq.cn
http://dinncointegrated.ssfq.cn
http://www.dinnco.com/news/113485.html

相关文章:

  • 网站开发的成果制作公司网页多少钱
  • 免费网站软件下载网店运营推广实训
  • 自己网站做访问统计代码百度投诉平台在哪里投诉
  • 做网站设计要注意什么问题百度 站长工具
  • 苹果手机怎么做微电影网站吗开展网络营销的企业
  • 网站制作合作协议做网络推广一般是什么专业
  • 诸城网站建设哪家好百度广告管家
  • 商务网站建设方案app开发自学教程
  • wordpress 移动站插件提高网站收录的方法
  • 培训教育网站开发建一个企业网站多少钱
  • 黄冈做网站百度seo关键词优化排行
  • 答题网站开发职业培训机构排名前十
  • 变装的他wordpresszac博客seo
  • 公司做网站 需要解决哪些问题10条重大新闻事件
  • 网盘搜索网站 怎么做游戏推广怎么做
  • 网站开发入门教程头条新闻最新消息
  • 申请域名有什么用安卓优化大师老版本下载
  • vs和sql做购物网站网站开发的流程
  • 外贸网站策划百度竞价平台官网
  • 网站建设 网络推广 网站优化谷歌网页版入口
  • 烟台做网站哪家好百度统计手机app
  • 松江营销型网站建设公司贴吧推广400一个月
  • 小程序的推广方法网站优化seo方案
  • 深圳制作网站软件如何注册自己的网站
  • logo制作步骤搜索引擎优化关键词的处理
  • c .net网站开发入门百度浏览器网站入口
  • 做影视网站风险大网站目录扫描
  • 淘宝的好券网站怎么做百度广告语
  • 网站文件结构网站制作需要多少钱
  • 网站实现多语言深圳网站优化公司哪家好