I'm having a ContextMenu, and I need to determine what control has it been opened on in the event ContextMenu_Opening.
The event arguments do not contain such information, or either the sender. How can I do that?
Solved by using a simple property which I had no idea it existed..:
private void contextMenu1_Opening(object sender, CancelEventArgs e)
{
MessageBox.Show(contextMenu1.SourceControl.Name);
}
Related
I have been developing a windows form application and ran into a problem.
After trying various things (Listed below) I have come to seek your knowledge to help point me in the right direction.
I have replicated a much simpler version of my program:
As you can see, I have two textboxes. I want to be able to click on the textbox on the bottom (textbox1) and call some form of an event, in this case, for simplicity, pop up a message box.
I have been through the events listed here:
https://msdn.microsoft.com/en-us/library/system.windows.forms.textbox_events(v=vs.110).aspx
And implemented them into my code as I expected one of them to work. However, this is not the case.
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
//Above - Will pop message box when text entered.
private void textBox1_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
private void textBox1_Enter(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
private void textBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("TextBox Entered");
}
Does anybody know what I am missing? I presume what I am trying to achieve is actually possible?
Kind Regards,
B.
Ensure the event is subscribed to the methods you have written. You can do this in the design view using the Events tab of the property window (looks like a lightning bolt). As mentioned by others, a double click in the events window will generate the event's method for you, and subscribe to it automatically.
Another way is to subscribe directly using code; you could write this in the form constructor for example:
textBox1.TextChanged += textBox1_TextChanged;
I am generating a number of buttons with similar or identical content and was hoping to use the names they are given to differentiate them. As the program is creating the buttons dynamically I can't create a separate event for them all and instead need to be able to grab the name to be able to know which button triggered the event.
Is there a way to pass through the name of a button to the click event it initiates? the sender object seems to contain the content but not the name.
It is for a event like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
//getname of button
Canvas.Children.Remove(//name of button\\)
}
Far as I know, WPF does not even assign anything to the Name property automatically - that's just for developers to assign so we can reference the control.
However, you should be able to just pass in the sender to the remove method since it accepts a UIElement argument which Button is derived from.
Canvas.Children.Remove((Button)sender);
I'm not familiar with WPF. If this answer appears to be a junk, I'll delete it.
In ASP.Net, we can cast to a button to get the sender's information. Something like this -
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
button.Name
}
Perhaps getting at the control's name will work for you
private void button1_Click(object sender, RoutedEventArgs e)
{
Control control = (Control)sender;
Canvas.Children.Remove(control.Name);
}
If I create a simple Winforms app with a button and a textbox, and the following event handlers, I'd expect to see "False" when I hit the button. When I hit the button, it actually produces "True".
Why is the form valid? It doesn't appear that the validating event is executing at all, even though the docs say that passing false causes validation to be performed unconditionally.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.Validate(false).ToString());
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
It looks like you're trying to validate a child control of the Form. If that's the case, you should use one of the ValidateChildren methods instead of Validate.
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.SelectAll();
}
This works but I have 6 textBoxes. Is there any easier way instead of adding event listeners for each and every textbox? Or a shorthand or something?
Thanks.
Add the same event handler to each and have ((TextBox)sender).SelectAll() to ensure the one that is clicked on is highlighted.
If you're looking for something more generic create a derived class of TextBox containing the same.
In Visual C# Form Application, When I Click on the button I want to add to the other controls(like listboxes,labels,textboxes) in same form.
How do I do this?
I have no idea what "to come to the other controls" might mean. But the event handlers in your Form derived class is the switchboard. Implement the button's Click event and have it do whatever you want done with any other controls. A trivial example:
private void button1_Click(object sender, EventArgs e) {
label1.Text = "You clicked the button!";
}
In the form designer, add an event handler to the button's Click event.
The form designer will give you a new method like this; add your code into this method:
private void button_Click(object sender, EventArgs e)
{
// Write some code that uses list boxes, labels, text boxes etc.
}
You question is somewhat unclear, but if you simply want to access other controls on the form, just go ahead and do so:
private void YourButton_Click(object sender, EventArgs e)
{
string someValue = yourTextBox.Text;
// do something with the value
}
If you want to add one event handler to many controls, you can do it.
Just go to properties of control you wish to subscribe, find appropriate event from list (ex: onClick) and choise your existed handler.
But this method will be sutable if events compotable.
Describe your task more detail.