ComboBox Question

G

Guest

Here is my goal:
I want a combo box that has a font size of 8.25pt
When the user clicks the dropdown I want the size to change to 15pt
Simple I thought.

So I Created the DropDown Event to handle the font size to change to 15pt.
Then I Created the SelecttionChangeCommitted Event to handle the font size
to change back to 8.25pt.

The problem that I have is if the user clicks on the combobox, the dropdown
event is fired and the font changes to 15pt. But if the user clicks on the
combo box again (or the form or another control) the dropdown hides but the
font doesn't change because the value for the control never changed. Now I
can handle the LostFocus event to monitor if they click on another control.
But if they click on the combo box or the form what event gets fired? I'm
looking for a dropdownhide event or something. (Note: onClick event only
fires when dropdown is dropped.)

Bellow is sample code. When you run it click on the combo box, then click
either on the combo box again (without selecting somthing) or on the form.

**********************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.Items.AddRange(new object[] {
"Test1",
"Test2",
"Test3",
"Test4",
"Test5",
"Test6"});
this.comboBox1.Location = new System.Drawing.Point(52, 48);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.DropDown += new
System.EventHandler(this.comboBox1_DropDown);
this.comboBox1.SelectionChangeCommitted += new
System.EventHandler(this.comboBox1_SelectionChangeCommitted);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void comboBox1_DropDown(object sender, System.EventArgs e)
{
Font newFont = new Font(comboBox1.Font.FontFamily.Name,15F);
comboBox1.Font = newFont;
}

private void comboBox1_SelectionChangeCommitted(object sender,
System.EventArgs e)
{
Font newFont = new Font(comboBox1.Font.FontFamily.Name,8.25F);
comboBox1.Font = newFont;
}
}
}
************************************************************

Thanks
KeithH
 
R

RBischoff

Hello KeithH,
What version of .net framework? (I am using v1.1) I found that your example code works fine *if* you only click on the "down arrow" of the cbo. The only time I found the bug , is if I click on the "text" portion of the cbo twice.

Are your results consistent with mine?

Best of luck!

Your C# ally ,
RBischoff


