• C#中的ComboBox实现只能选择不能输入,且下拉框中有默认值。


    下拉框有DropDownStyle这一属性,把DropDownStyle类型选为DropDownList,则下拉框只能选择不能输入了。但是这时的下拉框是没有默认值的,即使在Text属性中输入默认值,也不起作用。就要在(某某某.Designer.cs)文件中修改。
    这是没有修改的:
    this.NameTemplateBox.Cursor = System.Windows.Forms.Cursors.Default; 
    this.NameTemplateBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
    this.NameTemplateBox.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
    this.NameTemplateBox.FormattingEnabled = true; 
    this.NameTemplateBox.Items.AddRange(new object[] { 
    "-- choose the template --", 
    "First", 
    "First,Last", 
    "First,Middle,Last"}); 
    this.NameTemplateBox.Location = new System.Drawing.Point(138, 124); 
    this.NameTemplateBox.Name = "NameTemplateBox"; 
    this.NameTemplateBox.Size = new System.Drawing.Size(150, 23); 
    this.NameTemplateBox.TabIndex = 15; 

    加粗的那行代码是下拉框只能选择不能输入的设置。
    要在那行代码之前加一句默认值的设置。修改如下:
    this.NameTemplateBox.Cursor = System.Windows.Forms.Cursors.Default; 
    this.NameTemplateBox.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 
    this.NameTemplateBox.FormattingEnabled = true; 
    this.NameTemplateBox.Items.AddRange(new object[] { 
    "-- choose the template --", 
    "First", 
    "First,Last", 
    "First,Middle,Last"}); 
    this.NameTemplateBox.Text = this.NameTemplateBox.Items[0].ToString(); 
    this.NameTemplateBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
    this.NameTemplateBox.Location = new System.Drawing.Point(138, 124); 
    this.NameTemplateBox.Name = "NameTemplateBox"; 
    this.NameTemplateBox.Size = new System.Drawing.Size(150, 23); 
    this.NameTemplateBox.TabIndex = 15; 

    如果第一行加粗的改为
    this.NameTemplateBox.Text =“默认值”; 
    则和在属性中直接输入默认值一样,会被自动删除掉。
    注意:第一行加粗的语句要写在
    this.NameTemplateBox.Items.AddRange(new object[] { 
    "-- choose the template --", 
    "First", 
    "First,Last", 
    "First,Middle,Last"}); 
    语句的后面。上面的语句可以在Items的属性中设置。

    以上都写好后,就会实现效果,但是效果在一些组件有改变(即使不和下拉框有关联的),整个文件会重构,里面修改过的语句就会还原到,等于什么也没有改。换句话说,就是每次组件的修改,就要把上面的动作在做一遍。。。。。。
    我的方法是把默认值的赋值全部写在另一个自己写的方法里面,然后在初始化组建之后,调用这个方法。

  • 相关阅读:
    CentOS 7 虚拟机的安装
    2 MySQL rpm
    01-在实体类上加了lombok的@Data注解
    02-myBatisPlus的wrapper接口的使用
    2 MySQL rpm 安装 --下载
    1-MySQL介绍
    MySQL的不归路
    电脑型号4 1500 内存大 机械大硬盘
    电脑型号3 1200 大硬盘
    电脑概览 2 1200 固态SSD
  • 原文地址:https://www.cnblogs.com/moyuling/p/4336283.html
Copyright © 2020-2023  润新知