Click here to Skip to main content
15,909,898 members
Home / Discussions / C#
   

C#

 
GeneralRe: TypeInitializationException Pin
nasambur2-Feb-07 3:24
nasambur2-Feb-07 3:24 
QuestionListBox close Pin
quiteSmart1-Feb-07 23:47
quiteSmart1-Feb-07 23:47 
AnswerRe: ListBox close Pin
bobsugar2222-Feb-07 0:21
bobsugar2222-Feb-07 0:21 
GeneralRe: ListBox close Pin
quiteSmart2-Feb-07 1:15
quiteSmart2-Feb-07 1:15 
GeneralRe: ListBox close Pin
bobsugar2222-Feb-07 2:55
bobsugar2222-Feb-07 2:55 
AnswerRe: ListBox close Pin
Luc Pattyn2-Feb-07 0:23
sitebuilderLuc Pattyn2-Feb-07 0:23 
GeneralRe: ListBox close Pin
blue_arc2-Feb-07 1:34
blue_arc2-Feb-07 1:34 
AnswerRe: ListBox close Pin
J4amieC2-Feb-07 2:09
J4amieC2-Feb-07 2:09 
Hi qS,

I tried this out for ya, and could easily reproduce the problem you described. Buttons seem ok (they take care of invoking the Leave event on the ListBox), and handling the MouseDown event on the form takes care of hiding it in that instance. Panels cause a problem as they themselves capture the MouseDown event and therefore its not raised on the Form.

As an aside; I was sure there was a way to override this behaviour to tell specific controls to "pass all mouse and key events up the control heirachy. Each level should then be able to do the same thing - handle or pass the event. I cannot find this although im 90% sure it exists.

There is, like always, a sufficient workaround which is to intercept a control being added to the form and handle its MouseDown event also. You can re-use the same event handler as you used for your Form as they have the same signature (and you want the same behaviour - hide the listbox).

I added 1 button and 1 listbox to a form. I thn added the following event handlers:

//Show the listbox
private void button1_Click(object sender, System.EventArgs e)
{
	this.listBox1.Visible = true;
}

// hide the listbox if it looses focus
private void listBox1_Leave(object sender, System.EventArgs e)
{
	this.listBox1.Visible = false;
}

//event handler for Form.MouseDown (and later for each control)
private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
	if(sender != this.listBox1)
		this.listBox1.Visible=false;
}


The last method there has a check that the mouse down event isnt actually on the listbox were using, as you need the mouse down event to set your selection. Then just override OnControlAdded:

protected override void OnControlAdded(ControlEventArgs e)
{
	e.Control.MouseDown += new MouseEventHandler(Control_MouseDown);
	base.OnControlAdded (e);
}


Nice!


GeneralRe: ListBox close Pin
quiteSmart2-Feb-07 2:17
quiteSmart2-Feb-07 2:17 
QuestionSystem.Text.Encoding.Unicode.GetString() does not properly works in Vista... please help??? Pin
Waqas Nasir1-Feb-07 23:42
Waqas Nasir1-Feb-07 23:42 
QuestionResponse.redirect Pin
acodman1-Feb-07 23:22
acodman1-Feb-07 23:22 
AnswerRe: Response.redirect Pin
Christian Graus1-Feb-07 23:48
protectorChristian Graus1-Feb-07 23:48 
GeneralRe: Response.redirect Pin
acodman2-Feb-07 0:36
acodman2-Feb-07 0:36 
GeneralRe: Response.redirect Pin
Parwej Ahamad2-Feb-07 0:41
professionalParwej Ahamad2-Feb-07 0:41 
AnswerRe: Response.redirect Pin
Parwej Ahamad2-Feb-07 0:32
professionalParwej Ahamad2-Feb-07 0:32 
AnswerRe: Response.redirect Pin
Ajay R Ojha2-Feb-07 0:40
Ajay R Ojha2-Feb-07 0:40 
GeneralRe: Response.redirect Pin
blue_arc2-Feb-07 1:18
blue_arc2-Feb-07 1:18 
GeneralRe: Response.redirect Pin
acodman2-Feb-07 2:06
acodman2-Feb-07 2:06 
GeneralRe: Response.redirect Pin
acodman2-Feb-07 2:11
acodman2-Feb-07 2:11 
QuestionEncryption and decryption Pin
sjs4u1-Feb-07 23:13
sjs4u1-Feb-07 23:13 
AnswerRe: Encryption and decryption Pin
S. Senthil Kumar2-Feb-07 5:59
S. Senthil Kumar2-Feb-07 5:59 
QuestionHow to convert ascii to char Pin
vaibhavnvag1-Feb-07 22:47
vaibhavnvag1-Feb-07 22:47 
AnswerRe: How to convert ascii to char Pin
Parwej Ahamad1-Feb-07 22:56
professionalParwej Ahamad1-Feb-07 22:56 
GeneralRe: How to convert ascii to char Pin
aSarafian2-Feb-07 2:22
aSarafian2-Feb-07 2:22 
Questionbindinglist Problem Pin
mark_w_1-Feb-07 22:45
mark_w_1-Feb-07 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.