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

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

ComboBoxDIY.cs文件

//ComboBoxDIY.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace WindowsApplication7 { public partial class ComboBoxDIY : UserControl { public bool buttondown = false; public ComboBoxDIY() { InitializeComponent(); this.listBox1.Visible = false; this.vScrollBar1.Visible = false; } private void button1_Click(object sender, EventArgs e) { //下拉按钮未曾按下 if (this.buttondown==false) { //listbox所有数据的项数 int count = this.listBox1.Items.Count; //获取listbox所能显示的项数 int displaycount = this.listBox1.Height / this.listBox1.ItemHeight; //滚动条显示的最大值 int scrollmax = 0; //垂直方向上显示内容数目大于所能显示的数目时 //垂直滚动条直接可见 if (count > displaycount) { scrollmax = count - 1; this.vScrollBar1.Visible = true; } this.vScrollBar1.LargeChange = displaycount; this.vScrollBar1.Maximum = scrollmax; this.vScrollBar1.Minimum = 0; this.vScrollBar1.Scroll += new ScrollEventHandler(vscroll); this.listBox1.Visible = true; //下拉按钮按下 this.buttondown = true; } //下拉按钮已按下 else { if(this.vScrollBar1.Visible)this.vScrollBar1.Visible = false; this.listBox1.Visible = false; //下拉按钮弹起 this.buttondown = false; } } private void vscroll(object sender, ScrollEventArgs e) { //ScrollBar控制listBox滚动 this.listBox1.TopIndex=e.NewValue; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { //文本框显示选择结果 this.textBox1.Text =this.listBox1.Items[this.listBox1.SelectedIndex].ToString(); this.vScrollBar1.Visible = false; this.listBox1.Visible = false; //下拉按钮弹起 this.buttondown = false; } } }

ComboBoxDIY.Designer.cs文件

//ComboBoxDIY.Designer.cs namespace WindowsApplication7 { partial class ComboBoxDIY { ///

/// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.listBox1 = new System.Windows.Forms.ListBox(); this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(85, 0); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(65, 44); this.button1.TabIndex = 0; this.button1.Text = "▼"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.textBox1.Location = new System.Drawing.Point(0, 0); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(88, 44); this.textBox1.TabIndex = 1; // // listBox1 // this.listBox1.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.listBox1.FormattingEnabled = true; this.listBox1.ItemHeight = 33; this.listBox1.Items.AddRange(new object[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}); this.listBox1.Location = new System.Drawing.Point(0, 44); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(148, 103); this.listBox1.TabIndex = 2; this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); // // vScrollBar1 // this.vScrollBar1.Location = new System.Drawing.Point(85, 44); this.vScrollBar1.Name = "vScrollBar1"; this.vScrollBar1.Size = new System.Drawing.Size(63, 103); this.vScrollBar1.TabIndex = 3; // // UserControl1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.vScrollBar1); this.Controls.Add(this.listBox1); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "UserControl1"; this.Size = new System.Drawing.Size(149, 148); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.VScrollBar vScrollBar1; } }

 

你可能感兴趣的:(如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变?(自绘ComboBox))