I am trying to get the text value from a button that was clicked. In my head, it looks something like this:
private void button2_Click(object sender, EventArgs e)
{
string s = thisbutton.text
}
The object which fired the event is sender, so:
private void button2_Click(object sender, EventArgs e)
{
string s = (sender as Button).Text;
}
Just cast the sender Object to a Button Object and access the text attribute :
protected void btn_Click (object sender, EventArgs e){
Button btn = sender as Button;
string s= btn.Text
}
Should be like this:
private void button2_Click(object sender, EventArgs e)
{
string s = this.button2.Text;
}
In every build in event handler there are 2 parameters sender and e.Sender takes reference to that object which fires the event.The second parameter e holds some information about the event(such as the location of pointer and other of this kind)
You need only bring it to Button type and get what information you want
try and apply this example in your button event
private void button_click(object sender, EventArgs e)
{
var getValue = ((Button)sender).Text; //this will get the value of the text using sender
}
This was asked some time ago and the platform in my case may be a little different to what the OP used but I arrived at the same question for GTK.
I am developing in Xaramin / Visual Studio in OSX using GTK2+, and for me the original accepted answer is close, but yields an error that .Text doesn't exist. In my case, it needs to be Label. This works for me:
protected void Button_Clicked(object sender, EventArgs e)
{
Button btn = sender as Button;
lblWhichButton.Text = btn.Label;
if (btn.Label == "<<<" )
i--;
else
i++;
lblCounter.Text = "" + i;
}
Related
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.
I use 6 different buttons doing practically the same thing.
private void VisaCreaDoc_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.CREADOC);
}
private void VisaTravaux_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.TRAVAUX);
}
private void VisaRemiseOuvrageIR_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.REMISEOUVRIR);
}
private void VisaRemiseOuvrageExpl_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.REMISEOUVREXPL);
}
private void VisaMES_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.MISEENSERVICE);
}
private void VisaEncodageArchivage_Click(object sender, RoutedEventArgs e)
{
ViewModel.ValidateItem(InfosPosteViewModel.ENCODAGEARCHIVAGE);
As you can see, they're using a function from the ViewModel with a different parameter.
Is there any way to regroup the 6 button events to have only one and kind of pass the parameter directly in the XAML call or something similar to avoid having the "code duplication" ?
Not sure if you like it "better" but you can check which button was clicked inside the handler:
void HandleButton_Click(object sender, RoutetEventArgs e)
{
if (sender is Button b)
{
if (b == VisaCreaDoc) # VisaCreaDoc is the given name of your button instace in xaml
ViewModel.ValidateItem(InfosPosteViewModel.CREADOC);
else if (b == VisaTravaux)
ViewModel.ValidateItem(InfosPosteViewModel.TRAVAUX);
else if (...) // etc.
}
}
You can spice it up with a switch pattern matching to get rid of the if / else if / else if / ... chains.
Maybe you can avoid some duplicate code by creating a command for the ViewModel function (see Introduction to WPF Commands). As far as I know you can also define a CommandParameter in XAML.
I haven't found something that matches my problem so I ask it here. I have some code which belongs to a Textbox:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
This comes from the TextBox TextChanged event. So when the TextChanged Event gets fired it calls a method which has the if-construct above and recognizes that the textchanged event came from the textbox.
Now I want exactly the same just when someone writes into a cell in a DataGridView (not when it's just clicked - when the content changes).
How to do this correctly and which event fires when the content changes in a cell without leaving the cell?
I have found a solution for this:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
//"unwire" the event before hooking it up ensures the event handler gets called only once
tb.TextChanged -= new EventHandler(tb_TextChanged);
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("changed");
}
Now it fires everytime the value in the cell gets changed - it behaves like a textbox.
Now - I still need a solution for the "if-construct" above. How to do this exactly? I've casted the cell into a textbox but now? Does this still comes from the dataGridview?
Once I had a similar problem an solved it this way (think there are better ones but it worked)
private void DataGridView1_onFocus ( Object sender, EventArgs e)
{
DataGridView1.onKeyPress+=DataGridView1_onKeyStroke;
}
private void DataGridView1_onFocusLost( Object sender, EventArgs e)
{
DataGridView1.onKeyPress-=DataGridView1_onKeyStroke;
}
private void DataGridView1_onKeyStroke( Object sender , EventArgs e)
{
//Do your thing
}
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());
}
}
I have a textBox and search button, I would ask about how can I make the user can click Enter to start searching without need to go and click the search button?
This would be best practice
private void txtSearch_Enter(object sender, EventArgs e)
{
AcceptButton = btnSearch;
}
private void txtSearch_Leave(object sender, EventArgs e)
{
AcceptButton = null;
}
The Form has a property called "AcceptButton" that identifies a button that should be associated to the "Enter" keypress. Its considered the "default action" for the form.
More info here:
Windows Form - AcceptButton property
If you want to use something other than Enter/Return, you could also try:
private void EnterKeyAction()
{
// Search...
}
private void btnEnter_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
EnterKeyAction();
}
private void btnEnter_Click(object sender, EventArgs e)
{
EnterKeyAction();
}