Passing Values Between Windows Forms c#
You need to put loginData
into a local variable inside the frmVoiceOver
class to be able to access it from other methods. Currently it is scoped to the constructor:
class frmVoiceOver : Form
{
private NewDataSet _loginData;
public frmVoiceOver(NewDataSet loginData)
{
_loginData = loginData;
InitializeComponent();
}
private void btnVoiceOverNo_Click(object sender, EventArgs e)
{
// Use _loginData here.
this.Close();
Form myFrm = new frmClipInformation();
myFrm.Show();
}
}
Also, if the two forms are in the same process you likely don't need to serialize the data and can simply pass it as a standard reference to the form's constructor.
Google something like "C# variable scope" to understand more in this area as you will encounter the concept all the time. I appreciate you are self-taught so I'm just trying to bolster that :-)
Passing data between C# forms
The code that you put in the sample project (that you provided as a link in the comments) should be in your question. Given that it becomes much easier to understand what you're trying to do and to give you a workable solution.
I would suggest creating a "DataTransferObject" and pass that between each form.
public class Dto
{
public string Text;
}
The code in MainWindow
would then look like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
var dto = new Dto();
window2 win2 = new window2();
win2.Dto = dto;
win2.ShowDialog();
textBox1.Text = dto.Text;
}
And the code in window2
would look like this:
public Dto Dto;
private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
{
if (this.Dto != null)
{
this.Dto.Text = textBox2.Text;
}
}
That is one way - out of about a million - of transferring data between forms. An advantage of using a data transfer object is that it begins you on the road of separating your data from your UI, and that is generally a very good thing to do.
Passing data between forms
Form1 frm = new Form1();
frm is now a new instance of class Form1.
frm does not refer to the original instance of Form1 that was displayed to the user.
One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.
Passing values between Windows form and a database
An example of an alternative to the DB connection you have made is below
List<MyPhone> myIPhoneList = new List<Myphone>();
List<MyPhone> myAndroidList = new List<Myphone>();
SqlConnection myDBConnection = new SqlConnection("MyConnectionString"); //DB Connection
SqlCommand dbCommand = new SqlCommand("SelectAllPhones"); //Stored Procedure
SqlDataReader recordReader = dbCommand.ExecuteReader(); //Execute
//Read records return in to phone objects
while (recordReader.Read()) {
var phoneField1 = recordReader["PhoneField1FromDatabase"];
var phoneField2 = recordReader["PhoneField2FromDatabase"];
//etc...
var myPhone = new MyPhone();
myPhone.Name = phoneField1;
//etc...
if (myPhone.OS == "iPhone")
myIPhoneList.Add(myPhone);
if (myPhone.OS = "Android")
myAndroidList.Add(myPhone);
}
Pass data between 2 already OPEN winforms (C#)
You can use events to pass messages to disconnected entities. You can listen for events in Form1 and raise them from Form2. This is from memory, don't fully expect it to compile.
Create a class that is the "event hub".
public static class EventHub
{
public static event EventHandler<EventArgs> formMessageHandler;
public static void OnFormMessage(object sender, EventArgs e)
{
formMessageHandler?.Invoke(sender, e);
}
}
In Form2 button click event raise the event. You can use the sender reference directly to access public members or you can put relevant data into an EventArgs.
EventHub.OnFormMessage(this, EventArgs.Empty);
In Form1, maybe in the constructor, wire up a listener:
EventHub.formMessageHandler += some_method_you_write;
Now, when you click the button in Form2, Form1 will receive a reference to Form2 in some_method_you_write(). Or, you can actually use the EventArgs to pass data so you don't need to make any public controls on Form2.
Passing Values from one Form to another Form in a Button click
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
Related Topics
Where to Learn About VS Debugger 'Magic Names'
Do Try/Catch Blocks Hurt Performance When Exceptions Are Not Thrown
Make Https Call Using Httpclient
How to Stop C# Console Applications from Closing Automatically
Can Anonymous Class Implement Interface
How to Calculate Distance Similarity Measure of Given 2 Strings
Assigning Out/Ref Parameters in Moq
How to Deal with Files with a Name Longer Than 259 Characters
How to Embed a Text File in a .Net Assembly
Intersection of Multiple Lists with Ienumerable.Intersect()
Change Wpf Controls from a Non-Main Thread Using Dispatcher.Invoke
How to Pass Command-Line Arguments to a Winforms Application
Why Is It Bad to Use an Iteration Variable in a Lambda Expression
C# Splitting Strings on '#' Character
How to Make a .Net Windows Forms Application That Only Runs in the System Tray
Remove Weird Characters ( a with Hat) from SQL Server Varchar Column