C# - How to access a form from another form?

.NET programming topics
Post Reply
Fox
Sergeant
Sergeant
Posts: 11
Joined: Fri Jul 22, 2011 9:22 am

C# - How to access a form from another form?

Post by Fox » Fri Jul 22, 2011 10:36 am

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.
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: C# - How to access a form from another form?

Post by Saman » Fri Jul 22, 2011 12:08 pm

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.
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: C# - How to access a form from another form?

Post by Enigma » Fri Jul 22, 2011 12:31 pm

Hi
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.");
        }
      }
}
and here goes the Form2 code

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();
        }
    }
}
You can use the tag property to assign any type of object.
Hope this will help you.

Thanks
Fox
Sergeant
Sergeant
Posts: 11
Joined: Fri Jul 22, 2011 9:22 am

Re: C# - How to access a form from another form?

Post by Fox » Fri Jul 22, 2011 2:47 pm

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.
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: C# - How to access a form from another form?

Post by Enigma » Fri Jul 22, 2011 3:26 pm

Hi 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?
yes. This correct
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.
No. In your case it should be

Code: Select all

int val = Convert.ToInt32(this.Tag);
//or you can use 
int val = (int)this.Tag;
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
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: C# - How to access a form from another form?

Post by Saman » Fri Jul 22, 2011 3:30 pm

You create Form2 from Form1. So you have coding as below.

Code: Select all

Form2 frm2 = new Form2();
Then you set Form2's Tag to hold the Form1 object ('this' refers to current object (opened window) of Form1 Class).

Code: Select all

frm2.Tag = this;
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.

Code: Select all

Form1 frm1 = this.Tag as Form1;
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.

Code: Select all

Form1 frm1 = (Form1) this.Tag;
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.

Code: Select all

frm1.trackbar
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?
P.S. Can somebody tell me how to use the syntax highlighting here?
Go to Portal >> BBCode FAQ
Fox
Sergeant
Sergeant
Posts: 11
Joined: Fri Jul 22, 2011 9:22 am

Re: C# - How to access a form from another form?

Post by Fox » Mon Jul 25, 2011 6:48 pm

Thanks everybody! I finally got it working.
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: C# - How to access a form from another form?

Post by Saman » Mon Jul 25, 2011 6:57 pm

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.
Post Reply

Return to “.NET Programming”