Interaction between forms — How to change a control of a form from another form?Get combobox selectedindex from another class in winformspassing datagridview value to second formC# Fill Datagridview in Form1 using Form2 query buttonChange Label Text via ListView ItemActivateCannot update datagridview from another formControlling an objects property from a form to another using a button C#Sharing Informations between Form/ClassHow to print my score on another Winform?How can I call the controls of a form from another form in winformsUpdating form label instead creating new formCross-thread operation not valid: Control accessed from a thread other than the thread it was created onHow do I update the GUI from another thread?Dynamic Forms Control PropertiesCall one constructor from anotherComboBox.SelectedValue throws null reference exceptionHow to get SelectedValue from first combobox to fill data in second combobox in c# winformsHow to change selected combobox item depanding on text instead of value in windows form application (C#)?databinding multi-valued control in WinForms DataGridViewWindows Form DataGridView attach SelectedIndexChanged event to ComboBoxWinforms Datagridview Sort by Combobox Displaymember
Can I still be respawned if I die by falling off the map?
Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
What happens if you are holding an Iron Flask with a demon inside and walk into an Antimagic Field?
Open a doc from terminal, but not by its name
Invalid date error by date command
How should I respond when I lied about my education and the company finds out through background check?
Can a Canadian Travel to the USA twice, less than 180 days each time?
Does malloc reserve more space while allocating memory?
What is Cash Advance APR?
How do apertures which seem too large to physically fit work?
Lowest total scrabble score
What exact color does ozone gas have?
Using substitution ciphers to generate new alphabets in a novel
Why is the "ls" command showing permissions of files in a FAT32 partition?
What should you do when eye contact makes your subordinate uncomfortable?
Do the primes contain an infinite almost arithmetic progression?
putting logo on same line but after title, latex
Why can Carol Danvers change her suit colours in the first place?
Why would a new[] expression ever invoke a destructor?
Creepy dinosaur pc game identification
Has any country ever had 2 former presidents in jail simultaneously?
Need help understanding what a natural log transformation is actually doing and why specific transformations are required for linear regression
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
Interaction between forms — How to change a control of a form from another form?
Get combobox selectedindex from another class in winformspassing datagridview value to second formC# Fill Datagridview in Form1 using Form2 query buttonChange Label Text via ListView ItemActivateCannot update datagridview from another formControlling an objects property from a form to another using a button C#Sharing Informations between Form/ClassHow to print my score on another Winform?How can I call the controls of a form from another form in winformsUpdating form label instead creating new formCross-thread operation not valid: Control accessed from a thread other than the thread it was created onHow do I update the GUI from another thread?Dynamic Forms Control PropertiesCall one constructor from anotherComboBox.SelectedValue throws null reference exceptionHow to get SelectedValue from first combobox to fill data in second combobox in c# winformsHow to change selected combobox item depanding on text instead of value in windows form application (C#)?databinding multi-valued control in WinForms DataGridViewWindows Form DataGridView attach SelectedIndexChanged event to ComboBoxWinforms Datagridview Sort by Combobox Displaymember
I would like to set comboBox.SelectedValue when I select the row in my dataGridView on first form to populate comboBox with that value on another form,
On second form in my load event I have comboBox.DataSource
, DisplayMember
, ValueMember
set it correctly but nothing is happening when I set selectedValue on first. Everything works great when I do it on one form
c# .net winforms
add a comment |
I would like to set comboBox.SelectedValue when I select the row in my dataGridView on first form to populate comboBox with that value on another form,
On second form in my load event I have comboBox.DataSource
, DisplayMember
, ValueMember
set it correctly but nothing is happening when I set selectedValue on first. Everything works great when I do it on one form
c# .net winforms
As an option you can pass the value to constructor of your new form. Then you can store it in a member field and use it inLoad
event.
– Reza Aghaei
Aug 4 '16 at 13:38
I really have no Idea how to do that I don't work so much with forms ?
– jhony3
Aug 4 '16 at 13:42
2
Such question has been asked many times, For example this post may be a possible duplicate: Passing Values Between Windows Forms c# Also this one: How to pass values between forms in c# windows application?. But I also believe the current answer is good enough to be a separate post, it contains some useful options for different cases.
– Reza Aghaei
Aug 4 '16 at 14:21
add a comment |
I would like to set comboBox.SelectedValue when I select the row in my dataGridView on first form to populate comboBox with that value on another form,
On second form in my load event I have comboBox.DataSource
, DisplayMember
, ValueMember
set it correctly but nothing is happening when I set selectedValue on first. Everything works great when I do it on one form
c# .net winforms
I would like to set comboBox.SelectedValue when I select the row in my dataGridView on first form to populate comboBox with that value on another form,
On second form in my load event I have comboBox.DataSource
, DisplayMember
, ValueMember
set it correctly but nothing is happening when I set selectedValue on first. Everything works great when I do it on one form
c# .net winforms
c# .net winforms
edited May 30 '17 at 11:07
Reza Aghaei
68.7k858172
68.7k858172
asked Aug 4 '16 at 13:29
jhony3jhony3
132110
132110
As an option you can pass the value to constructor of your new form. Then you can store it in a member field and use it inLoad
event.
– Reza Aghaei
Aug 4 '16 at 13:38
I really have no Idea how to do that I don't work so much with forms ?
– jhony3
Aug 4 '16 at 13:42
2
Such question has been asked many times, For example this post may be a possible duplicate: Passing Values Between Windows Forms c# Also this one: How to pass values between forms in c# windows application?. But I also believe the current answer is good enough to be a separate post, it contains some useful options for different cases.
– Reza Aghaei
Aug 4 '16 at 14:21
add a comment |
As an option you can pass the value to constructor of your new form. Then you can store it in a member field and use it inLoad
event.
– Reza Aghaei
Aug 4 '16 at 13:38
I really have no Idea how to do that I don't work so much with forms ?
– jhony3
Aug 4 '16 at 13:42
2
Such question has been asked many times, For example this post may be a possible duplicate: Passing Values Between Windows Forms c# Also this one: How to pass values between forms in c# windows application?. But I also believe the current answer is good enough to be a separate post, it contains some useful options for different cases.
– Reza Aghaei
Aug 4 '16 at 14:21
As an option you can pass the value to constructor of your new form. Then you can store it in a member field and use it in
Load
event.– Reza Aghaei
Aug 4 '16 at 13:38
As an option you can pass the value to constructor of your new form. Then you can store it in a member field and use it in
Load
event.– Reza Aghaei
Aug 4 '16 at 13:38
I really have no Idea how to do that I don't work so much with forms ?
– jhony3
Aug 4 '16 at 13:42
I really have no Idea how to do that I don't work so much with forms ?
– jhony3
Aug 4 '16 at 13:42
2
2
Such question has been asked many times, For example this post may be a possible duplicate: Passing Values Between Windows Forms c# Also this one: How to pass values between forms in c# windows application?. But I also believe the current answer is good enough to be a separate post, it contains some useful options for different cases.
– Reza Aghaei
Aug 4 '16 at 14:21
Such question has been asked many times, For example this post may be a possible duplicate: Passing Values Between Windows Forms c# Also this one: How to pass values between forms in c# windows application?. But I also believe the current answer is good enough to be a separate post, it contains some useful options for different cases.
– Reza Aghaei
Aug 4 '16 at 14:21
add a comment |
1 Answer
1
active
oldest
votes
Form
in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:
Manipulate second Form from first Form
You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.
You can create public property or method in the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form. This option is not limited to passing values when creating the second form. You can even use that property during the execution of second
Form
. Also it's useful for getting a value from it.As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.
Manipulate first Form from second form
You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.
You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.
You can define a public property of type
Action
or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.
Here are some useful examples about above solutions.
Manipulate second Form from first Form
Example1 - Using constructor of second Form
Use this example when you need to pass some data to second form, when creating the second form.
public partial class Form2 : Form
int selectedValue;
public Form2(int value)
InitializeComponent();
selectedValue = value;
private void Form2_Load(object sender, EventArgs e)
//Load data
this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedValue = selectedValue;
Then in your first form, it's enough to pass the value to Form2
when you create a new instance of it:
var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();
Example2 - using public Property or Method of second Form
Use this example when you need to pass some data to second form, when creating or even after creation of second form.
public partial class Form2 : Form
public Form2()
InitializeComponent();
public string SomeValue
get return textBox1.Text;
set textBox1.Text = value;
Then in your first form, it's enough to pass the value to Form2
when you need, after creating Form2
or whenever you need to set value of textBox1
on Form2
:
var f = new Form2(); //value is not needed here
f.SomeValue = "some value";
f.Show();
//...
f.SomeValue = "some other value";
Example 3 - Making a Control of Second form public
Use this example when you need to change a property of a control on second form, when creating or even after creation of second form. It's better to use public property or method instead of exposing whole control properties.
In your Form
, at designer, select the control and in Properties window set the Modifiers
property to Public
. Also make sure the GenerateMember
property is true
. Then you can simply access this control using its name from outside of the Form
.
var f = new Form2();
f.textBox1= "some value";
Manipulate first Form from second form
Example 4 - Create public Method or Property in first Form and pass an instance of First Form to constructor of second Form
Use this example when you need to change first Form
from second Form.
In your Form1
, create a property of a method that accepts some parameters and put the logic in it:
public void ChangeTextBox1Text(string text)
this.textBox1.Text = text;
Then create a constructor in Form2
which accepts a parameter of type Form1
and keep the passed value in a member field and use it when you need:
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.ChangeTextBox1Text("Some Value");
Now when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Example 5 - Using event of second Form in first Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 6 - Injection an Action in second Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 7 - Making a Control of first form public
In this solution you need to make a control in first form public, like example 3. Then like example 4 pass an instance of the first form to second form and keep it in a field and use it when you need. Using a public method or property is preferred.
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.textBox1.Text = "Some Value";
when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
it works thx man
– jhony3
Aug 4 '16 at 14:21
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38768737%2finteraction-between-forms-how-to-change-a-control-of-a-form-from-another-form%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Form
in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:
Manipulate second Form from first Form
You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.
You can create public property or method in the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form. This option is not limited to passing values when creating the second form. You can even use that property during the execution of second
Form
. Also it's useful for getting a value from it.As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.
Manipulate first Form from second form
You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.
You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.
You can define a public property of type
Action
or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.
Here are some useful examples about above solutions.
Manipulate second Form from first Form
Example1 - Using constructor of second Form
Use this example when you need to pass some data to second form, when creating the second form.
public partial class Form2 : Form
int selectedValue;
public Form2(int value)
InitializeComponent();
selectedValue = value;
private void Form2_Load(object sender, EventArgs e)
//Load data
this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedValue = selectedValue;
Then in your first form, it's enough to pass the value to Form2
when you create a new instance of it:
var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();
Example2 - using public Property or Method of second Form
Use this example when you need to pass some data to second form, when creating or even after creation of second form.
public partial class Form2 : Form
public Form2()
InitializeComponent();
public string SomeValue
get return textBox1.Text;
set textBox1.Text = value;
Then in your first form, it's enough to pass the value to Form2
when you need, after creating Form2
or whenever you need to set value of textBox1
on Form2
:
var f = new Form2(); //value is not needed here
f.SomeValue = "some value";
f.Show();
//...
f.SomeValue = "some other value";
Example 3 - Making a Control of Second form public
Use this example when you need to change a property of a control on second form, when creating or even after creation of second form. It's better to use public property or method instead of exposing whole control properties.
In your Form
, at designer, select the control and in Properties window set the Modifiers
property to Public
. Also make sure the GenerateMember
property is true
. Then you can simply access this control using its name from outside of the Form
.
var f = new Form2();
f.textBox1= "some value";
Manipulate first Form from second form
Example 4 - Create public Method or Property in first Form and pass an instance of First Form to constructor of second Form
Use this example when you need to change first Form
from second Form.
In your Form1
, create a property of a method that accepts some parameters and put the logic in it:
public void ChangeTextBox1Text(string text)
this.textBox1.Text = text;
Then create a constructor in Form2
which accepts a parameter of type Form1
and keep the passed value in a member field and use it when you need:
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.ChangeTextBox1Text("Some Value");
Now when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Example 5 - Using event of second Form in first Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 6 - Injection an Action in second Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 7 - Making a Control of first form public
In this solution you need to make a control in first form public, like example 3. Then like example 4 pass an instance of the first form to second form and keep it in a field and use it when you need. Using a public method or property is preferred.
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.textBox1.Text = "Some Value";
when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
it works thx man
– jhony3
Aug 4 '16 at 14:21
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
|
show 3 more comments
Form
in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:
Manipulate second Form from first Form
You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.
You can create public property or method in the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form. This option is not limited to passing values when creating the second form. You can even use that property during the execution of second
Form
. Also it's useful for getting a value from it.As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.
Manipulate first Form from second form
You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.
You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.
You can define a public property of type
Action
or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.
Here are some useful examples about above solutions.
Manipulate second Form from first Form
Example1 - Using constructor of second Form
Use this example when you need to pass some data to second form, when creating the second form.
public partial class Form2 : Form
int selectedValue;
public Form2(int value)
InitializeComponent();
selectedValue = value;
private void Form2_Load(object sender, EventArgs e)
//Load data
this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedValue = selectedValue;
Then in your first form, it's enough to pass the value to Form2
when you create a new instance of it:
var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();
Example2 - using public Property or Method of second Form
Use this example when you need to pass some data to second form, when creating or even after creation of second form.
public partial class Form2 : Form
public Form2()
InitializeComponent();
public string SomeValue
get return textBox1.Text;
set textBox1.Text = value;
Then in your first form, it's enough to pass the value to Form2
when you need, after creating Form2
or whenever you need to set value of textBox1
on Form2
:
var f = new Form2(); //value is not needed here
f.SomeValue = "some value";
f.Show();
//...
f.SomeValue = "some other value";
Example 3 - Making a Control of Second form public
Use this example when you need to change a property of a control on second form, when creating or even after creation of second form. It's better to use public property or method instead of exposing whole control properties.
In your Form
, at designer, select the control and in Properties window set the Modifiers
property to Public
. Also make sure the GenerateMember
property is true
. Then you can simply access this control using its name from outside of the Form
.
var f = new Form2();
f.textBox1= "some value";
Manipulate first Form from second form
Example 4 - Create public Method or Property in first Form and pass an instance of First Form to constructor of second Form
Use this example when you need to change first Form
from second Form.
In your Form1
, create a property of a method that accepts some parameters and put the logic in it:
public void ChangeTextBox1Text(string text)
this.textBox1.Text = text;
Then create a constructor in Form2
which accepts a parameter of type Form1
and keep the passed value in a member field and use it when you need:
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.ChangeTextBox1Text("Some Value");
Now when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Example 5 - Using event of second Form in first Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 6 - Injection an Action in second Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 7 - Making a Control of first form public
In this solution you need to make a control in first form public, like example 3. Then like example 4 pass an instance of the first form to second form and keep it in a field and use it when you need. Using a public method or property is preferred.
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.textBox1.Text = "Some Value";
when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
it works thx man
– jhony3
Aug 4 '16 at 14:21
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
|
show 3 more comments
Form
in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:
Manipulate second Form from first Form
You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.
You can create public property or method in the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form. This option is not limited to passing values when creating the second form. You can even use that property during the execution of second
Form
. Also it's useful for getting a value from it.As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.
Manipulate first Form from second form
You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.
You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.
You can define a public property of type
Action
or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.
Here are some useful examples about above solutions.
Manipulate second Form from first Form
Example1 - Using constructor of second Form
Use this example when you need to pass some data to second form, when creating the second form.
public partial class Form2 : Form
int selectedValue;
public Form2(int value)
InitializeComponent();
selectedValue = value;
private void Form2_Load(object sender, EventArgs e)
//Load data
this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedValue = selectedValue;
Then in your first form, it's enough to pass the value to Form2
when you create a new instance of it:
var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();
Example2 - using public Property or Method of second Form
Use this example when you need to pass some data to second form, when creating or even after creation of second form.
public partial class Form2 : Form
public Form2()
InitializeComponent();
public string SomeValue
get return textBox1.Text;
set textBox1.Text = value;
Then in your first form, it's enough to pass the value to Form2
when you need, after creating Form2
or whenever you need to set value of textBox1
on Form2
:
var f = new Form2(); //value is not needed here
f.SomeValue = "some value";
f.Show();
//...
f.SomeValue = "some other value";
Example 3 - Making a Control of Second form public
Use this example when you need to change a property of a control on second form, when creating or even after creation of second form. It's better to use public property or method instead of exposing whole control properties.
In your Form
, at designer, select the control and in Properties window set the Modifiers
property to Public
. Also make sure the GenerateMember
property is true
. Then you can simply access this control using its name from outside of the Form
.
var f = new Form2();
f.textBox1= "some value";
Manipulate first Form from second form
Example 4 - Create public Method or Property in first Form and pass an instance of First Form to constructor of second Form
Use this example when you need to change first Form
from second Form.
In your Form1
, create a property of a method that accepts some parameters and put the logic in it:
public void ChangeTextBox1Text(string text)
this.textBox1.Text = text;
Then create a constructor in Form2
which accepts a parameter of type Form1
and keep the passed value in a member field and use it when you need:
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.ChangeTextBox1Text("Some Value");
Now when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Example 5 - Using event of second Form in first Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 6 - Injection an Action in second Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 7 - Making a Control of first form public
In this solution you need to make a control in first form public, like example 3. Then like example 4 pass an instance of the first form to second form and keep it in a field and use it when you need. Using a public method or property is preferred.
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.textBox1.Text = "Some Value";
when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Form
in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:
Manipulate second Form from first Form
You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.
You can create public property or method in the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form. This option is not limited to passing values when creating the second form. You can even use that property during the execution of second
Form
. Also it's useful for getting a value from it.As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.
Manipulate first Form from second form
You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.
You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.
You can define a public property of type
Action
or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.
Here are some useful examples about above solutions.
Manipulate second Form from first Form
Example1 - Using constructor of second Form
Use this example when you need to pass some data to second form, when creating the second form.
public partial class Form2 : Form
int selectedValue;
public Form2(int value)
InitializeComponent();
selectedValue = value;
private void Form2_Load(object sender, EventArgs e)
//Load data
this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "Id";
this.comboBox1.SelectedValue = selectedValue;
Then in your first form, it's enough to pass the value to Form2
when you create a new instance of it:
var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();
Example2 - using public Property or Method of second Form
Use this example when you need to pass some data to second form, when creating or even after creation of second form.
public partial class Form2 : Form
public Form2()
InitializeComponent();
public string SomeValue
get return textBox1.Text;
set textBox1.Text = value;
Then in your first form, it's enough to pass the value to Form2
when you need, after creating Form2
or whenever you need to set value of textBox1
on Form2
:
var f = new Form2(); //value is not needed here
f.SomeValue = "some value";
f.Show();
//...
f.SomeValue = "some other value";
Example 3 - Making a Control of Second form public
Use this example when you need to change a property of a control on second form, when creating or even after creation of second form. It's better to use public property or method instead of exposing whole control properties.
In your Form
, at designer, select the control and in Properties window set the Modifiers
property to Public
. Also make sure the GenerateMember
property is true
. Then you can simply access this control using its name from outside of the Form
.
var f = new Form2();
f.textBox1= "some value";
Manipulate first Form from second form
Example 4 - Create public Method or Property in first Form and pass an instance of First Form to constructor of second Form
Use this example when you need to change first Form
from second Form.
In your Form1
, create a property of a method that accepts some parameters and put the logic in it:
public void ChangeTextBox1Text(string text)
this.textBox1.Text = text;
Then create a constructor in Form2
which accepts a parameter of type Form1
and keep the passed value in a member field and use it when you need:
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.ChangeTextBox1Text("Some Value");
Now when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
Example 5 - Using event of second Form in first Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 6 - Injection an Action in second Form
Take a look at this post. It's about communication between form and a control, but it's applicable to communication between forms also.
Example 7 - Making a Control of first form public
In this solution you need to make a control in first form public, like example 3. Then like example 4 pass an instance of the first form to second form and keep it in a field and use it when you need. Using a public method or property is preferred.
Form1 form1;
public Form2 (Form1 f)
InitializeComponent();
form1 = f;
private void button1_Click(object sender, EventArgs e)
form1.textBox1.Text = "Some Value";
when creating Form2
you should pass an instance of Form1
to it:
var f = new Form2(this);
f.Show();
edited Feb 6 '18 at 13:54
GntS
4211622
4211622
answered Aug 4 '16 at 13:48
Reza AghaeiReza Aghaei
68.7k858172
68.7k858172
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
it works thx man
– jhony3
Aug 4 '16 at 14:21
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
|
show 3 more comments
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
it works thx man
– jhony3
Aug 4 '16 at 14:21
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
Ok thx man I will try this and will come back with info
– jhony3
Aug 4 '16 at 13:49
it works thx man
– jhony3
Aug 4 '16 at 14:21
it works thx man
– jhony3
Aug 4 '16 at 14:21
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
I clicked up is that I have two combo and just pass two values saved my time thx again man
– jhony3
Aug 4 '16 at 14:24
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Ok I press thic green check now
– jhony3
Aug 4 '16 at 14:27
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
Thanks for the feedback :)
– Reza Aghaei
Aug 4 '16 at 14:29
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38768737%2finteraction-between-forms-how-to-change-a-control-of-a-form-from-another-form%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As an option you can pass the value to constructor of your new form. Then you can store it in a member field and use it in
Load
event.– Reza Aghaei
Aug 4 '16 at 13:38
I really have no Idea how to do that I don't work so much with forms ?
– jhony3
Aug 4 '16 at 13:42
2
Such question has been asked many times, For example this post may be a possible duplicate: Passing Values Between Windows Forms c# Also this one: How to pass values between forms in c# windows application?. But I also believe the current answer is good enough to be a separate post, it contains some useful options for different cases.
– Reza Aghaei
Aug 4 '16 at 14:21