How to raise MouseClick event? - c#

Hello
I have read that events can be raised the same way as methods. Well it works for my custom events (I create a delegate, the event and I am able to raise the event by calling it).
However I am not able to manually raise events like MouseClick and other, it keeps saying that it must appear on the left side of the += operator. What is the problem?

While I am certain you'll get other answers more informative than this one, basically you can't "raise" an event outside the class that contains it. MSDN has this to say about events
Events are a special kind of multicast
delegate that can only be invoked from
within the class or struct where they
are declared (the publisher class). If
other classes or structs subscribe to
the event, their event handler methods
will be called when the publisher
class raises the event.
If you wanted to literally raise the event for, say, a Windows Forms Control MouseClick, you'd have to create a subclass of that control and either invoke base.OnMouseClick() or override it.

If this is a button, you can programmatically click it using the PerformClick method.
Sadly, this only works on buttons and not other types of Controls... except MenuItem.

If you want to click button you should call:
button1.PerformClick();
If you want to call MouseClick please refer to this forum, there is solution in c# using windows api:

private void button1_Click(object sender, EventArgs e)
{
//Enter your code here
}
void Page_Load(object sender, EventArgs e){
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1_Click(this, e);
}

Let's say you want to manually raise the event "click". This works for me:
public partial class CustomButton : UserControl
{
public new event EventHandler Click;
private void lblText_Click(object sender, EventArgs e)
{
Click(this, e);
}
}

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.

what means "object sender, RoutedEventArgs e" in c#? [duplicate]

What do sender and eventArgs mean/refer to? How can I make use of them (for the scenario below)?
Scenario:
I'm trying to build a custom control with a delete function, and I want to be able to delete the control that was clicked on a page that contains many of the same custom control.
The sender is the control that the action is for (say OnClick, it's the button).
The EventArgs are arguments that the implementor of this event may find useful. With OnClick it contains nothing good, but in some events, like say in a GridView 'SelectedIndexChanged', it will contain the new index, or some other useful data.
What Chris is saying is you can do this:
protected void someButton_Click (object sender, EventArgs ea)
{
Button someButton = sender as Button;
if(someButton != null)
{
someButton.Text = "I was clicked!";
}
}
sender refers to the object that invoked the event that fired the event handler. This is useful if you have many objects using the same event handler.
EventArgs is something of a dummy base class. In and of itself it's more or less useless, but if you derive from it, you can add whatever data you need to pass to your event handlers.
When you implement your own events, use an EventHandler or EventHandler<T> as their type. This guarantees that you'll have exactly these two parameters for all your events (which is a good thing).
Manually cast the sender to the type of your custom control, and then use it to delete or disable etc. Eg, something like this:
private void myCustomControl_Click(object sender, EventArgs e)
{
((MyCustomControl)sender).DoWhatever();
}
The 'sender' is just the object that was actioned (eg clicked).
The event args is subclassed for more complex controls, eg a treeview, so that you can know more details about the event, eg exactly where they clicked.
'sender' is called object which has some action perform on some
control
'event' its having some information about control which has
some behavoiur and identity perform
by some user.when action will
generate by occuring for event add
it keep within array is called event
agrs
FYI, sender and e are not specific to ASP.NET or to C#. See Events (C# Programming Guide) and Events in Visual Basic.

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).

Can't call event from button

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.

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