增加组合框的高度 [英] Increasing the height of a combobox
问题描述
我必须增加窗体组合框的高度。我怎样才能轻松实现它。此外,我不能增加我的字体大小。只有控件必须增加
提前致谢。
更新部分
我已经完成了但有问题。请使用 System.Windows.Forms查看我的代码
;
使用 System.Drawing;
命名空间 WindowsFormsApplication1
{
partial class Form1
{
/// < 摘要 >
/// 必需的设计变量。
/// < / summary >
private System.ComponentModel.IContainer components = null ;
/// < 摘要 >
/// 清理正在使用的所有资源。
/// < / summary >
/// < param < span class =code-summarycomment> name =disposing > 如果管理资源应予以处理;否则,false。< / param >
protected 覆盖 void Dispose( bool 处理)
{
if (处置&&(components!= null ))
{
components.Dispose();
}
base .Dispose(disposing);
}
#region Windows窗体设计器生成的代码
/// < 摘要 >
/// Designer支持所需的方法 - 不要修改
/// 使用代码编辑器的方法的内容。
/// < / summary >
///
private < span class =code-keyword> void InitializeComponent()
{
this .components = new System.ComponentModel.Container();
this .customerBindingSource = new System.Windows.Forms.BindingSource(此跨度> .components);
this .invoiceDataSet = new WindowsFormsApplication1.InvoiceDataSet();
this .customerTableAdapter = new WindowsFormsApplication1.InvoiceDataSetTableAdapters.CustomerTableAdapter();
this .comboBox3 = 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 .comboBox3.DataSource = this .customerBindingSource;
this .comboBox3.DisplayMember = CusName 跨度>;
this .comboBox3.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
此 .comboBox3.DropDownWidth = 250 ;
此 .comboBox3.FormattingEnabled = true ;
this .comboBox3.ItemHeight = 55 ;
this .comboBox3.Location = new System.Drawing.Point( 193 , 71 );
this .comboBox3.Name = comboBox3 跨度>;
this .comboBox3.Size = new System.Drawing.Size( 58 , 61 );
this .comboBox3.TabIndex = 2 ;
this .comboBox3.ValueMember = CusId 跨度>;
this .comboBox3.DrawItem + = new System.Windows.Forms.DrawItemEventHandler(此跨度> .comboBox3_DrawItem);
this .comboBox3.MeasureItem + = new System.Windows.Forms.MeasureItemEventHandler(此跨度> .comboBox3_MeasureItem);
//
// Form1
//
this .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this .ClientSize = new System.Drawing.Size( 482 , 270 );
this .Controls.Add( this .comboBox3);
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 )
< blockquote>你不能 - 组合框始终自动调整大小以匹配字体,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()
{
this.components = new System.ComponentModel.Container();
this.customerBindingSource = new System.Windows.Forms.BindingSource(this.components);
this.invoiceDataSet = new WindowsFormsApplication1.InvoiceDataSet();
this.customerTableAdapter = new WindowsFormsApplication1.InvoiceDataSetTableAdapters.CustomerTableAdapter();
this.comboBox3 = 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
//
this.comboBox3.DataSource = this.customerBindingSource;
this.comboBox3.DisplayMember = "CusName";
this.comboBox3.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.comboBox3.DropDownWidth = 250;
this.comboBox3.FormattingEnabled = true;
this.comboBox3.ItemHeight = 55;
this.comboBox3.Location = new System.Drawing.Point(193, 71);
this.comboBox3.Name = "comboBox3";
this.comboBox3.Size = new System.Drawing.Size(58, 61);
this.comboBox3.TabIndex = 2;
this.comboBox3.ValueMember = "CusId";
this.comboBox3.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox3_DrawItem);
this.comboBox3.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox3_MeasureItem);
//
// Form1
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
this.ClientSize = new System.Drawing.Size(482, 270);
this.Controls.Add(this.comboBox3);
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);
}
}
}
这篇关于增加组合框的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!