Can't call event from button - c#

I have three different events:
form_load
button_click
pnlTiles_Paint
My button click event I have:
private void btnUpdate_Click(object sender, EventArgs e)
{
pnlTiles.Paint += pnlTiles_Paint;
}
My form_load event I have:
private void frmMain_Load(object sender, EventArgs e)
{
pnlTiles.Paint += pnlTiles_Paint;
}
Now my problem is that the event gets called when I use it in form_load but when I use it in the button event; it just skips over the event, I tried to step into the event when debugging the button click. But I made no progress on trying to figure out why the event doesn't get called from the button.

As others have pointed out, pnlTiles.Paint += pnlTiles_Paint; means you are subscribing to the Paint event on the control pnlTiles. What this means is that every time pnlTiles raises the Paint event, the method specified by pnlTiles_Paint will get called. What causes Paint to get fired? According to msdn,
The Paint event is raised when the control is redrawn. It passes an instance of PaintEventArgs to the method(s) that handles the Paint event.
So what's probably happening is that pnlTiles raises the Paint event AFTER the main form raises the Load event (since this is the first time pnlTiles is getting drawn on the screen) which is why your handler ends up getting called.
However, clicking on a button does not cause pnlTiles to get redrawn and the Paint event to get raised, hence your handler does not get called.
If for some reason, after you have hooked up your handler in the Button's click handler, pnlTiles needs to be redrawn, pnlTiles_Paint will get called. You can test this by calling pnlTiles.Invalidate() in the Button's click handler after subscribing to pnlTile's Paint event.
private void btnUpdate_Click(object sender, EventArgs e)
{
pnlTiles.Paint += pnlTiles_Paint;
pnlTiles.Invalidate();
}
Since you're a little fuzzy on the basics of events, it might be worth reading these articles:
Jon Skeet - Events
MSDN - Events.
Also, make sure to read cgijbels comment on this post about not subscribing to the same event multiple times.

Related

Excel add-in ribbon click events bubbling

I am following this walkthrough on MSDN: Creating a Custom Tab by Using the Ribbon Designer
Looking at steps 3 and 4:
In step 3 it adds an event handler to the ribbon_Load function, basically adding a click event to a button in the ribbon:
private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
{
this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
}
Then, in step 4 they add another event handler in the way that I am more used to, like so:
private void button1_Click(object sender, RibbonControlEventArgs e)
{
MergeReportInterface ui = new MergeReportInterface();
ui.ShowDialog();
}
I am not really understanding the purpose of this, because all it does is cause the event to fire twice. If I comment out the event handler that was added to the load function the event occurs once.
Could someone please explain to me what the point of this is? if there is any, or if there is some error on the MSDN site. What should be the proper way to handle a ribbon click event?
private void button1_Click(object sender, RibbonControlEventArgs e)
{
MergeReportInterface ui = new MergeReportInterface();
ui.ShowDialog();
}
This is not adding an event handler. This is the method that your event will call.
this.button1.Click += new RibbonControlEventHandler(this.button1_Click);
This is saying 'When button1 fires its Click event, call this.button1_Click'.
Your code only sets up one event handler, it should only fire once.
However, it's likely you created the button1_Click method by double clicking a button on your form designer. This, behind the scenes, adds an additional event handler. This is why you're getting the event fired twice.
So you have two options:
Go back into the IDE and remove the click handler via your form designer. Go to your code and manually write the method button1_Click.
OR
Remove this line: this.button1.Click += new RibbonControlEventHandler(this.button1_Click);, as VisualStudio is doing that for you automatically.

Button clicked event not triggering after lost focus event

I have a Winforms c# form with some comboBoxes , cancel and save buttons that work fine.
I now need to capture when the user has finished entering text into a comboBox.
I add an empty ( for now) lostFocus (or Leave) event to the combbox , which triggers fine. However if the cause of that event was a cancel or save button press , the corresponding event is no longer triggered. These buttons still work fine if pressed at other times.
Should these two event be firing in sequence or is there some better way to capture completed text entry?
The Leave and/or LoseFocus events do not get triggered because you do not leave the combobox and because it doesn't lose focus when you press Enter or Escape.
Therefore the best way is to add the function you are triggering in the LoseFocus event, also to the Button click events of the Cancel- and the Accept-Buttons.
Adding a call to the leave event itself: comboBox1.Leave(null, null); would be the simplest way.
To make sure that the function is called only once, I check who has focus in the ButtonClick events:
private void acceptButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(acceptButton, null);
// do accept stuff here..
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (comboBox1.ContainsFocus) comboBox1_Leave(cancelButton, null);
// do cancel stuff here..
}
private void comboBox1_Leave(object sender, EventArgs e)
{
// do leave stuff here..
Console.WriteLine(sender);
}
I also pass in the Button so you could check the sender to see how the Leave was triggered..
I'm answering my own question here as I feel it might be useful to other newbies.
The breakpoint I had set in my empty lostFocus event was stopping button click event from occurring. When I removed the breakpoint the problem went away.
However when I added code to my lostFocus event, a form redraw was sometimes moving the buttons and preventing their events from firing. To solve this problem I adapted TaWs very useful answer and fired the button event from within the lostFocus event.
private void comboBox1_LostFocus(object sender, EventArgs e)
{
bool saving = btnSave.ContainsFocus;
// form redraw stuff here..
if (saving)
btnSave_Click(btnSave, null);
}

