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.
Related
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();
}
}
First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
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 made this code in a class library named usedelegates:
namespace useofdelegates
{
public class Class1
{ //int i;
public delegate void mydelegate(object sender,EventArgs e);
public event mydelegate myevent;
public void fire()
{
EventArgs ee = new EventArgs();
myevent(this,ee);
}
}
}
Then in a windows form application, I intended to fire this event on the click of a button. The code in the form application is:
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
useofdelegates.Class1 ob;
private void button1_Click(object sender, EventArgs e)
{
// ob = new useofdelegates.Class1();
***ob.fire();***//give exception as object reference not set to an instance of an object.*/
}
private void Form1_Load(object sender, EventArgs e)
{
useofdelegates.Class1 ob = new useofdelegates.Class1();
ob.myevent+=new useofdelegates.Class1.mydelegate(ob_myevent);
// ob.fire();
}
public void ob_myevent(object sender, EventArgs e)
{
MessageBox.Show("hello hapiness");
}
}
}
This code on compiling throws an exception:
Object reference not set to an instance of an object.
but when I call ob.fire() in form_load(), it gives me the desired result without any exception. Why is this happening?
Several things:
The ob object is not a class variable (field) so each function needs to initialize it, register the event and then call it. You are doing this on the form load, but not on the button click.
Or rather, looking at your code, you are hiding the field in your form load by using a method variable with the same name.
This should work fine:
useofdelegates.Class1 ob;
private void button1_Click(object sender, EventArgs e)
{
ob.fire();
}
private void Form1_Load(object sender, EventArgs e)
{
ob = new useofdelegates.Class1();
ob.myevent+=new useofdelegates.Class1.mydelegate(ob_myevent);
}
You also need to check that the delegate object is not null before calling it in the class that defines the event and delegate:
if (myevent != null)
myevent(this,ee);
The thread safe version is this:
mydelegate eventcopy = myevent;
if (eventcopy != null)
eventcopy(this,ee);
You are hiding the member declaration for ob by redeclaring it as a local variable in Form_Load. Here's the way it should look:
private void Form1_Load(object sender, EventArgs e)
{
ob = new useofdelegates.Class1();
ob.myevent+=new useofdelegates.Class1.mydelegate(ob_myevent);
// ob.fire();
}
You are probably getting a waring about this from your compiler. It's always a good idea to try to eliminate compiler warnings.