Passing variable from one button click to another - c#

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
}
}

Related

How do you insert data into a dataset then have a message box tell what happened?

I am having an issue with adding a name to a dataset through a windows form application. Also for some reason beyond me, my message box is not reading my variables. The output for the message box says AddEmployee has been added to the employee directory with the title of AddEmployee. When I check the dataset after I try adding someone it has someone added but the name is AddEmployee and job title is AddEmployee. Here is the code I am trying to get to work.
public AddEmployee()
{
InitializeComponent();
}
private String employeeName;
private String jobTitle;
private void Enter_Click(object sender, EventArgs e)
{string newEmployee = $"{employeeName} has been added to the employee directory with the title of {jobTitle}";
EmployeeDirectoryDataSetTableAdapters.EmployeeDirectoryTableAdapter EmployeeDirectoryTableAdapter =
new EmployeeDirectoryDataSetTableAdapters.EmployeeDirectoryTableAdapter();
EmployeeDirectoryTableAdapter.InsertQuery(employeeName, jobTitle);
textBox1.Text = String.Empty;
textBox2.Text = String.Empty;
MessageBox.Show(newEmployee);
}
public void SetName(String Name)
{
employeeName = Name;
}
public void SetJob(String Job)
{
jobTitle = Job;
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
SetName(Text);
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
SetJob(Text);
}
private void Button1_Click(object sender, EventArgs e)
{
this.Hide();
var newform = new Employee();
newform.Show();
}
private void AddEmployee_Load(object sender, EventArgs e)
{ this.employeeDirectoryTableAdapter.Fill(this.employeeDirectoryDataSet.EmployeeDirectory);
}
}

Populate class from textbox C#

I am trying to fill the class with data and use this data anywhere else on the program, where I will need it.
I created this class:
public class id
{
private string name; // field
public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}
And in form_name I tried to fill the class this way:
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
this.Close();
}
But I don't get any results. Could someone help me to clarify this?
What I have tried:
And in another form I tried to retrieve data like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = new id();
txtrez.Text = idobje.Name;
}
You can use some global area to store and access a common variable.
For example, create a class as a central repository.
public static class Globals {
public object myObj;
}
Then assign your created object to this one on first form.
private void Button_Click(object sender, RoutedEventArgs e)
{
id IDOBJE = new id();
{
IDOBJE.Name = txtshenimi.Text;
}
Globals.myObj = IDOBJE;
this.Close();
}
Access that on your second form this way.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
id idobje = (id)Globals.myObj;
txtrez.Text = idobje.Name;
}

How to create a global variable in C#?

I need to use a global variable in my .net project. However, i cannot handle it between two methods..
my code:
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
str = "i am a string";
showString();
}
}
void showString()
{
aspLabel.Text = str; //error
}
Question update:
I will not consider to use showString(str) because this variable is used many methods.. For example, I have a click event which need to use it.
protected void Btn_Click(object sender, EventArgs e)
{
exportToExcel(str);
}
Therefore, I need to create it in global!
The answer is don't do global variables (you also can't).
Closest to Global is having it in a class that is static and has a static member - but I really think it would be the wrong approach for most of the cases. Static classes/members usually make code more coupled and reduces testability so pick carefully when you decide to do so.
Do instead: (pass parameter)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string str = "i am a string";
showString(str);
}
}
void showString(string str)
{
aspLabel.Text = str;
}
Or:
public class SomeClass
{
private string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
str = "i am a string";
showString();
}
}
protected void Btn_Click(object sender, EventArgs e)
{
exportToExcel(str);
}
void showString()
{
aspLabel.Text = str;
}
}
Here you can change the str to be a property or a different access modifier as you wish, but this is the general idea.
If you have it as public instead of private you will be able to access it from different classes that hold an instance to this class. like this:
public class SomeClass
{
public string Str { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Str = "i am a string";
showString();
}
}
protected void Btn_Click(object sender, EventArgs e)
{
exportToExcel(Str);
}
void showString()
{
aspLabel.Text = Str;
}
}
public class SomeOtherClass
{
public SomeOtherClass()
{
SomeClass someClass = new SomeClass();
var otherStr = someClass.Str;
}
}
As has been said, don't do global variables. Instead pass a parameter into the method.
To make it slightly more obvious what is happening:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string str = "i am a string";
showString(str);
}
}
void showString(string nowthis) // nowthis == str, value is copied in
{
aspLabel.Text = nowthis;
}
There's no notion of a global variable in C#.
You can have static members like this
public static class MyClassWithStatics
{
public static string MyString {get;set;}
}
Then, in another class, you can reference it:
public class MyOtherClass
{
public void MyMethod()
{
var str = MyClassWithStatics.MyString;
}
}

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

Getting information from button1_click() to button2_click()

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

Categories

Resources