K> Here is my goal:
K> I want a combo box that has a font size of 8.25pt
K> When the user clicks the dropdown I want the size to change to 15pt
K> Simple I thought.
K> So I Created the DropDown Event to handle the font size to change to
K> 15pt. Then I Created the SelecttionChangeCommitted Event to handle
K> the font size to change back to 8.25pt.
K>
K> The problem that I have is if the user clicks on the combobox, the
K> dropdown event is fired and the font changes to 15pt. But if the
K> user clicks on the combo box again (or the form or another control)
K> the dropdown hides but the font doesn't change because the value for
K> the control never changed. Now I can handle the LostFocus event to
K> monitor if they click on another control. But if they click on the
K> combo box or the form what event gets fired? I'm looking for a
K> dropdownhide event or something. (Note: onClick event only fires
K> when dropdown is dropped.)
K>
K> Bellow is sample code. When you run it click on the combo box, then
K> click either on the combo box again (without selecting somthing) or
K> on the form.
K>
K> **********************************************************
K> using System;
K> using System.Drawing;
K> using System.Collections;
K> using System.ComponentModel;
K> using System.Windows.Forms;
K> using System.Data;
K> namespace Test
K> {
K> /// <summary>
K> /// Summary description for Form1.
K> /// </summary>
K> public class Form1 : System.Windows.Forms.Form
K> {
K> private System.Windows.Forms.ComboBox comboBox1;
K> /// <summary>
K> /// Required designer variable.
K> /// </summary>
K> private System.ComponentModel.Container components = null;
K> public Form1()
K> {
K> //
K> // Required for Windows Form Designer support
K> //
K> InitializeComponent();
K> //
K> // TODO: Add any constructor code after InitializeComponent call
K> //
K> }
K> /// <summary>
K> /// Clean up any resources being used.
K> /// </summary>
K> protected override void Dispose( bool disposing )
K> {
K> if( disposing )
K> {
K> if (components != null)
K> {
K> components.Dispose();
K> }
K> }
K> base.Dispose( disposing );
K> }
K> #region Windows Form Designer generated code
K> /// <summary>
K> /// Required method for Designer support - do not modify
K> /// the contents of this method with the code editor.
K> /// </summary>
K> private void InitializeComponent()
K> {
K> this.comboBox1 = new System.Windows.Forms.ComboBox();
K> this.SuspendLayout();
K> //
K> // comboBox1
K> //
K> this.comboBox1.DropDownStyle =
K> System.Windows.Forms.ComboBoxStyle.DropDownList;
K> this.comboBox1.Items.AddRange(new object[] {
K> "Test1",
K> "Test2",
K> "Test3",
K> "Test4",
K> "Test5",
K> "Test6"});
K> this.comboBox1.Location = new System.Drawing.Point(52, 48);
K> this.comboBox1.Name = "comboBox1";
K> this.comboBox1.Size = new System.Drawing.Size(121, 21);
K> this.comboBox1.TabIndex = 0;
K> this.comboBox1.DropDown += new
K> System.EventHandler(this.comboBox1_DropDown);
K> this.comboBox1.SelectionChangeCommitted += new
K> System.EventHandler(this.comboBox1_SelectionChangeCommitted);
K> //
K> // Form1
K> //
K> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
K> this.ClientSize = new System.Drawing.Size(292, 266);
K> this.Controls.Add(this.comboBox1);
K> this.Name = "Form1";
K> this.Text = "Form1";
K> this.ResumeLayout(false);
K> }
K> #endregion
K> /// <summary>
K> /// The main entry point for the application.
K> /// </summary>
K> [STAThread]
K> static void Main()
K> {
K> Application.Run(new Form1());
K> }
K> private void comboBox1_DropDown(object sender, System.EventArgs e)
K> {
K> Font newFont = new Font(comboBox1.Font.FontFamily.Name,15F);
K> comboBox1.Font = newFont;
K> }
K> private void comboBox1_SelectionChangeCommitted(object sender,
K> System.EventArgs e)
K> {
K> Font newFont = new Font(comboBox1.Font.FontFamily.Name,8.25F);
K> comboBox1.Font = newFont;
K> }
K> }
K> }
K> ************************************************************
K>
K> Thanks
K> KeithH
 
G

Guest

