Getting information from button1_click() to button2_click() - c#

I am fairly new to c#. I have one winform with 2 buttons on it. button1_click() generates some data say data1, data2, data3, data4. Now I want to use this data in button2_click():
private void button1_click(object sender, EventArgs e)
{
//generate data1, data2, data3, data4..
}
private void button2_click(object sender, EventArgs e)
{
//do processing using data1, data2, data3, data4..
}
I assume this should be relatively simple to do in c# without using files and such.
I understand I can pass arguments using a custom class derived from EvenArgs, but I need to get hold of the data first before i can pass it.

Create private fields for data1 etc. and set them in button1_click once they are set they will be available via the current instance in button2_click.
FIelds are part of an object instance's shared state. This means that any instance fields (fields that are not marked as static and are declared within the body of the current type) are available to all instance methods (methods that are not marked as static and are declared within the body of the current type). Since both of your button click event handlers are instance methods they can both access the fields.
Try something like this:
class Foo
{
// These are the fields
Object data1;
Object data2;
Object data3;
Object data4;
void button1_click(object sender, EventArgs e)
{
this.data1 = generateData1();
this.data2 = generateData2();
this.data3 = generateData3();
this.data4 = generateData4();
}
void button2_click(object sender, EventArgs e)
{
// In this method you can access this.data1 etc. since
// they are instance fields
}
}

The easies way to do it is to add some state to your Form that is containing these two buttons (I assume they are on the same form).
So in the same class in which you have the posted methods you need to add members:
class MyForm : Form
{
MyType data1;
MyType data2;
private void button1_click(object sender, EventArgs e)
{
//generate data1, data2, data3, data4.. <-- here you just set the state of the Form
}
private void button2_click(object sender, EventArgs e)
{
//do processing using data1, data2, data3, data4.. <-- here you use the state set by button 1
}
}

You could use class fields:
private string data1 = "";
private string data2 = "";
private string data3 = "";
private void button1_click(object sender, EventArgs e)
{
data1 = "some data for field1";
data2 = "some data for field2";
data3 = "some data for field3";
}
private void button2_click(object sender, EventArgs e)
{
// use data1, data2, ... here:
MessageBox.Show(data1 + data2 + data3);
}

Example:
public class MyForm : Form {
private string _data1;
private void button1_click(object sender, EventArgs e) {
_data1 = "hello";
}
private void button2_click(object sender, EventArgs e) {
MessageBox.Show(_data1);
}
}

Related

Why my method "ReadPrice" can't access to the string "name" in the method "ReadName" [duplicate]

