如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变?(自绘ComboBox) [转]

原文地址:http://bbs.csdn.net/topics/390135022

http://blog.csdn.net/scsdn/article/details/4363299

想使用winform的combox控件,但是觉得控件太小了,想在触摸屏上使用,感觉combox后面的下拉选择的三角形太小了,combox的宽度也太小了,请问下,combox的这些属性,可以更改吗?能够使小三角形大一些嘛?能够使combox的宽度宽些不?请大家给予帮助,谢谢

 

设置字体可以使得combobox变大。

combobox,listview等一些控件的外观会由于字体大小的改变而改变。

这个没办法。
除非重绘
http://blog.csdn.net/scsdn/article/details/4363299

 

关于如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变的问题,通过自绘自定义控件得以解决。

ComboBoxDIY.cs文件

  1. //ComboBoxDIY.cs  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Drawing;  
  6. using System.Data;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsApplication7  
  11. {  
  12.     public partial class ComboBoxDIY : UserControl  
  13.     {  
  14.         public bool buttondown = false;  
  15.         public ComboBoxDIY()  
  16.         {  
  17.             InitializeComponent();  
  18.             this.listBox1.Visible = false;  
  19.             this.vScrollBar1.Visible = false;  
  20.         }  
  21.   
  22.         private void button1_Click(object sender, EventArgs e)  
  23.         {  
  24.             //下拉按钮未曾按下  
  25.             if (this.buttondown==false)  
  26.             {  
  27.                 //listbox所有数据的项数  
  28.                 int count = this.listBox1.Items.Count;  
  29.                 //获取listbox所能显示的项数  
  30.                 int displaycount = this.listBox1.Height / this.listBox1.ItemHeight;  
  31.                 //滚动条显示的最大值  
  32.                 int scrollmax = 0;  
  33.                 //垂直方向上显示内容数目大于所能显示的数目时  
  34.                 //垂直滚动条直接可见  
  35.                 if (count > displaycount)  
  36.                 {  
  37.                     scrollmax = count - 1;  
  38.                     this.vScrollBar1.Visible = true;  
  39.                 }  
  40.                 this.vScrollBar1.LargeChange = displaycount;  
  41.                 this.vScrollBar1.Maximum = scrollmax;  
  42.                 this.vScrollBar1.Minimum = 0;  
  43.                 this.vScrollBar1.Scroll += new ScrollEventHandler(vscroll);  
  44.                   
  45.                 this.listBox1.Visible = true;  
  46.                 //下拉按钮按下  
  47.                 this.buttondown = true;  
  48.             }  
  49.             //下拉按钮已按下  
  50.             else   
  51.             {  
  52.                 if(this.vScrollBar1.Visible)this.vScrollBar1.Visible = false;  
  53.                 this.listBox1.Visible = false;  
  54.                 //下拉按钮弹起  
  55.                 this.buttondown = false;  
  56.             }  
  57.         }  
  58.   
  59.         private void vscroll(object sender, ScrollEventArgs e)  
  60.         {  
  61.             //ScrollBar控制listBox滚动  
  62.             this.listBox1.TopIndex=e.NewValue;  
  63.         }  
  64.   
  65.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)  
  66.         {  
  67.             //文本框显示选择结果  
  68.             this.textBox1.Text =this.listBox1.Items[this.listBox1.SelectedIndex].ToString();  
  69.             this.vScrollBar1.Visible = false;  
  70.             this.listBox1.Visible = false;  
  71.             //下拉按钮弹起  
  72.             this.buttondown = false;  
  73.         }  
  74.     }  
  75. }  

 

