Kenny Bones
Kenny Bones

Reputation: 5139

Access/Set controls on another form

I'm not good at C# at all, I just don't get the logics. But VB I seem to understand alot better since it seems much more logical. Atleast to me.

So I'm run into something which isn't a problem at all in VB, accessing controls on a different form then the one you're currently in.

In VB, if I want to set the state of a button say, in Form2. I just type the following:

Form2.Button1.Text = "Text"

In C# I cannot seem to do this. Why? There must be a pretty good reason for this right?

Edit: So if I have this code, what would it look like to be able to access controls on the other form?

if (!AsioOut.isSupported())
            {
                SoundProperties.radioButtonAsio.Enabled = false;
                SoundProperties.buttonControlPanel.Enabled = false;
                SoundProperties.comboBoxAsioDriver.Enabled = false;
            }
            else
            {
                // Just fill the comboBox AsioDriver with available driver names
                String[] asioDriverNames = AsioOut.GetDriverNames();
                foreach (string driverName in asioDriverNames)
                {

                    SoundProperties.comboBoxAsioDriver.Items.Add(driverName);
                }

                SoundProperties.comboBoxAsioDriver.SelectedIndex = 0;
            }

Just tried to add this "SoundProperties SoundProperties = new SoundProperties(); And I do get access to the controls. But do I need to add this bit of code in both parts of this IF-statement? Seems like I do, but still, adding that line to the last part of this code doesn't do anything ang gives me the error message:

"A local variable named 'SoundProperties' cannot be declared in this scope because it would give a different meaning to 'SoundProperties', which is already used in a 'child' scope to denote something else"

Removing the line gives me the following error:

"An object reference is required for the non-static field, method, or property 'NAudio.SoundProperties.comboBoxAsioDriver'"

Here's the code after adding these lines in two places:

 if (!AsioOut.isSupported())
            {
                SoundProperties SoundProperties = new SoundProperties();
                SoundProperties.radioButtonAsio.Enabled = false;
                SoundProperties.buttonControlPanel.Enabled = false;
                SoundProperties.comboBoxAsioDriver.Enabled = false;
            }
            else
            {
                // Just fill the comboBox AsioDriver with available driver names
                String[] asioDriverNames = AsioOut.GetDriverNames();
                foreach (string driverName in asioDriverNames)
                {
                    SoundProperties SoundProperties = new SoundProperties();
                    SoundProperties.comboBoxAsioDriver.Items.Add(driverName);
                }

                SoundProperties SoundProperties = new SoundProperties();
                SoundProperties.comboBoxAsioDriver.SelectedIndex = 0;
            }

Upvotes: 0

Views: 2540

Answers (2)

DataDink
DataDink

Reputation: 824

Please don't hate me for saying this - but I think this is an issue that I've seen a lot of VB coders run into.

VB allows you to not deal with classes if you don't want to. When in C# you are adding a form to your project - visual studio is just making a class file for you that inherits from "Form".

In C# you have to actually instantiate this into an object and then work with that object. VB allows you to just access the class as if it was already instantiated - but it is actually just making a new "Form2" for you.

In vb if you want to have more than 1 actual "Form2" you would say something like this...

Dim window1 as new Form2()
Dim window2 as new Form2()

window1.Show()
window2.Show()

Now you will have two copies of "Form2" on your screen when you run this. The difference between VB and C# is you also need to actually make(instantiate) your first copy of Form2 - C# will not do it for you.

Now to answer your question:

Once you have an actual object that has been instantiated - you need to actually make "Button1" public instead of private.

To do this - on Form2 - select Button1 and look at the properties... Find the "Modifiers" property and set this to public.

You will now be able to see "Button1" on both window1 and window2.

Hope that helped.

Upvotes: 4

Mischa
Mischa

Reputation: 901

You can access another form in c# too. But you need a reference to the Form instance you want to interact with.

So you have to hold the variable of the 2nd Form instance and access it via this.

E.g.: From the code of the first form call:

Form2 my2ndForm = new Form2();
my2ndForm.Button1.Text = "Text";

Be sure to set the access modifier of the Button1 to public or internal.

Upvotes: 2

Related Questions