Custom Component On Click Event - c#

I've built a custom component that basically has a picture box and label in it. In the parent form, I want to be able to detect when its been clicked on. The standard .click event doesn't seem to be working, but I've never used events before so am unsure if I'm using them correctly. Heres the code I'm using (in the parent) to try and make it recognise the click:
Item aItem = new Item();
aItem.Icon = ItemImage;
aItem.Title = Title;
aItem.Click += new EventHandler(ItemClicked);
aItem.Filename = File;
and heres the method its calling:
public void ItemClicked(Object sender, EventArgs e)
{
MessageBox.Show("Item Clicked!");
}
This code never fires. Do I need to put anything into the component or am I just doing this wrong?
Cheers

Right I finally worked it out. Tejs response just confused me more so here's what I did.
In my UserControl I had the following event:
public event EventHandler Clicked;
Then I had an event for when the image was clicked (still in the UserControl) and I just called the Clicked event:
private void imgItem_Click(object sender, EventArgs e)
{
Clicked(this, e);
}
Then in my parent form, when I created the object, I had the following:
Item aItem = new Item();
aItem.Clicked += new EventHandler(ItemClicked);
void ItemClicked(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}

You would do this by exposing an event':
Your custom component:
// A custom delegate like MyItemClickedHandler, or you could make a Func<> or Action<>
public event MyItemClickedHandler ItemClickedEvent;
public void ItemClicked(object sender, EventArgs e)
{
if(ItemClickedEvent != null)
ItemClickedEvent(); // Your delegate could pass parameters if needed
}
Then your parent form simply observes the event:
myCustomControl.ItemClickedEvent += new MyItemClickedHandler(SomeMethod);
Then, whenever the event is raised on your custom control, the parent is notified because it subscribed the event.

Related

How can I use mousemove event in c#

The question is this:
when the mouse cursor moved on the button some thing should be happen but I don't know what exactly have to write
When you select the button in the VS-designer you will have access to the properties and events (lightning Icon in the property window).
In the events-listing are all events that the button can fire. May be for your purpose the events: ´MouseEnter´ and ´MouseLeave´ would be a good choice. Just double click the event and Visual Studio will generate the appropriate method. Like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Green;
}
In my example I just change the backcolour of the button when the mouse is on the button and change it again when it leaves the button.
Practically you could run any code inside the generated method.
You can create eventHandler like this :
myButton.MouseMove += new MouseEventHandler(doSomething);
Where myButton is the button from which you want to trigger the event when mouse moves over it. and doSomething() is the method defined as like the following:
public void doSomething(object sender, MouseEventArgs e)
{
// do what ever you want
}

How to invoke event handler mentod added to control? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How can I invoke an event handler from a function?
I need to do something I was pretty sure should be simple, I have Form with controls and every control have event handler that sets label with correct formating etc.
What I need is to call every handlers collection for every control on specific type.
I browsed all stackoverflow and never saw result.
It is simple application so please stop comments like: "you need additional function called from event handler".
This is what I tried
foreach (Control ctrl in Controls)
{
if (ctrl is TrackBar)
{
TrackBar tb = ctrl as TrackBar;
Invoke(tb.Scroll, tb, new EventArgs());
}
}
But I see very strange error
The event 'System.Windows.Forms.TrackBar.Scroll' can only appear on the left hand side of += or -=
Any solution?
You can't raise event outside class in which event is declared (only adding and removing handlers is available). Instead of trying to raise event, which will call event handler, simply extract handler logic to separate method, and call that method:
foreach (TrackBar tb in Controls.OfType<TrackBar>())
{
DoSomething(tb.Value);
}
And handler:
void trackBar_Scroll(object sender, EventArgs e)
{
DoSomething(((TrackBar)sender).Value);
}
Error appears because tb.Scroll is event not delegate.
You could try making a delegate with the same Signature as the Event, create a common eventhandler for your controls and then you can either invoke it or respond to the event as normal.
i.e.
Something like this:
public partial class Form1 : Form
{
public delegate void trackbarscroll( object sender, EventArgs e);
trackbarscroll tbs;
public Form1()
{
InitializeComponent();
trackBar1.Scroll += new EventHandler(trackBar_Scroll);
trackBar2.Scroll += new EventHandler(trackBar_Scroll);
tbs = trackBar_Scroll;
}
void trackBar_Scroll(object sender, EventArgs e)
{
TrackBar tb = (TrackBar)sender;
}
private void button1_Click(object sender, EventArgs e)
{
foreach (Control ctrl in Controls)
{
if (ctrl is TrackBar)
{
TrackBar tb = ctrl as TrackBar;
Invoke(tbs,tb, new EventArgs());
}
}
}
}

