How to pass variable from event method? - c#

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.

Related

Access private class

Please be patient with me, I am new to C# and I am trying my best
so my question is, when we create a button in windows form app (wfa), there is a private event automatically created if you double click the button.
let's say we have 2 buttons (btn1 & btn2)
btn1 click event has variable of type string called access and I need to call this variable in btn2 click event
so how we will pass the info from a private event to another !
enter image description here
private void button1_Click(object sender, EventArgs e)
{
string access1 = "access 1";
}
private void button2_Click(object sender, EventArgs e)
{
string access2 = access1;
}
You can make the info a field instead of a local variable:
string access1 = "access 1";
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string access2 = access1;
}
Or use a property: What is the difference between a field and a property?

Calling a private void function from a button click method

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();
}
}

Is it possible to group click events?

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)
{
// ....
}
}

How to edit the contents of a list box, from a text box in another form

I am creating a library application with a listbox containing a list of books on the main form. I have created an edit form for the books. I want to be able to change the contents of the selected item in the listbox by changing the text in the textboxes of the edit form. Any suggestions how I could do this?
Main form:
private void lstBooks_SelectedIndexChanged(object sender, EventArgs e)
{
string currentBook = lstBooks.SelectedItem.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
}
Edit form:
private void frmEditBook_Load(object sender, EventArgs e)
{
txtName.Text = listBoxBooks.SelectedItem.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text.Replace);
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
In your edit form, have a public property for your text
public string NewText
{
get
{
return txtName.Text;
}
}
And then when changing the item in the listbox, simply use the following procedure.
private void btnEdit_Click(object sender, EventArgs e)
{
//Your code from above
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
//My line
lstBooks.Items[lstBooks.SelectedIndex] = tempEditBook.NewText;
}
Well, it could be very simple if you call the new form with ShowDialog() instead of Show(). In your frmEditBook you should define the property
public string Txt {get; set;}
and set it at btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text);
Txt = txtName.Text;
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
and use the variable after the edit form is closed:
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.ShowDialog();
//tempEditBook.Txt here is your text
lstBooks.SelectedItem = tempEditBook.Txt;
frmkeepBookstore.Hide();
}

I want to call a visual c# event within another event. How do I do that?

I want to call btnDisconnect_Click within btnExit_Click.
private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
}
Usually in cases like these I make my click handlers only call another function and pass in appropriate arguments:
private void btnDisconnect_Click(object sender, EventArgs e)
{
DoDisconnect();
}
private void DoDisconnect()
{
...
}
Then I can call that same function from wherever:
private void btnExit_Click(object sender, EventArgs e)
{
DoDisconnect();
}
This way your "disconnect" logic is gummed up by taking dummy arguments that don't actually affect the disconnect behavior in any way.
It also means you can start factoring out view logic from forms.
That depends on if you are using the arguments passed to the event handlers
You could yust call it using nulls
Something like
private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
btnDisconnect_Click(null,null);
}
They're just methods. Just call it. You'll need to provide whatever event arguments btnDisconnect_Click is expecting (which is probably nothing). So the simplest thing is:
private void btnExit_Click(object sender, EventArgs e)
{
btnDisconnect_Click(this, EventArgs.Empty);
}
This will pass the current form/window/whatever it is as the sender, and an EventArgs object with no data.
You can call it just as you have it listed. The this below isn't necessary but it puts context on the code:
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
this.btnDisconnect_Click(null, null);
// If you need to have sender as something you can always put
// this in directly
this.btnDisconnect_Click(this.btnDisconnect, new System.EventArgs());
}
I'm going to make an assumption here and say that what you're trying to do is call a Disconnect (perhaps a network resource) for both the disconnect and exit buttons. Instead of calling one event handler method from the other you may want to refactor the disconnect event handler's code into a separate method. Then call that method from both handlers. For example:
private void Disconnect()
{
//Disconnect here
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
//do some other stuff here
Disconnect();
}
private void btnExit_Click(object sender, EventArgs e)
{
//do some other exit stuff here
Disconnect();
}
This makes your code much cleaner and saves you from having to call one event handler from another. This begins to separate your view logic from the rest of your program's logic, which is much more desirable and much easier to maintain in the long run. For instance you may want a separate controller for handling the network resource, instead of embedding it into the view's logic.
In the simplest case you can just call the btnDiconnect_Click directly as follows:
private void btnDisconnct_Click(Object sender, EventArgs e)
{
//Does Something
}
private void btnExit_Click(Object sender, EventArgs e)
{
//Call btnDisconnect_Click()
btnDisconnect_Click(sender, e);
}
You could just call the method passing in valid parameters.
btnDisconnect_Click(btnDisconnect,new EventArgs());
However you might want to consider refactoring out the code in btnDisconnect into a new method and calling that instead:
private void doSomething()
{
//....
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
doSomething();
}
private void btnExit_Click(object sender, EventArgs e)
{
doSomething();
}
{// this is probably your constructor
.
public delegate void MyCustomHandler(object sender, EventArgs e);
.
MyCustomHandler myCustomHandler = new MyCustomHandler(); //you can do more in your delegates constructor, members etc
myCustomHandler += btnExit_Click;
myCustomHandler += btnDisconnect_Click;
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
// do Something
}
private void btnExit_Click(object sender, EventArgs e)
{
// do Something
}
//And wherever you need to invoke these, you do
myCustomHandler(object sender, EventArgs e);

Categories