ComboBoxDIY.Designer.cs文件

  1. //ComboBoxDIY.Designer.cs  
  2. namespace WindowsApplication7  
  3. {  
  4.     partial class ComboBoxDIY  
  5.     {  
  6.         /// <summary>   
  7.         /// 必需的设计器变量。  
  8.         /// </summary>  
  9.         private System.ComponentModel.IContainer components = null;  
  10.   
  11.         /// <summary>   
  12.         /// 清理所有正在使用的资源。  
  13.         /// </summary>  
  14.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
  15.         protected override void Dispose(bool disposing)  
  16.         {  
  17.             if (disposing && (components != null))  
  18.             {  
  19.                 components.Dispose();  
  20.             }  
  21.             base.Dispose(disposing);  
  22.         }  
  23.  
  24.         #region 组件设计器生成的代码  
  25.   
  26.         /// <summary>   
  27.         /// 设计器支持所需的方法 - 不要  
  28.         /// 使用代码编辑器修改此方法的内容。  
  29.         /// </summary>  
  30.         private void InitializeComponent()  
  31.         {  
  32.             this.button1 = new System.Windows.Forms.Button();  
  33.             this.textBox1 = new System.Windows.Forms.TextBox();  
  34.             this.listBox1 = new System.Windows.Forms.ListBox();  
  35.             this.vScrollBar1 = new System.Windows.Forms.VScrollBar();  
  36.             this.SuspendLayout();  
  37.             //   
  38.             // button1  
  39.             //   
  40.             this.button1.Location = new System.Drawing.Point(85, 0);  
  41.             this.button1.Name = "button1";  
  42.             this.button1.Size = new System.Drawing.Size(65, 44);  
  43.             this.button1.TabIndex = 0;  
  44.             this.button1.Text = "▼";  
  45.             this.button1.UseVisualStyleBackColor = true;  
  46.             this.button1.Click += new System.EventHandler(this.button1_Click);  
  47.             //   
  48.             // textBox1  
  49.             //   
  50.             this.textBox1.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  51.             this.textBox1.Location = new System.Drawing.Point(0, 0);  
  52.             this.textBox1.Name = "textBox1";  
  53.             this.textBox1.Size = new System.Drawing.Size(88, 44);  
  54.             this.textBox1.TabIndex = 1;  
  55.             //   
  56.             // listBox1  
  57.             //   
  58.             this.listBox1.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));  
  59.             this.listBox1.FormattingEnabled = true;  
  60.             this.listBox1.ItemHeight = 33;  
  61.             this.listBox1.Items.AddRange(new object[] {  
  62.             "0",  
  63.             "1",  
  64.             "2",  
  65.             "3",  
  66.             "4",  
  67.             "5",  
  68.             "6",  
  69.             "7",  
  70.             "8",  
  71.             "9"});  
  72.             this.listBox1.Location = new System.Drawing.Point(0, 44);  
  73.             this.listBox1.Name = "listBox1";  
  74.             this.listBox1.Size = new System.Drawing.Size(148, 103);  
  75.             this.listBox1.TabIndex = 2;  
  76.             this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);  
  77.             //   
  78.             // vScrollBar1  
  79.             //   
  80.             this.vScrollBar1.Location = new System.Drawing.Point(85, 44);  
  81.             this.vScrollBar1.Name = "vScrollBar1";  
  82.             this.vScrollBar1.Size = new System.Drawing.Size(63, 103);  
  83.             this.vScrollBar1.TabIndex = 3;  
  84.             //   
  85.             // UserControl1  
  86.             //   
  87.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  
  88.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  89.             this.Controls.Add(this.vScrollBar1);  
  90.             this.Controls.Add(this.listBox1);  
  91.             this.Controls.Add(this.textBox1);  
  92.             this.Controls.Add(this.button1);  
  93.             this.Name = "UserControl1";  
  94.             this.Size = new System.Drawing.Size(149, 148);  
  95.             this.ResumeLayout(false);  
  96.             this.PerformLayout();  
  97.   
  98.         }  
  99.  
  100.         #endregion  
  101.   
  102.         private System.Windows.Forms.Button button1;  
  103.         private System.Windows.Forms.TextBox textBox1;  
  104.         private System.Windows.Forms.ListBox listBox1;  
  105.         private System.Windows.Forms.VScrollBar vScrollBar1;  
  106.   
  107.     }  
  108. }  
posted @ 2014-03-21 16:25  CocoWang  阅读(3753)  评论(0编辑  收藏  举报