I'm new to C# and I really need to know how to call/use a string from another method.
For example:
public void button1_Click(object sender, EventArgs e)
{
string a = "help";
}
public void button2_Click(object sender, EventArgs e)
{
//this is where I need to call the string "a" value from button1_click
string b = "I need ";
string c = b + a;
}
In this example I need to call string "a" defined in function button1_Click() from function button2_Click()
Usually you'd pass it as an argument, like so:
void Method1()
{
var myString = "help";
Method2(myString);
}
void Method2(string aString)
{
var myString = "I need ";
var anotherString = myString + aString;
}
However, the methods in your example are event listeners. You generally don't call them directly. (I suppose you can, but I've never found an instance where one should.) So in this particular case it would be more prudent to store the value in a common location within the class for the two methods to use. Something like this:
string StringA { get; set; }
public void button1_Click(object sender, EventArgs e)
{
StringA = "help";
}
public void button2_Click(object sender, EventArgs e)
{
string b = "I need ";
string c = b + StringA;
}
Note, however, that this will behave very differently in ASP.NET. So if that's what you're using then you'll probably want to take it a step further. The reason it behaves differently is because the server-side is "stateless." So each button click coming from the client is going to result in an entirely new instance of the class. So having set that class-level member in the first button click event handler won't be reflected when using it in the second button click event handler.
In that case, you'll want to look into persisting state within a web application. Options include:
Page Values (hidden fields, for example)
Cookies
Session Variables
Application Variables
A Database
A Server-Side File
Some other means of persisting data on the server side, etc.
You need to declare string a in the scope of the class, not the method, at the moment it is a "local variable".
Example:
private string a = string.Empty;
public void button1_Click(object sender, EventArgs e)
{
a = "help";
}
public void button2_Click(object sender, EventArgs e)
{
//this is where I need to call the string "a" value from button1_click
string b = "I need";
string c = b + a;
}
You can now access the value of your "private field" a from anywhere inside your class which in your example will be a Form.
Agree with #Devid 's answer but I prefer to create a class of required entities and then use them in entire solution without passing variable as argument.
Classname.variableName;
for ex-
Class argumentData{
public static string firstArg= string.Empty;
public static string secArg= string.Empty;
}
Say I am assigning data in function
void assignData()
{
argumentData.firstArg="hey";
argumentData.secArg="hello";
}
if I want to use it in another method then
void showData()
{
Console.WriteLine("first argument"+argumentData.firstArg);
Console.WriteLine("sec argument"+ argumentData.secArg);
}
Hope this helps!
Refactor that into a method call (or property) so you can access the value of a elsewhere in your application:
public String GetStringAValue() {
return "help";
}
public void button1_Click(object sender, EventArgs e) {
string a = GetStringAValue();
}
public void button2_Click(object sender, EventArgs e) {
string a = GetStringAValue();
string b = "I need";
string c = b + a;
}
Also note that you could be using implicit type declarations. In effect, these are equivalent declarations:
string a = GetStringAValue();
var a = GetStringAValue();
class SomeClass
{
//Fields (Or Properties)
string a;
public void button1_Click(object sender, EventArgs e)
{
a = "help"; //Or however you assign it
}
public void button2_Click(object sender, EventArgs e)
{
string b = "I need";
string c = b + (a ?? String.Empty); //'a' should be null checked somehow.
}
}
make is a class level variable (global variable) or create a getter and setter for String a, to name a couple options.
You can't do that. string a is a local variable declaration. It's called "local" because it is only accessible "locally" to the block in which it occurs.
To make the variable visible to both methods, you can create a field in the class containing the methods. If the methods are in different classes, though, the solution gets more complicated.
You can't do this because those variables are in different scopes (think it as being hidden). The only way to achieve this is to move a in the main form class:
public partial class Form1 : Form
{
string a;
// etc ...
}
you can use session here
public void button1_Click(object sender, EventArgs e)
{
string a = "help";
Session["a"]=a;
}
public void button2_Click(object sender, EventArgs e)
{
string d=Session["a"].ToString();
string b = "I need ";
string c = b + d;
}
You could save the variable into a file, then access the file later, like this:
public void button1_Click(object sender, EventArgs e)
{
string a = "help";
File.WriteAllText(#"C:\myfolder\myfile.txt", a); //Change this to your real file location
}
public void button2_Click(object sender, EventArgs e)
{
string d = File.ReadAllText(#"C:\myfolder\myfile.txt");
//this is where I need to call the string "a" value from button1_click
string b = "I need";
string c = b + d; //Instead of a, put the variable name (d in this case)
}
If you do that, just make sure to put this in your code: using System.IO;

Cannot access control on another form even though Modifier is set to PUBLIC

I am trying to access a on my MainForm from another Form. This control is a FlowLayoutPanel, and I have set its Access Modifier to Public. I don't know why I can't access it from another form, because this method has always worked for me in the past.
MainForm.cs:
void button1_Click(object sender, EventArgs e)
{
using(var editor = new Editor())
{
editor.ShowDialog();
}
}
Editor.cs:
void button1_Click(object sender, EventArgs e)
{
int count = MainForm.flow.Count;
}
Why can I not access this control from another form - even though its modifier is set to public?
you are accessing the control/property wrong.
You should do it like this.
MainForm.cs
private void button1_Click(object sender, EventArgs e)
{
var frm = new Editor();
frm.ShowDialog(this);
}
Editor.cs
private void button1_Click(object sender, EventArgs e)
{
var f = (this.Owner as MainForm);
int count = f.flow.Count;
MessageBox.Show(count.ToString());
}

Passing variable from one button click to another

This is a simple question but I can't seem to find an answer. I want to use the stored value from one button click to another within the same form. Any assistance would greatly be appreciated. I tried using an example from Calling code of Button from another one in C# but could not get it to work.
public struct xmlData
{
public string xmlAttribute;
}
private void Show_btn_Click(object sender, EventArgs e)
{
xmlData myXML = new xmlData();
//do something.....
myXML.xmlAttributes = "blah"
}
private void Submit_btn_Click(object sender, EventArgs e)
{
//I want to call myXML.xmlAttributes retrieving the stored value from Show_btn_Click
}
You should declare myXML variable at higher level of scope.
xmlData myXML = new xmlData();
public struct xmlData
{
public string xmlAttribute;
}
private void Show_btn_Click(object sender, EventArgs e)
{
//do something.....
myXML.xmlAttributes = "blah"
}
private void Submit_btn_Click(object sender, EventArgs e)
{
//I want to call myXML.xmlAttributes retrieving the stored value from Show_btn_Click
}
Instanciate the xmlData in the Constructor so you can access it overall in the class.
public class XYZ
{
xmlData myXML;
public XYZ()
{
myXML = new xmlData();
}
private void Show_btn_Click(object sender, EventArgs e)
{
//do something.....
myXML.xmlAttributes = "blah"
}
private void Submit_btn_Click(object sender, EventArgs e)
{
// Here you can work myXML.xmlAttributes
}
}

simple passing value from one form to another

I'm trying to pass value from one form to another in winforms.
On my main form I have btnAddNewRecord and dataOptions combobox.
User should first select from combobox(dataOptions) and than click on btnAddNewRecord.
I want to pass this user selected value from dataoptions combobox to new form, so I tried like this
MainForm
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
var formAddRecord = new FormNewRecord();
formAddRecord.ShowDialog();
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
var formAddRecord = new FormNewRecord();
formAddRecord.SelectedDataOptions = data.ToString();
}
FormNewRecord.cs
public string SelectedDataOptions {get; set;}
private void FormNewRecord_Load(,,,,,)
{
txtSelectedDataOptions.Text = SelectedDataOptions;
}
no error on build, but on debugging txtSelectedDataOptions is not populated with passed value. What I'm doing wrong here?
Thanks
You are creating two different instances of FormNewRecord. Make formAddRecord as private field and show it on button click.
FormNewRecord formAddRecord = new FormNewRecord();
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
formAddRecord.ShowDialog();
}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
formAddRecord.SelectedDataOptions = data.ToString();
}
Well, formAddRecord should be a private field of your class, not a var redeclared in each method !
(Method btnAddNewRecord_Click has no ideas of variables declared in Method dataOptions_SelectedIndexChanged, by the way you create different instances).
So
private FormNewRecord formNewRecord_ = new FormNewRecord();
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
formNewRecord_ .ShowDialog();
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
formNewRecord_.SelectedDataOptions = data.ToString();
}
I don't think new a form instance in another form is a good way, a better way you can set the data you want to pass as public in the parent form, and when you show the child form, set the parent form as child's owner, then you can get and use the data in the child form.
set the data as a public property in the parent form, like this: Main Form:
public string passData = "";
private void btnAddNewRecord_Click(object sender, EventArgs e)
{
var formAddRecord = new FormNewRecord();
formAddRecord.ShowDialog(this); //important
}
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
passData = data.ToString(); //store the selected value to passData
}
2.get passed data from child's owner:
FormNewRecord.cs
private void FormNewRecord_Load(,,,,,)
{
if(this.Owner != null)
{
MainForm mf = (MainForm)this.Owner;
txtSelectedDataOptions.Text = mf.passData;
}
}

how to get variable from form load to action?

private void showCategory_Load(object sender, EventArgs e)
{
string categoryId = "100"
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = categoryId;
}
how to pass string categoryId from form load to button click?
Thank you.
If it's Windows Forms application than using the variable on the class level, not in the function
public partial class showCategory : Form
{
string categoryId = null;
private void showCategory_Load(object sender, EventArgs e)
{
this.categoryId = "100";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = this.categoryId;
}
}
If it's Web Application - store the value the some state callection: ViewState, Session, HiddenField and so on..

Categories

Resources