I am trying to call a private void function from a button click method . The value of selectedChoice is fetched from combo box . I debugged and found out that the value of selectedChoice is being fetched properly and it is even going inside If condition inside the button click method. Its just that the functions are not being call .
Every method is inside the class Form1.
namespace Test
{
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
if(selectedChoice == "ABC")
{
Function1();
Function2();
}
The combo box code -
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex!=0)
{
selectedChoice = comboBox1.SelectedItem.ToString();
}
}
The functions I am trying to call are simply defined as
private void Function1()
{
//do something
}
You are trying to perform a compare opertion
selectedChoice == "ABC"
the
==
Operator shall not be used for string. This operator will check if the id of the element and the comparer are equals. Please use
selectedChoice.Equals("ABC")
instead.
Nevertheless the Methods are called using this code:
private string selectedChoice = "ABC";
private void button1_Click(object sender, EventArgs e)
{
if(selectedChoice.Equals("ABC"))
{
Function1();
Function2();
}
}
private void Function2()
{
}
private void Function1()
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex!=0)
{
selectedChoice = comboBox1.SelectedItem.ToString();
}
}
Related
I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}
I have a ListView with two events,
1 listView1_MouseClick(object sender, MouseEventArgs e)
2 listView1_KeyDown(object sender, KeyEventArgs e)
I need to create a single method for both event, but the problem is each event has different return types. I created a method getListViewSelected(e)
private void getListViewSelected(KeyEventArgs e)
{}
but in case listView1_MouseClick(object sender, MouseEventArgs e) the argument is Invalid. is there any way to do that?
Make 3 methods. First two are the handlers that convert the data received from the fixed signatures to your general method parameters of the third. In the third you can implement the logic that needs to trigger for both.
listView1_MouseClick(object sender, MouseEventArgs e)
{
getListViewSelected(MyDataType.FromMouseEventArgs(e));
}
listView1_KeyDown(object sender, KeyEventArgs e)
{
getListViewSelected(MyDataType.FromKeyEventArgs(e));
}
private void getListViewSelected(MyDataType data)
{
//Do your stuff
}
An example of the MyDataType class:
public class MyDataType
{
public string AnyThingOfInterest
{
get; set;
}
public static MyDataType FromKeyEventArgs(KeyEventArgs e)
{
return new MyDataType() { AnyThingOfInterest = e.ToString() };
}
public static MyDataType FromMouseEventArgs(MouseEventArgs e)
{
return new MyDataType() { AnyThingOfInterest = e.ToString() };
}
}
I have a event method
private void button1_Click_1(object sender, EventArgs e)
with some code in it. There I want to use a variable from another event method called
public void textBox2_TextChanged(object sender, EventArgs e)
so if a user writes a number it gets saved in a variable in the second method and i want to use this variable in the first method where a button gets clicked. How do I pass it?
Save it as a field of your form class. You can then write it in one event handler and read it in another:
public partial class MyForm
{
private string _someValue = null;
public void TextBox2_TextChanged(object sender, EventArgs e)
{
_someValue = "Some New Value";
}
public void Button1_Click(object sender, EventArgs e)
{
if (_someValue != null)
{
// ...
}
}
}
Though perhaps you can just read TextBox2.Text in the second method.
Hi it might sound a simple task but i am a bit confused here
I have an event :
private void ManReg_Click(object sender, EventArgs e)
{
int store = data[0];
}
and then another event function like :
private void measurementsRead_Click(object sender, EventArgs e)
{
}
I would like to use the variable "store" in the second function. How can I ?
The form is declared as follows :
namespace myRfid
{
public partial class Form1 : Form
{
}
}
You should put variable onto the class level
class ...
{
private int store;
private void ManReg_Click(object sender, EventArgs e)
{
store = data[0];
}
private void measurementsRead_Click(object sender, EventArgs e)
{
// use store
}
}
You can set it as shown in below code in your class but private as it would be used within class only as shown below :-
private int store;
private void ManReg_Click(object sender, EventArgs e)
{
this.store = data[0];
}
private void measurementsRead_Click(object sender, EventArgs e)
{
this.store//// however you want to use
}
I write:
bool example = true under the code for say, a button event: private void button1_Click(object sender, EventArgs e)
I am trying to change the value of this bool when the user pushes a button.
The problem is, my bool is only recognized in this context. When I try to use it under private void checkBox1_CheckedChanged(object sender, EventArgs e) Visual Studio notifies me of an error saying the name doesn't exist in the context.
Where should I be putting the code for a bool?
Thanks.
Move it to class-level: that is, a member of the Form class:
public class YourForm : Form {
private bool _example = true;
// ... your event handlers here
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
_example = false; // etc
}
}
Where should I be putting the code for a bool?
Outside of your methods, in the class level
public class MyForm : Form
{
// here is class level
public void SomeMethod()
{
// here is method level
}
}
As a member of the class;
public class MyClass
{
private bool example = false;
private void button1_Click(object sender, EventArgs e)
{
example = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//example available
}
}
Use static variable or class level.