add click event to many control - c#

I have many controls in my form. for example 120 labels in one panel. and i want when user clicked on each label just call same function with same parameter.
Now i used like this :
private void label67_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label66_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label65_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
private void label64_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
now can i make easy way to call ChangeToTextbox function when user clicked in any label?

Add the same OnClick handler for all labels on the panel:
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.OfType<Label>().ToList().ForEach(l => l.Click += label_Click);
}
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}

Find all your controls then just add the handler via code;
List<Control> controls = GetAllMyControls();
foreach(Control control in controls)
{
control.OnClick += (o, e) => { ChangeToTextBox(o); }
}
The syntax should be very similar for both web and winform solutions.

It can be easily achieved by using following approach: Pls give a try,
1) Go to Windows Forms Designer and click the first Label control to select it. Then hold down the CTRL key while you click each of the other labels to select them. Be sure that every label is selected.
2) Then go to the Events page in the Properties window. Scroll down to the Click event, and type label_Click in the box
3) Press ENTER. The IDE adds a Click event handler called label_Click() to the code, and hooks it to each of the labels.
private void label_Click(object sender, EventArgs e)
{
ChangeToTextbox(sender);
}
Reference: http://msdn.microsoft.com/en-us/library/dd553231.aspx

Related

Raise an Event Except when something happpen

Scenario: Only If the user follow the path Click on ListView > Click on Button the Button1 do something.
In other word I want to check in Button1_Click(object sender, EventArgs e) if the previous focus was on ListView.
So I tried this:
private void ListView_Test_Leave(object sender, EventArgs e)
{
_focusedControl = null;
}
I want raise previous event except when this event is raised:
private void Button1_Click(object sender, EventArgs e)
{
if(_focusedControl == listView_Test)
{
// ...
}
}
Edit: I have a variable that holds a reference to the currently focused control:
private Control _focusedControl;
and I update it in this way:
private void ListView_Test_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
If the user follow the path Click on ListView > Click on Button I want raise only the Button1_Click event, in all other case I want normal raise.
You could use a helper variable.
bool wasRaised=false;
private void Button1_Click(object sender, EventArgs e) { wasRaised=true;}
Then you can check that variable in your event, and only run if it is false.

c# how to catch mouse event outside textbox

i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}

How to perform Label click in C#?

I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}

Click event in blank area of TreeView

To determine the content of my context menu I need to detect when the user clicks into the blank area of a treeview.
But, neither the Click nor the MouseClick events fire.
Is it possible to achieve the desired functionality by deriving from TreeView?
If not, what can I do?
You can use the MouseDown event, that fires even in the empty area.
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
// MessageBox.Show("treeView1_MouseClick");
}
private void treeView1_Click(object sender, EventArgs e)
{
// MessageBox.Show("treeView1_Click");
}
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("treeView1_MouseDown");
}

Update form from shared event

I've got a form I've created with 4 buttons that will modify a listView, each with their own click event. I'm wondering as a way to avoid adding a method call in each of these buttons if it their is an event available for after a button has finished executing its click event?
The reason for this is so I can update the listView which will in turn update a webBrowser
Here's an example which results in "1" and then "all" being displayed to the user when button 1 is clicked, "2" and then "all" when button 2 is clicked etc etc.
Note: you don't want to do it like this - see comments
public Form1()
{
InitializeComponent();
foreach(var b in Controls.OfType<Button>())
b.Click += new EventHandler(AnyButton_Click);
}
void AnyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("all");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("3");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("2");
}

Categories