How to Pass Values Between Forms in C# Windows Application

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.

How to pass values between forms in c# windows application?

Ian has given some example code, but I'd like to make a broader point:

UI classes are just classes.

How would you pass a value from one object to another object if they weren't part of the user interface? You'd have a reference from one to the other, and call a method or set a property. The same exact thing holds for user interface objects.

I mention this because it's something that comes up a lot. Whenever you ask yourself: "How do I do X with forms?" try asking yourself the same question but with plain old classes. Often the answer will be exactly the same.

Of course there are some differences for user interface classes - particularly with threading - but for an awful lot of cases, it really helps if you just think of them as normal classes.

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

Passing values between two windows forms

You could do something like this:

Child form

public string YourText { get; set; }

public TestForm()
{
InitializeComponent();
}

public void UpdateValues()
{
someLabel.Text = YourText;
}

Initiate it

var child = new TestForm {YourText = someTextBox.Text};

child.UpdateValues();

child.ShowDialog();

With this approach you don't have to change the Constructor, you could also add another constructor.

The reason for them being empty is that the properties are set after the constructor, you could Also do someting like this to add a bit of logic to your getters and setters, However, I would consider not affecting UI on properties!

private string _yourText = string.Empty;
public string YourText
{
get
{
return _yourText;
}
set
{
_yourText = value;
UpdateValues();
}
}

In this case, the UI will be updated automaticly when you set the property.

WinForms passing data between Forms

You should try to split your UI-code and your database code. This is called Layering (MVVM, MVC, MVP,...) but you don't have to!

There are several ways:

1) Make a class that both forms can reference and execute your database logic there. (that would be the cleanest way for now)

2) Create an Event in your Form with the menu and react on it in the other Form

3) Your menu Form holds a reference to the other Form and executes a Method on it passing the selected subitem.

In code

1

public static class SqlClass
{
public static void ExecuteQuery(string menuItem)
{
//execute query
}
}

Form 1
//menu changed...
SqlClass.ExecuteQuery(menuItem)

2

Form1:
public event EventHandler<string> MenuItemChanged;

//menu changed...
if(this.MenuItemChanged != null)
this.MenuItemChanged(this, menuitem)

Form2:
public Form2(Form1 otherForm)
{
InitializeComponent();
_otherForm.MenuItemChange += //... handle your sql code
}

3

private readonly Form2 _otherForm;

public Form1(Form2 otherForm)
{
InitializeComponent();
_otherForm = otherForm;
}

//menu changed...
otherForm.ExecuteQuery(menuitem);

For the examples, Form 2 is the form where you want to execute your query because there is the Method/Event-Handler defined that will interact with your database.

To understand the solution, you need a more high level perspective - you get an information in code behind of a Form (a Class) and you want to consume that information somewhere else.

In general you need a reference to the Form that holds the information you are interested in and it tells you the information when changed (Event) OR

the information source tells every interested destination (calls a Method). Then the Information source hold the references to the consumers.

Both concepts are the same just the direction of the communication (and reference) changes.

The alternative (Option 1) is that you move your information destination somewhere else (e.g. in a static class) and consume it there. The Mechanism of passing the information is pretty much the same (via a parameterized Method call) but it encapsulates the UI-colde (Form) from the Database code (SQL query execution)



Related Topics



Leave a reply



Submit