How to connect text change event in multiple text boxes to a single method (C#)?

When I double click on my text boxes in the designed, it creates a method auto-magically for me. Since I wish the same things to occur in any of the cases, I simply call an auxiliary method from each, like in the code below.
private void TextBox_1_TextChanged(object sender, EventArgs e)
{
TextChanged();
}
private void TextBox_2_TextChanged(object sender, EventArgs e)
{
TextChanged();
}
private void TextChanged(object sender, EventArgs e) { ... }
Now I'd like to know if there's a way (other than going into my design file (which, according to the information in it, shouldn't be attempted to) to connect the actions listeners to the same method and skip the detour via the automatically generated ones.
On the designer page go to the events tab, find the event you are looking for (TextChanged) and manually enter the name of the event handler you wish them all to use.
I usually proceed like this in my projects, if controls are not going to change at runtime (i.e. if all controls in the form are added at design time):
// this is the container's ctor
public MyForm()
{
TextBox1.TextChanged += new EventHandler(UniqueHandler);
TextBox2.TextChanged += new EventHandler(UniqueHandler);
...
TextBoxN.TextChange += new EventHandler(UniqueHandler);
}
void UniqueHandler(object sender, EventArgs e)
{
TextBox source = (sender as TextBox);
// handle the event!
}
If controls will change, it's actually quite similar, it just doesn't happen in the ctor but on-site:
// anywhere in the code
TextBox addedAtRuntime = new TextBox();
addedAtRuntime.TextChanged += new EventHandler(UniqueHandler);
MyForm.Controls.Add(addedAtRuntime);
// code goes on, the new textbox will share the handler
In the properties fold-out (most often to the right of your screen) you should have a thunder icon. That's where all the events are referable.
If you don't see the properties, select the regarded component (the text box in your case), right-mouse it and pick "properties" in the context menu.
You can do it by this way:
void btn1_onchange(object sender, EventArgs e)
{
MessageBox.Show("Number One");
}
void btn1_onchange2(object sender, EventArgs e){
MessageBox.Show("Number Two");
}
public MyForm() {
Button btn1 = new Button();
btn1.Text = "Click Me";
this.Controls.Add(btn1);
btn1.TextChange += new EventHandler(btn1_onchange);
btn1.TextChange += new EventHandler(btn1_onchange2);
}
You could do it in designer view. Instead of double-clicking on an element - go to your buttons' properties, select events tab and then put a proper handler name for adequate event. Voila!
Follow these steps:
Go to the InitializeComponent().
There are three events attached to each text box.
There you shoud be able to see the following.
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
Replace this with
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
And then remove the method below
private void TextBox_2_TextChanged(object sender, EventArgs e)
{
TextChanged();
}

how to generate a SelectionRangeChanged Event Programatically ChartControl WinForms

want to create a selectionRangeChanged event programatically not really getting how to do it
private void btn_10D_Click(object sender, EventArgs e)
{
double varRange = 10;
double var_Sel1 = DatesX[0].ToOADate();
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionColor = Color.LightGray;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionStart = var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionEnd = varRange + var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.Position = varRange + var_Sel1;
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
}
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
throw new NotImplementedException();
}
thank you
For all events in C# is true that if class creator did not make extra effort to allow event firing form outside of class it is impossible to fire them.
According to MSDN
Chart.SelectionRangeChanged event Occurs when the selection start position or end position is changed.
But from my tests I can see that it is fired only if it is changed by user not program.
If I understand your intention correctly you want to handle those small buttons under your chart and btn_10D_Click method is a click handler for one of them. Try to move this line
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
to your constructor and ensure it is called once (remove it form other handlers). This will ensure your code is executed when user changes selection. If you want to execute same code for your button you should simply extract handler contents to method and call it form button click handler.
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
DoSomething(/*some arguments if you need them*/);
}
private void btn_10D_Click(object sender, EventArgs e)
{
\\your code
DoSomething();
}

Which button pressed in flowlayoutpanel?

For my current project I am adding an variable amount of usercontrols : ucTask to my flowlayoutpanel : flpTasks
foreach (task t in tasks)
{
ucTask uct = new ucTask();
uct.id = t.task_id;
uct.date= t.date.ToString();
uct.btnNaam = t.task_id.ToString();
uct.OnButtonclick += new EventHandler(uct_OnButtonclick);
flpTasks.Controls.Add(uct);
}
Now I will have a couple of those usercontrols in the flowlayoutpanel, and I want to fire an event when I press that button. With the EventHandler I can fire an event, but I need to fire a different event for a different button.
void uco_OnButtonclick(object sender, EventArgs e)
{
lblStatus.Text = "TEST";
}
I don't know how to know which button (or usercontrol) I pressed. And I'm out of ideas, any suggestions?
Thanks,
Thomas
Sender of event is actually one of buttons. When you cast sender to Button type, you can access any of Buttons properties and determine which button was pressed:
void uco_OnButtonclick(object sender, EventArgs e)
{
Button button = sender as Button;
if (button == null)
return;
lblStatus.Text = button.Name;
}
UPDATE: after reading your question once again, I get that ucTask is actually UserControl, which rises event when button inside it was clicked.
So, first is naming. Good style for class names in c# is PascalCase. E.g. for task user control good name will be TaskControl. Next goes event naming. It's common to name events as EventName(-ing, -ed). If you want to raise event, then good style is protected method OnEventName(-int, -ed). And another remark - when you writing user controls, you are free to use business terms to name your events. E.g. TaskCreated, TaskChanged. And you also can pass any parameters to your event by creating custom EventArgs.
So, for your user control:
public class TaskChangedEventArgs : EventArgs
{
public TaskChangedEventArgs(int taskId)
{
TaskId = taskId;
}
public int TaskId { get; private set; }
}
public class TaskControl : UserControl
{
public event EventHandler<TaskChangedEventArgs> TaskChanged;
// raise it inside button click event handler
protected void OnTaskChanged(int taskId)
{
if (TaskChanged != null)
TaskChanged(this, new TaskChangedEventArgs(taskId));
}
}
When you use your user control, just subscribe to its TaskChanged event:
uct.TaskChanged += new EventHandler<TaskChangedEventArgs>(uct_TaskChanged);
And all parameters, that you passed via event argument will be available in that event handler:
void uco_TaskChanged(object sender, TaskChangedEventArgs e)
{
lblStatus.Text = e.TaskId.ToString();
}
Well, the sender should be the user control right? Then you can cast and determine the item through ucTask.id.
void uco_OnButtonclick(object sender, EventArgs e)
{
ucTask uc = sender as ucTask;
lblStatus.Text = uc.id.ToString();
}
This presumes that the OnButtonclick event of the user control sends a reference to the user control and not a reference to the pressed button as the sender of the event.

Categories

Resources