Add parameter to Button click event
Simple solution:
<Button Tag="{Binding Code}" ...>
In your handler, cast the sender
object to Button
and access the Tag
property:
var myValue = ((Button)sender).Tag;
A more elegant solution would be to use the Command pattern of WPF: Create a Command for the functionality you want the button to perform, bind the Command to the Button's Command
property and bind the CommandParameter
to your value.
How can I add an additional parameter to a button click EventHandler?
You cannot convince a Button that it should know anything about a PropertyGrid. When it fires its Click event then it can only tell you about what it knows. Which is cast in stone.
You trivially work around this by using a lambda expression, it can capture the PropertyGrid argument value and pass it on to the method. Roughly:
private void SubscribeClick(PropertyGrid grid) {
button.Click += new EventHandler(
(sender, e) => button_Click(sender, e, grid)
);
}
Pass a string parameter in an onclick function
It looks like you're building DOM elements from strings. You just need to add some quotes around result.name:
'<input type="button" onClick="gotoNode(\'' + result.name + '\')" />'
You should really be doing this with proper DOM methods though.
var inputElement = document.createElement('input');
inputElement.type = "button"
inputElement.addEventListener('click', function(){
gotoNode(result.name);
});
document.body.appendChild(inputElement);
Just be aware that if this is a loop or something, result
will change before the event fires and you'd need to create an additional scope bubble to shadow the changing variable.
How to pass parameters to a Button click
You can not pass parameters to a button click function but can make a global variable that is valid in your button click and other scopes:
for example:
bool QuestionAnswered = true; // it is outside the button click or other functions
void SomeMethod()
{
QuestionAnswered = false;
}
private void NextGasQuestion_Click(object sender, EventArgs e)
{
if (!QuestionAnswered)
{
GasQuestionsFailed++;
}
}
C# Button Click with additional Parameter
use this code in form.designer.cs
private void InitializeComponent()
{
string[] args = new string[] { "param1", "param2" };
MyButton myButton = new MyButton(args);
this.SuspendLayout();
//
//myButton
//
myButton.Location = new System.Drawing.Point(230, 121);
myButton.Name = "myButton";
myButton.Size = new System.Drawing.Size(175, 31);
myButton.TabIndex = 0;
myButton.Text = "Test Click";
myButton.UseVisualStyleBackColor = true;
myButton.ButtonClick += MyButton_ButtonClick;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(myButton);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private void MyButton_ButtonClick(object Sender, System.EventArgs e, string[] args)
{
//do works.......
MyButton btn = Sender as MyButton;
MessageBox.Show(args[0] + " -- " + args[1] + " -- " + btn.Name);
}
and add this class
public class MyButton : Button
{
public event ClickEventHandler ButtonClick;
public delegate void ClickEventHandler(object Sender, EventArgs e, string[] args);
private string[] _args;
public MyButton(string[] args)
{
_args = args;
}
protected override void OnClick(EventArgs e)
{
if (ButtonClick != null)
{
ButtonClick(this, e, _args);
}
}
}
This code works perfectly.
How to pass the button value into my onclick event function?
You can pass the value to the function using this.value
, where this
points to the button
<input type="button" value="mybutton1" onclick="dosomething(this.value)">
And then access that value in the function
function dosomething(val){
console.log(val);
}
Adding a Parameter to a Button_Click event in WPF
I need to pass the list of all the processes to the event handler
Why? The button fires the event, so it has to have a known parameter list. Plus, it has no knowledge of the list of processes, so it wouldn't know what to pass in anyway. However, there's nothing from stopping you from firing off another method from the click event:
private void TerminateAll_Click(object sender, RoutedEventArgs e)
{
List<string> processes = // get the list
TerminateAll(processes);
}
public void TerminateAll(List<string> processes)
{
foreach(string process in processes)
Terminate(process);
}
private void Terminate(string process)
{
// terminate the process
}
How do I pass variables to a buttons event method?
Cant you just set a property or member variable on the form that hosts the button and access these from the button click event?
EDIT: custom button class suggestion after feedback comment (not the same suggestion as above)
class MyButton : Button
{
private Type m_TYpe;
private object m_Object;
public object Object
{
get { return m_Object; }
set { m_Object = value; }
}
public Type TYpe
{
get { return m_TYpe; }
set { m_TYpe = value; }
}
}
Button1Click(object sender, EventArgs args)
{
MyButton mb = (sender as MyButton);
//then you can access Mb.Type
//and Mb.object
}
How do I pass an object to a button click event handler in WPF? C#
Use this XAML code
<Button
x:Name="AddTaskButton" Content="+ Add" >
</Button>
You've added an eventhander on the XAML code too. You can only add event handlers with 2 parameters: an object
and a RoutedEventArgs
. Your XAML code adds an other event handler to AddTaskButton_Click
witch has a 3th parameter type of List<string>
.
Keep the C# code as is. It adds also an event handler on the correct way using the lambda expression at the constructor. But add a semicolon where you declare taskList
and change Tasklist
(with a capital T
and a little l
) to List<string>
.
Related Topics
How to Install a Windows Font Using C#
Why Visual Studio Doesn't Create a Public Class by Default
Split String into Smaller Strings by Length Variable
How to Generate and Auto Increment Id with Entity Framework
C# - How to Copy a Single Excel Worksheet from One Workbook to Another
Kestrel Shutdown Function in Startup.Cs in ASP.NET Core
How to Check If an Ip Address Is Within a Particular Subnet
Object' Does Not Contain a Definition for 'X'
ASP.NET MVC Controller Lifecycle
Setting the Initial Directory of an Savefiledialog
Combobox.Selectedtext Doesn't Give Me the Selectedtext
How to Have an Optional Parameter for an ASP.NET Soap Web Service