textbox on a tabpage vs textbox on a form?

B

BobG

I put some buttons and textboxes on the form, everything seemed to
work. Then I decided I needed two tabs, so I put a tabpage control on
the form, and copied the buttons and textboxes to tabpage1. Now the
buttons dont click. I guess they should be on tabpage1 instead of on
form1? Any way to replace all occurances of Form1_Button1 to
TabPage1_Button1? Is that how to fix this? Thanks!
 
J

Jeff Johnson

Now the buttons dont click.

Is that some code for "the event handlers I assigned to the original buttons
aren't firing when the new buttons are clicked"? Or do you really mean that
when you click the mouse button, the (on-screen) buttons don't show the
standard down/up visual feedback?
 
B

BobG

Is that some code for "the event handlers I assigned to the original buttons
aren't firing when the new buttons are clicked"? Or do you really mean that
when you click the mouse button, the (on-screen) buttons don't show the
standard down/up visual feedback?
===================================================
Hi Jeff. I put a MessageBox.Show in one of the button click handlers,
ran the debugger, clicked the button, no message box. Seems
rearranging all the buttons and textboxes from the original form to
the tabpage1 messed something up.
 
B

BobG

===================================================
Hi Jeff. I put a MessageBox.Show in one of the button click handlers,
ran the debugger, clicked the button, no message box. Seems
rearranging all the buttons and textboxes from the original form to
the tabpage1 messed something up.
===================================================
Turns out if one selects a button, pushes the 'ligtning bolt' to see
the events, and you select 'button1_click' for the event, it magically
starts working as designed. Man, do I have a long learning curve ahead
of me.
 
J

Jeff Johnson

===================================================
Hi Jeff. I put a MessageBox.Show in one of the button click handlers,
ran the debugger, clicked the button, no message box. Seems
rearranging all the buttons and textboxes from the original form to
the tabpage1 messed something up. ===================================================

Turns out if one selects a button, pushes the 'ligtning bolt' to see
the events, and you select 'button1_click' for the event, it magically
starts working as designed. Man, do I have a long learning curve ahead
of me.

I'm pretty sure the problem was in this part of your original post:
Then I decided I needed two tabs, so I put a tabpage control on
the form, and copied the buttons and textboxes to tabpage1.

If you truly COPIED the buttons, then you created completely new buttons.
They would have no connection to the click events that were assigned to the
original buttons, and you would have to, as you discovered, re-wire them to
an event handler. It might be possible to CUT (not copy) the buttons, paste
them on the tab, and have the event wireups follow along. It also might be
possible to drag and drop the buttons. You should experiment. (The reason I
can't say for sure is that I am familiar enough with the back-end guts of
things that I go directly into the form.Designer.cs file and change the code
so that my controls get parented correctly.)
 
T

Tim Sprout

I put some buttons and textboxes on the form, everything seemed to
work. Then I decided I needed two tabs, so I put a tabpage control on
the form, and copied the buttons and textboxes to tabpage1. Now the
buttons dont click. I guess they should be on tabpage1 instead of on
form1? Any way to replace all occurances of Form1_Button1 to
TabPage1_Button1? Is that how to fix this? Thanks!

I am a hobbyist/amateur CSharp programmer.

When I copy buttons, I rename the copied button in the properties window
because the (Name) for copied buttons defaults to "button1" or
"button2", whatever the next number is in sequence. (The button Text
property remains the same as the original button, of course). I then
double click the copied button to generate the event handler method code
into the form code, then I copy and paste the code body from the
original button to the new copied button code body, if i want the same
behavior.

--Tim sprout
 
J

Jeff Johnson

I am a hobbyist/amateur CSharp programmer.

When I copy buttons, I rename the copied button in the properties window
because the (Name) for copied buttons defaults to "button1" or "button2",
whatever the next number is in sequence. (The button Text property remains
the same as the original button, of course). I then double click the
copied button to generate the event handler method code into the form
code, then I copy and paste the code body from the original button to the
new copied button code body, if i want the same behavior.

If you truly want exactly the same behavior, you should put the code into a
standalone method and then call that method from the Click handlers:

private void whateverButton_Click(object sender, EventArgs e)
{
HandleButtonClick(sender);
}

private void someOtherButton_Click(object sender, EventArgs e)
{
HandleButtonClick(sender);
}

private void HandleButtonClick(object sender)
{
// Whatever....
// Notice I didn't pass the EventArgs because that particular class
contains
// no useful information. I might do so if the EventArgs-derived
argument
// class held something I might need.
}
 
T

Tim Sprout

If you truly want exactly the same behavior, you should put the code into a
standalone method and then call that method from the Click handlers:

private void whateverButton_Click(object sender, EventArgs e)
{
HandleButtonClick(sender);
}

private void someOtherButton_Click(object sender, EventArgs e)
{
HandleButtonClick(sender);
}

private void HandleButtonClick(object sender)
{
// Whatever....
// Notice I didn't pass the EventArgs because that particular class
contains
// no useful information. I might do so if the EventArgs-derived
argument
// class held something I might need.
}

Makes sense. Thanks.

--Tim Sprout
 
T

Tobias Müller

Jeff Johnson said:
If you truly want exactly the same behavior, you should put the code into a
standalone method and then call that method from the Click handlers:

private void whateverButton_Click(object sender, EventArgs e)
{
HandleButtonClick(sender);
}

private void someOtherButton_Click(object sender, EventArgs e)
{
HandleButtonClick(sender);
}

private void HandleButtonClick(object sender)
{
// Whatever....
// Notice I didn't pass the EventArgs because that particular class
contains
// no useful information. I might do so if the EventArgs-derived
argument
// class held something I might need.
}

This is definitely not needed and IMO an indirection too much.

The name of an event handler does not have to be objectName_EventName(),
thats just the default if you generate the handler by double clicking the
event in VS designer.

There is absolutely no problem using the same handler for two different
buttons. If you have a button 'whateverButton' with handler
'whateverButton_Click', you can create a second button 'someOtherButton'
and also use 'whateverButton_Click' as handler.

In the 'events' pane (the little lightning bolt) in VS designer you cannot
just double click to create an event handler but also use the drop down
menu to choose an existing one.

Tobi
 
J

Jeff Johnson

Tobias Müller said:
This is definitely not needed and IMO an indirection too much.

The name of an event handler does not have to be objectName_EventName(),
thats just the default if you generate the handler by double clicking the
event in VS designer.

There is absolutely no problem using the same handler for two different
buttons. If you have a button 'whateverButton' with handler
'whateverButton_Click', you can create a second button 'someOtherButton'
and also use 'whateverButton_Click' as handler.

In the 'events' pane (the little lightning bolt) in VS designer you
cannot
just double click to create an event handler but also use the drop down
menu to choose an existing one.

Perhaps I was mixing two threads. I agree that if you have multiple controls
which should react in a similar way to an event then there is no problem
with writing a single event handler and then wiring the controls to that
event handler. (I've done this myself on more than one occasion.)

What I was thinking of was when you also want to call the event handler from
other parts of the code. This is probably where my mind drifted into a
different thread and I posted the "completely separate method" concept. Of
course, if you have even the tiniest bit of variation (like button 1 does x,
y, and z, and button 2 does x, y, z, and w), you would want to consider
whether or not to have the event handler perform that logic or whether to
pull all the common logic into a separate method and then do the extra stuff
in separate, control-specific event handlers.
 

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