Problem with eventhandlers (!?)

R

Raj Chudasama

I have a client server app. On the client i have buttons (derived from user
contol) that let user perform actions when they are connected to the server.
so when they connect (using a menuitem) i add event handlers to the buttons
and when they disconnect i remove them. Now, everything works fine when
they connnect for the first time. All of the msgs are sent propoerly by the
client. To figure out what button is click by the client i used the "Tag"
property rather than having a on click event for each of the buttons ( see
click event code).

However, if the following scenario occurs it goes through both msgs(like
kind of having two event handlers for button click being executed)
connect -> disconnect->connect.
can someone please tell me what could be worng? only think i can think
about is something to do with event handlers thats it :(
------------------------------------------
i am only adding handlers once with every connect msg as follows:
private void SetupFunctionButtons()
{
foreach(WACButton btn in Names)
{
btn.MouseHover +=new EventHandler(btn_MouseHover);
btn.MouseLeave +=new EventHandler(btn_MouseLeave);
btn.MouseEnter +=new EventHandler(btn_MouseEnter);
btn.MouseUp +=new MouseEventHandler(btn_MouseUp);
btn.MouseDown +=new MouseEventHandler(btn_MouseDown);
btn.Click +=new EventHandler(btn_Click);
}
btnEmpty1.ImageIndex = 1;
btnEmpty2.ImageIndex = 1;
}
------------------------------------------
I know i am removing all handlers every time they disconnect (b/c u can t
click or do anyting with the button)

private void RemoveFunctionButtons()
{
foreach(WACButton btn in Names)
{
btn.MouseHover -=new EventHandler(btn_MouseHover);
btn.MouseLeave -=new EventHandler(btn_MouseLeave);
btn.MouseEnter -=new EventHandler(btn_MouseEnter);
btn.MouseUp -=new MouseEventHandler(btn_MouseUp);
btn.MouseDown -=new MouseEventHandler(btn_MouseDown);
btn.Click -=new EventHandler(btn_Click);
}
}

------------------------------------------

private void btn_Click(object sender, EventArgs e)
{
Carets.HideCaret(this.Handle);
WACButton btn = (WACButton)sender;
btn.ImageIndex = 3;
Thread.Sleep(200);
string tag = ((WACButton)sender).Tag.ToString();
try
{
switch(tag)
{
case "dnd":
{
s.IRCConnection.SendMessage(string.Format("PRIVMSG {0} :DNDON
UN|{1}^",mIRCRoom,mMyInfo.Name));
btn.ButtonText = "Disable DND";
btn.Tag = "disablednd";
break;
}
case "disablednd":
{
s.IRCConnection.SendMessage(string.Format("PRIVMSG {0} :DNDOFF
UN|{1}^",mIRCRoom,mMyInfo.Name));
btn.ButtonText = "DND";
btn.Tag = "dnd";
break;
}

}//end case//
}
catch
{
return;
}
Carets.ShowCaret(this.Handle);
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It's not clear to me why you add/remove the handlers, I would instead use a
flag to indicate if they should react to the events or not. In your case
even the Enabled/ property can be used.

If that is not enough, then create ONLY ONE isntance of each event and just
assign it as needed:

EventHandler m_mouseHover;

mouseHover = new EventHandler(btn_MouseHover);

// to hook
btn.MouseHover += mouseHover ;
// to unhook
btn.MouseHover -= mouseHover ;


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
R

Raj Chudasama

Hi, thanks for reply

thants what i am doing, i have one instance of each event. All buttons get
these events assigned to them. so use switch statement to figure out which
button was clicked. It works well, except the problem i stated. They are
basically disabled when they are not connected to the server, since there
are no event handlers assigned to them..

i will try some sort of flag.

thanks
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

They are not disabled, just they do nothing, but it may annoy the user, just
disable them.

and use one eventhandler, do not create them without reason.

cheers,
 
R

Raj Chudasama

HI tanks again

those event handlers sort of give it some nice looks. For each event
(mouseevent) i change buttons so it sort has that look of macro media flash
app.

i still can t seem to figure out why it executes click event twice.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Even so, you can create the delegates only once, and regarding the
buttons( or menuitems ) do not create them each time you change the look,
create them once and just keep them in an array.

Create the minimun qty of instances needed, this way you will consume less
memory


cheers,
 

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