Fire an event within another event

I need help on firing an event within C#
Basically I have a onclick event that fires when you click on a checkbox
void OnClick(object sender, RoutedEventArgs e)
{
...
}
I need help on firing an event within C#
Basically I have a onclick event that fires when you click on a checkbox
void OnClick(object sender, RoutedEventArgs e)
{
...
}
However, I need to fire this event once another event has been fired, so within this new event, is it possible I can fire the above one?
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
switch(dataGrid.Name)
{
case "Customer"
//fire OnCLick Event
break;
}
}
I have tried something like
??? += new MouseEventHandler(OnClick);
But I am not sure if this will actually work.
Yes you can, but only if the event is in your own class. You can't even raise a base class' event. You have a put a method in the base class to raise the event, and then call that.
The code you put there is adding another event handler, not raising an event; you don't need to do that.
If it's a button, use btnDoSomething.PerformClickEvent (winforms)
If the handler is in your code, you can call it without raising the event (commenters assume that this is what you want to do but in reaslity there are many cases where you'd need more than this) btnDoSomething_Click(null, null) - null usually works because handler code rarely cares about the sender or arguments and if you don't reference them, you don't need them.
If you can use #4, you can also refactor as mentioned. Usually not needed. But usually so easy to do you it's worth doing for clarity anyway.
For objects that map from Windows widgets of anysort, check out the SendMessage and PostMessage API calls. Wayyyy beyond the scope of this answer, though. Doesn't apply to non-windows-backed objects (but your sample implies windows).

Handling LinkClicked event for the richtextbox; the code runs twice inside the event handler

I've a windows form with richtextbox in it.There is a link which when clicked triggers the LinkClicked Event handler which is something like,
private void textSampleResults_LinkClicked(object sender, LinkClickedEventArgs e)
{
CallSomeMethod1;
CallSomeMethod2;
}
When I started debugging, I was expecting the code inside the event handler to run once only but it started Calling CalleSomethod1 and CallSomeMethod2 again.In summary, for every click, the code under the event handler is executed twice. Is it something expected? How to make it behave so that the code under the event handler gets executed only once?

How to raise list controls SelectedIndexChanged event in codebehind?

How to raise the SelectedIndexChanged event of an asp.net List control in a codebehind using C#?
If you're asking how to manually fire the event so that it can run whatever logic is attached: don't.
Your event handlers should be slim. If you need to perform the same operation from multiple places, then extract that functionality into its own method and have the event handler invoke that. For example:
private void CountryListBox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateStates(ListBox1.SelectedItem.Text);
}
private void UpdateStates(string country)
{
StateListBox.DataSource = GetStates(country);
StateListBox.DataBind();
}
Now instead of trying to fire the SelectedIndexChanged event, you just invoke the method that this event handler refers to, i.e.
private void Page_Load(object sender, EventArgs e)
{
UpdateStates("USA");
}
Don't put complex logic in event handlers and try to raise those events from unexpected places. Instead, put the complex logic in its own method, so that you can perform the associated actions from elsewhere.
It is raised automatically.
Go in the Events section, lightening
bolt in properties window
alt text http://img704.imageshack.us/img704/6100/listbox.jpg
double click the place holder next to
event. This is what you will get.
protected void ListBox1_SelectedIndexChanged(object
sender, EventArgs e)
{
}
if you want to raise this event from another code block then, call
ListBox1_SelectedIndexChanged(sender,
e);
If what you want is more than just executing the code behaviour coded for the selected index (like listed in the previous answer), the short answer is there is no easy way. You can write a simple code that on prerender or render to explicitly define the control id variable in your rendered HTML and then use javascript to set the selected index. This will cause the postback that trigger the event. Alternatively you can register an ajax call back method and have the client calls that either when some event happened or by automatic timer.

Categories

Resources