Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a ComboBox with DrawMode as OwnerDrawVariable. There are 2 event handlers
C#
this.comboBox3.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.AllcomboBox_DrawItem);
           this.comboBox3.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.AllcomboBox_MeasureItem);



Those are the following

C#
private void AllcomboBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
        {
            ComboBox cBox = (ComboBox)sender;
           //My code...
        }
private void AllcomboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            ComboBox cBox = (ComboBox)sender;
           //My code...

        }


It worked fine for this ComBoBox. When i have another ComBoBox, i added a new event args for that, How can i make a common EventArgs for all the comboboxes in my form.

Please help
Posted
Updated 2-Oct-13 18:56pm
v3

 
Share this answer
 
Comments
id-athul 3-Oct-13 0:49am    
Thanks for the link. Right now my code is as following.
Calling the EventArgs from the ComboBox
this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.AllcomboBox_DrawItem);
this.comboBox2.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.AllcomboBox_MeasureItem);

Now the EventArgs
private void AllcomboBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
}
private void AllcomboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
}
I am adding this is each form's designer.cs. Instead can i make it a single clas?
Hi,

Please check this.

Bolded the changed code lines.

C#
private void AllcomboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            ComboBox cBox = (ComboBox)sender;
            DataRowView drv = (DataRowView)cBox.Items[e.Index];
            string name = drv[0].ToString();
            Font myFont = new System.Drawing.Font("Comic Sans", 11);
            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, cBox.Font, textBrush, bounds, format);
                }
            }
 
        }
 
Share this answer
 
Comments
id-athul 3-Oct-13 1:40am    
Thanks a lot :)

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