Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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" )
Posted
Updated 1-Oct-13 0:04am
v5
Comments
Ron Beyer 1-Oct-13 0:47am    
Combobox.Height = whatever; Can't be that simple though, are you asking the wrong question? Are you asking about the items in the combobox? Are you wanting to dynamically calculate it? What are you really trying to do?
id-athul 1-Oct-13 0:52am    
Thanks, is it like Combobox.Height="30"; can you please tell me the syntax and the place where to add this code
Ron Beyer 1-Oct-13 0:55am    
You put it in the constructor of the code behind, or wherever you need to resize it, it really depends on what trigger (what happens that makes it resize) that tells you where to put it. BTW, you would not use the quotes when doing it, it would be like Combobox.Height = 30; no quotes or you will get a type conversion error.
id-athul 1-Oct-13 0:59am    
I want it in form load(ie whenever you see the combo box)
I used public Form1()
{
InitializeComponent();
comboBox1.Height = 230;
}

But its not working, It works only when the font is increased, But i dont want to increase the font
Ron Beyer 1-Oct-13 1:02am    
You may need to use the Combobox.ItemHeight instead. See the link for some details.

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.
 
Share this answer
 
Comments
id-athul 1-Oct-13 6:06am    
Please see my updated question. also this link
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle.aspx
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);

}
}

}
 
Share this answer
 
v2
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);

}
}

}
 
Share this answer
 
Please try this

changed
C#
object comboBoxItem = comboBox3.Items[e.Index];
e.Graphics.DrawString(comboBoxItem.ToString(), comboBox3.Font, textBrush, bounds);

to
C#
DataRowView comboBoxItem = (DataRowView)comboBox1.Items[e.Index];
e.Graphics.DrawString(comboBoxItem["city_desc"].ToString(), comboBox1.Font, textBrush, bounds);


Designer.cs
C#
private void InitializeComponent()
{
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.citymastBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this._TestDataSet = new WinTest._TestDataSet();
            this.city_mastTableAdapter = new winTest._TestDataSetTableAdapters.city_mastTableAdapter();
            
            this.comboBox1.DataSource = this.citymastBindingSource;
            this.comboBox1.DisplayMember = "city_desc";
            this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.ItemHeight = 25;
            this.comboBox1.Location = new System.Drawing.Point(367, 74);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(171, 31);
            this.comboBox1.TabIndex = 4;
            this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);  
            

            this.citymastBindingSource.DataMember = "city_mast";
            this.citymastBindingSource.DataSource = this._TestDataSet;
            // 
            //_TestDataSet
            // 
            this._TestDataSet.DataSetName = "_TestDataSet";
            this._TestDataSet.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // 
            // city_mastTableAdapter
            // 
            this.city_mastTableAdapter.ClearBeforeFill = true;

}


Code.cs
C#
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Font myFont = new System.Drawing.Font("Comic Sans", 11);
            DataRowView comboBoxItem = (DataRowView)comboBox1.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["city_desc"].ToString(), comboBox1.Font, textBrush, bounds);
                }

            }
        }

    }
 
Share this answer
 
Comments
id-athul 3-Oct-13 0:21am    
Thanks, it worked for me....:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900