I don't get that behavior. Clicking the test or the arrow does the same thing
(doesn't change the size back to small).
But I'm using the latest Service Pack (sp2?)

But still, there is a way even for you to get the 'bug'. So there has to be
an event that will help me out.

Thanks
KeithH

RBischoff said:
Hello KeithH,
What version of .net framework? (I am using v1.1) I found that your example code works fine *if* you only click on the "down arrow" of the cbo. The only time I found the bug , is if I click on the "text" portion of the cbo twice.

Are your results consistent with mine?

Best of luck!

Your C# ally ,
RBischoff


K> Here is my goal:
K> I want a combo box that has a font size of 8.25pt
K> When the user clicks the dropdown I want the size to change to 15pt
K> Simple I thought.
K> So I Created the DropDown Event to handle the font size to change to
K> 15pt. Then I Created the SelecttionChangeCommitted Event to handle
K> the font size to change back to 8.25pt.
K>
K> The problem that I have is if the user clicks on the combobox, the
K> dropdown event is fired and the font changes to 15pt. But if the
K> user clicks on the combo box again (or the form or another control)
K> the dropdown hides but the font doesn't change because the value for
K> the control never changed. Now I can handle the LostFocus event to
K> monitor if they click on another control. But if they click on the
K> combo box or the form what event gets fired? I'm looking for a
K> dropdownhide event or something. (Note: onClick event only fires
K> when dropdown is dropped.)
K>
K> Bellow is sample code. When you run it click on the combo box, then
K> click either on the combo box again (without selecting somthing) or
K> on the form.
K>
K> **********************************************************
K> using System;
K> using System.Drawing;
K> using System.Collections;
K> using System.ComponentModel;
K> using System.Windows.Forms;
K> using System.Data;
K> namespace Test
K> {
K> /// <summary>
K> /// Summary description for Form1.
K> /// </summary>
K> public class Form1 : System.Windows.Forms.Form
K> {
K> private System.Windows.Forms.ComboBox comboBox1;
K> /// <summary>
K> /// Required designer variable.
K> /// </summary>
K> private System.ComponentModel.Container components = null;
K> public Form1()
K> {
K> //
K> // Required for Windows Form Designer support
K> //
K> InitializeComponent();
K> //
K> // TODO: Add any constructor code after InitializeComponent call
K> //
K> }
K> /// <summary>
K> /// Clean up any resources being used.
K> /// </summary>
K> protected override void Dispose( bool disposing )
K> {
K> if( disposing )
K> {
K> if (components != null)
K> {
K> components.Dispose();
K> }
K> }
K> base.Dispose( disposing );
K> }
K> #region Windows Form Designer generated code
K> /// <summary>
K> /// Required method for Designer support - do not modify
K> /// the contents of this method with the code editor.
K> /// </summary>
K> private void InitializeComponent()
K> {
K> this.comboBox1 = new System.Windows.Forms.ComboBox();
K> this.SuspendLayout();
K> //
K> // comboBox1
K> //
K> this.comboBox1.DropDownStyle =
K> System.Windows.Forms.ComboBoxStyle.DropDownList;
K> this.comboBox1.Items.AddRange(new object[] {
K> "Test1",
K> "Test2",
K> "Test3",
K> "Test4",
K> "Test5",
K> "Test6"});
K> this.comboBox1.Location = new System.Drawing.Point(52, 48);
K> this.comboBox1.Name = "comboBox1";
K> this.comboBox1.Size = new System.Drawing.Size(121, 21);
K> this.comboBox1.TabIndex = 0;
K> this.comboBox1.DropDown += new
K> System.EventHandler(this.comboBox1_DropDown);
K> this.comboBox1.SelectionChangeCommitted += new
K> System.EventHandler(this.comboBox1_SelectionChangeCommitted);
K> //
K> // Form1
K> //
K> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
K> this.ClientSize = new System.Drawing.Size(292, 266);
K> this.Controls.Add(this.comboBox1);
K> this.Name = "Form1";
K> this.Text = "Form1";
K> this.ResumeLayout(false);
K> }
K> #endregion
K> /// <summary>
K> /// The main entry point for the application.
K> /// </summary>
K> [STAThread]
K> static void Main()
K> {
K> Application.Run(new Form1());
K> }
K> private void comboBox1_DropDown(object sender, System.EventArgs e)
K> {
K> Font newFont = new Font(comboBox1.Font.FontFamily.Name,15F);
K> comboBox1.Font = newFont;
K> }
K> private void comboBox1_SelectionChangeCommitted(object sender,
K> System.EventArgs e)
K> {
K> Font newFont = new Font(comboBox1.Font.FontFamily.Name,8.25F);
K> comboBox1.Font = newFont;
K> }
K> }
K> }
K> ************************************************************
K>
K> Thanks
K> KeithH
 
J

Jeffrey Tan[MSFT]

Hi KeithH,

Based on my understanding, you want to be notified when the dropdownlist of
combobox is closed up.

In .Net, there is no build-in support for this. But in win32, there will be
a CBN_CLOSEUP Notification sent to the combobox when the dropdownlist is
closed up. This CBN_CLOSEUP Notification is sent with WM_COMMAND message.
But in .Net, WM_COMMAND message is mapping to OCM_COMMAND message for
combobox. So we should inherit from the combobox, then override WndProc
method, and intercept OCM_COMMAND with CBN_CLOSEUP notification. Sample
code lists below:

private const uint CBN_CLOSEUP=0x8;
private const uint WM_COMMAND=0x0111;
private const uint WM_USER = 0x400;
private const uint OCM__BASE= WM_USER + 0x1C00;
private const uint OCM_COMMAND= OCM__BASE + WM_COMMAND;

protected override void WndProc(ref Message m)
{
if(m.Msg==OCM_COMMAND)
{
if((((uint)m.WParam&0xFFFF0000)>>16)==CBN_CLOSEUP)
{
Font newFont = new Font(this.Font.FontFamily.Name,8.25F);
this.Font = newFont;
}
}
base.WndProc (ref m);
}
It works well on my side.
=====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top