增加组合框的高度

编程入门 行业动态 更新时间:2024-10-24 22:23:54
本文介绍了增加组合框的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须增加窗体组合框的高度。我怎样才能轻松实现它。此外,我不能增加我的字体大小。只有控件必须增加 提前致谢。 更新部分 我已经完成了但有问题。请使用 System.Windows.Forms查看我的代码

; 使用 System.Drawing; 命名空间 WindowsFormsApplication1 { partial class Form1 { /// < 摘要 > /// 必需的设计变量。 /// < / summary > private System.ComponentModel.IContainer components = null ; /// < 摘要 > /// 清理正在使用的所有资源。 /// < / summary > /// < param name =disposing > 如果管理资源应予以处理;否则,false。< / param > protected 覆盖 void Dispose( bool 处理) { if (处置&&(components!= null )) { components.Dispose(); } base .Dispose(disposing); } #region Windows窗体设计器生成的代码 /// < 摘要 > /// Designer支持所需的方法 - 不要修改 /// 使用代码编辑器的方法的内容。 /// < / summary > /// private void InitializeComponent() { this ponents = new System.ComponentModel.Container(); this .customerBindingSource = new System.Windows.Forms.BindingSource(此 ponents); this .invoiceDataSet = new WindowsFormsApplication1.InvoiceDataSet(); this .customerTableAdapter = new WindowsFormsApplication1.InvoiceDataSetTableAdapters.CustomerTableAdapter(); this boBox3 = new System.Windows.Forms.ComboBox(); ((System.ComponentModel.ISupportInitialize)( this .customerBindingSource))。BeginInit(); ((System.ComponentModel.ISupportInitialize)( this .invoiceDataSet))。BeginInit(); this .SuspendLayout(); // // customerBindingSource // this .customerBindingSource.DataMember = 客户; this .customerBindingSource.DataSource = this .invoiceDataSet; // // invoiceDataSet // this .invoiceDataSet.DataSetName = InvoiceDataSet; this .invoiceDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; // // customerTableAdapter // this .customerTableAdapter.ClearBeforeFill = true ; // // comboBox3 // this boBox3.DataSource = this .customerBindingSource; this boBox3.DisplayMember = CusName ; this boBox3.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; 此 boBox3.DropDownWidth = 250 ; 此 boBox3.FormattingEnabled = true ; this boBox3.ItemHeight = 55 ; this boBox3.Location = new System.Drawing.Point( 193 , 71 ); this boBox3.Name = comboBox3 ; this boBox3.Size = new System.Drawing.Size( 58 , 61 ); this boBox3.TabIndex = 2 ; this boBox3.ValueMember = CusId ; this boBox3.DrawItem + = new System.Windows.Forms.DrawItemEventHandler(此 boBox3_DrawItem); this boBox3.MeasureItem + = new System.Windows.Forms.MeasureItemEventHandler(此 boBox3_MeasureItem); // // Form1 // this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; this .ClientSize = new System.Drawing.Size( 482 , 270 ); this .Controls.Add( this boBox3); this .MinimumSize = new System.Drawing.Size( 0 , 30 ); this .Name = Form1; this .Text = Form1; this .Load + = new System.EventHandler(此 .Form1_Load); ((System.ComponentModel.ISupportInitialize)( this .customerBindingSource))。EndInit(); ((System.ComponentModel.ISupportInitialize)( this .invoiceDataSet))。EndInit(); this .ResumeLayout( false ); } public class SeparatorItem { 私人 对象数据; public SeparatorItem( object data) { 此 .data = data; } public 覆盖 string ToString() { if (data!= null ) { return data.ToString(); } return base .ToString(); } } 私有 void comboBox3_MeasureItem( object sender,System.Windows.Forms.MeasureItemEventArgs e) { object comboBoxItem = comboBox3。项[e.Index]; e.ItemWidth = 260 ; e.ItemHeight = 55 ; 大小textSize = e.Graphics.MeasureString(comboBoxItem.ToString(),comboBox3.Font).ToSize(); e.ItemWidth = textSize.Width; } private void comboBox3_DrawItem( object sender,System.Windows.Forms.DrawItemEventArgs e) { Font myFont = new System.Drawing.Font( Comic Sans, 11 ); object comboBoxItem = comboBox3.Items [e.Index]; bool isSeparatorItem =(comboBoxItem SeparatorItem); e.DrawBackground(); e.DrawFocusRectangle(); 使用(画笔textBrush = 新 SolidBrush(e.ForeColor)) { 矩形边界= e.Bounds; 使用(StringFormat format = new StringFormat()) { format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; // e.Graphics.DrawString(comboBoxItem.ToString(),comboBox3.Font,textBrush,bounds,格式); e.Graphics.DrawString(comboBoxItem.ToString(),comboBox3.Font,textBrush,bounds); } } } #endregion private InvoiceDataSet invoiceDataSet; private System.Windows.Forms.BindingSource customerBindingSource; private InvoiceDataSetTableAdapters.CustomerTableAdapter customerTableAdapter; private ComboBox comboBox3; } }

但是我将System.Data.DataRowView作为我的文本。不是组合框中的文字。但是当我从组合框中选择时,我在组合框中得到了正确的值(尽管显示文本是 System.Data.DataRowView

解决方案

你不能 - 组合框始终自动调整大小以匹配字体,AFAIK没有可能改变它。如果你真的希望文本浮动在空白区域的中间,那么你必须从头开始实现自己的ComboBox控件。

我找到了解决方案,请结账我做的更改(粗体文字) private void comboBox3_DrawItem(object sender,System.Windows.Forms.DrawItemEventArgs e) { DataRowView drv =(DataRowView)comboBox3.Items [e.Index]; string name = drv [CusName ] .ToString(); 字体myFont = new System.Drawing.Font(Comic Sans,11); object comboBoxItem = comboBox3.Items [e.Index]; e.DrawBackground(); e.DrawFocusRectangle() ; 使用(刷textBrush =新的SolidBrush(e.ForeColor)) { 矩形边界= e .Bounds; 使用(StringFormat格式=新的StringFormat()) { format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; e.Graphics.DrawString(name,comboBox3.Font ,textBrush,bounds,format); } } }

我找到了解决方案,请查看我做的更改(粗体文字) private void comboBox3_DrawItem(object sender,System.Windows.Forms.DrawItemEventArgs e) { DataRowView drv =(DataRowView )comboBox3.Items [e.Index]; string name = drv [CusName]。ToString(); Font myFont = new System.Drawing.Font(C omic Sans,11); object comboBoxItem = comboBox3.Items [e.Index]; e.DrawBackground(); e.DrawFocusRectangle(); 使用(刷子textBrush =新的SolidBrush(e.ForeColor)) { 矩形边界= e.Bounds; 使用(StringFormat format = new StringFormat()) { format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; e.Graphics.DrawString( name ,comboBox3.Font,textBrush,bounds,format); } } }

Hi, I have to increase the height of a combobox of a windows form. How can i implement it easily. Also i cant increase my font size. Only the control must increase Thanks in advance. UPDATED PART I have done it but with an issue.Please see my code

using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApplication1 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> /// private void InitializeComponent() { thisponents = new System.ComponentModel.Container(); this.customerBindingSource = new System.Windows.Forms.BindingSource(thisponents); this.invoiceDataSet = new WindowsFormsApplication1.InvoiceDataSet(); this.customerTableAdapter = new WindowsFormsApplication1.InvoiceDataSetTableAdapters.CustomerTableAdapter(); thisboBox3 = new System.Windows.Forms.ComboBox(); ((System.ComponentModel.ISupportInitialize)(this.customerBindingSource)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.invoiceDataSet)).BeginInit(); this.SuspendLayout(); // // customerBindingSource // this.customerBindingSource.DataMember = "Customer"; this.customerBindingSource.DataSource = this.invoiceDataSet; // // invoiceDataSet // this.invoiceDataSet.DataSetName = "InvoiceDataSet"; this.invoiceDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; // // customerTableAdapter // this.customerTableAdapter.ClearBeforeFill = true; // // comboBox3 // thisboBox3.DataSource = this.customerBindingSource; thisboBox3.DisplayMember = "CusName"; thisboBox3.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; thisboBox3.DropDownWidth = 250; thisboBox3.FormattingEnabled = true; thisboBox3.ItemHeight = 55; thisboBox3.Location = new System.Drawing.Point(193, 71); thisboBox3.Name = "comboBox3"; thisboBox3.Size = new System.Drawing.Size(58, 61); thisboBox3.TabIndex = 2; thisboBox3.ValueMember = "CusId"; thisboBox3.DrawItem += new System.Windows.Forms.DrawItemEventHandler(thisboBox3_DrawItem); thisboBox3.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(thisboBox3_MeasureItem); // // Form1 // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; this.ClientSize = new System.Drawing.Size(482, 270); this.Controls.Add(thisboBox3); this.MinimumSize = new System.Drawing.Size(0, 30); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.customerBindingSource)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.invoiceDataSet)).EndInit(); this.ResumeLayout(false); } public class SeparatorItem { private object data; public SeparatorItem(object data) { this.data = data; } public override string ToString() { if (data != null) { return data.ToString(); } return base.ToString(); } } private void comboBox3_MeasureItem(object sender,System.Windows.Forms.MeasureItemEventArgs e) { object comboBoxItem = comboBox3.Items[e.Index]; e.ItemWidth = 260; e.ItemHeight = 55; Size textSize = e.Graphics.MeasureString(comboBoxItem.ToString(), comboBox3.Font).ToSize(); e.ItemWidth = textSize.Width; } private void comboBox3_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Font myFont = new System.Drawing.Font("Comic Sans", 11); object comboBoxItem = comboBox3.Items[e.Index]; bool isSeparatorItem = (comboBoxItem is SeparatorItem); e.DrawBackground(); e.DrawFocusRectangle(); using (Brush textBrush = new SolidBrush(e.ForeColor)) { Rectangle bounds = e.Bounds; using (StringFormat format = new StringFormat()) { format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; // e.Graphics.DrawString(comboBoxItem.ToString(), comboBox3.Font, textBrush, bounds, format); e.Graphics.DrawString(comboBoxItem.ToString(), comboBox3.Font, textBrush, bounds); } } } #endregion private InvoiceDataSet invoiceDataSet; private System.Windows.Forms.BindingSource customerBindingSource; private InvoiceDataSetTableAdapters.CustomerTableAdapter customerTableAdapter; private ComboBox comboBox3; } }

But im getting "System.Data.DataRowView" as my text. Not the text in the combobox. But when i pick from the combobox i get the correct value in combobox(Though the display text is "System.Data.DataRowView" )

解决方案

You can't - A combobox always auto sizes to match the font, and AFAIK there is no may to change that. If you really want the text "floating" in the middle of a blank space, then you will have to implement your own ComboBox control from scratch.

I've found the solution, Please checkout the changes (Bold Text) i've made private void comboBox3_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { DataRowView drv = (DataRowView)comboBox3.Items[e.Index]; string name = drv["CusName"].ToString(); Font myFont = new System.Drawing.Font("Comic Sans", 11); object comboBoxItem = comboBox3.Items[e.Index]; e.DrawBackground(); e.DrawFocusRectangle(); using (Brush textBrush = new SolidBrush(e.ForeColor)) { Rectangle bounds = e.Bounds; using (StringFormat format = new StringFormat()) { format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; e.Graphics.DrawString(name, comboBox3.Font, textBrush, bounds, format); } } }

I've found the solution, Please checkout the changes (Bold Text) i've made private void comboBox3_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { DataRowView drv = (DataRowView)comboBox3.Items[e.Index]; string name = drv["CusName"].ToString(); Font myFont = new System.Drawing.Font("Comic Sans", 11); object comboBoxItem = comboBox3.Items[e.Index]; e.DrawBackground(); e.DrawFocusRectangle(); using (Brush textBrush = new SolidBrush(e.ForeColor)) { Rectangle bounds = e.Bounds; using (StringFormat format = new StringFormat()) { format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Near; e.Graphics.DrawString(name, comboBox3.Font, textBrush, bounds, format); } } }

更多推荐

增加组合框的高度

本文发布于:2023-11-23 03:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   高度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!