Hey everybody,
I'm developing a video processing application in C#. The video feed is shown in Form1. What I want to do is control the properties of the video feed from Form2 because I don't want to flood the main interface with trackbars. Then I realized that variables from Form1 are not accessible from Form2 and vise versa.Can somebody tell me how to pass the values from the trackbars of Form2 to Form1?
Thanks,
Fox.
C# - How to access a form from another form?
Re: C# - How to access a form from another form?
Welcome to ROBOT.LK Fox!
Refer to How to pass controls from one form to another using C#. I guess you'll be able to figure out based on that. If you have questions, please ask.
Refer to How to pass controls from one form to another using C#. I guess you'll be able to figure out based on that. If you have questions, please ask.
Re: C# - How to access a form from another form?
Hi
here is another way. Use the "TAG" property.
Form1 code will look like this
and here goes the Form2 code
You can use the tag property to assign any type of object.
Hope this will help you.
Thanks
here is another way. Use the "TAG" property.
Form1 code will look like this
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Tag = this; // Assign the current from1 object to the form2 tag property
frm2.ShowDialog();
}
public void CallForm1method()
{
MessageBox.Show("Form 1 method called.");
}
}
}
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = this.Tag as Form1; //getting back the form1 object
frm1.CallForm1method();
}
}
}
Hope this will help you.
Thanks
Re: C# - How to access a form from another form?
Thanks for the quick replies guys!
I'm a little confused about using the Tag property posted by Enigma.I think I understand the Form1 code.
So if I want to pass a value of a trackbar from Form1 to Form2,
Form2 form2= new Form2();
form2.Tag = trackbar1.Value;
Is that correct? If so does the second line actually pass the trackbar1 value to the form2.Tag property?
OK the Form2 code is a little bit confusing for me.
In my case it should be,
int val = this.Tag as int; //This line throws an error saying that an int is non nullable.
Can you help me out with this?
I'll also try the method in the topic Saman suggested.
P.S. Can somebody tell me how to use the syntax highlighting here?
Thanks,
Fox.
I'm a little confused about using the Tag property posted by Enigma.I think I understand the Form1 code.
So if I want to pass a value of a trackbar from Form1 to Form2,
Form2 form2= new Form2();
form2.Tag = trackbar1.Value;
Is that correct? If so does the second line actually pass the trackbar1 value to the form2.Tag property?
OK the Form2 code is a little bit confusing for me.
In my case it should be,
int val = this.Tag as int; //This line throws an error saying that an int is non nullable.
Can you help me out with this?
I'll also try the method in the topic Saman suggested.
P.S. Can somebody tell me how to use the syntax highlighting here?
Thanks,
Fox.
Re: C# - How to access a form from another form?
Hi Fox
Because you can not cast a value type using as keyword. As far as I know only reference types can be cast like that.
for syntax highlighting please refer this
https://robot.lk/faq.php?mode=bbcode#f2r0
Thanks
yes. This correctI'm a little confused about using the Tag property posted by Enigma.I think I understand the Form1 code.
So if I want to pass a value of a trackbar from Form1 to Form2,
Form2 form2= new Form2();
form2.Tag = trackbar1.Value;
Is that correct?
No. In your case it should beOK the Form2 code is a little bit confusing for me.
In my case it should be,
int val = this.Tag as int; //This line throws an error saying that an int is non nullable.
Code: Select all
int val = Convert.ToInt32(this.Tag);
//or you can use
int val = (int)this.Tag;
for syntax highlighting please refer this
https://robot.lk/faq.php?mode=bbcode#f2r0
Thanks
Re: C# - How to access a form from another form?
You create Form2 from Form1. So you have coding as below.
Then you set Form2's Tag to hold the Form1 object ('this' refers to current object (opened window) of Form1 Class).
Now, let's think we are in the code of newly created object frm2 of Form2.
Now this.Tag property holds the object of the calling object (calling window of type Form1).
That means, you can get it casts to a Form type and that's the object of calling window.
Note that 'as' is used to cast in C#. You would also use direct casting like below as long as you give a guarantee to the system that you know it is exactly in the specified type. Otherwise it will lead to unexpected results.
Since c-style casting and 'as' seem quite confusing, I have added a little description here at What's the best casting method in C# (c-style Vs. 'as').
Okay.. So far so good. Now you need to access trackbar, right? I guess that's your problem?
Since you now have frm1 (this is the same object windows which is opened behind), you can access that object as below.
Whatever you change to that will directly effect the behind window since it is the same object you are referring. I'm sure you are now clear on Enigma's code?
Code: Select all
Form2 frm2 = new Form2();
Code: Select all
frm2.Tag = this;
Now this.Tag property holds the object of the calling object (calling window of type Form1).
That means, you can get it casts to a Form type and that's the object of calling window.
Code: Select all
Form1 frm1 = this.Tag as Form1;
Code: Select all
Form1 frm1 = (Form1) this.Tag;
Okay.. So far so good. Now you need to access trackbar, right? I guess that's your problem?
Since you now have frm1 (this is the same object windows which is opened behind), you can access that object as below.
Code: Select all
frm1.trackbar
Go to Portal >> BBCode FAQP.S. Can somebody tell me how to use the syntax highlighting here?
Re: C# - How to access a form from another form?
Thanks everybody! I finally got it working.
Re: C# - How to access a form from another form?
Excellent.
Spread the message of ROBOT.LK buddy. We need 1000s of people discussing technical matters for free all the time.
1. Invite your friend using "Invite a friend" link on top.
2. Invite all your friends (irrespective of their technical ability) to our Facebook group.
I'm sure we can make this a great place for technology lovers.
Spread the message of ROBOT.LK buddy. We need 1000s of people discussing technical matters for free all the time.
1. Invite your friend using "Invite a friend" link on top.
2. Invite all your friends (irrespective of their technical ability) to our Facebook group.
I'm sure we can make this a great place for technology lovers.