tags:

views:

91

answers:

3

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;
            }
A: 

Why do you feel it's not letting you do this? Any error messages? Are you sure you've got two comparable situations? Windows forms behave very similarly (if not identically) between VB.NET and C#.

Rushyo
+1  A: 

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.

Mischa
+1  A: 

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.

DataDink
Oh, I don't hate you at all! :) Actually this explains alot and makes perfectly sense really. Just wondering why this is even necessary and why it isn't like VB in that way. Probably just that C# works in a different way I guess :)
Kenny Bones
But still, what would the code actually look like?I mean, do I have to create a new instance every time I need to access a control in the other form?I'll update the first post to add some code I've got
Kenny Bones
No - you just have to keep track of the "instance" of Form2 that you've already created - this is all just a bunch of OOP stuff so here are some things to google: "Class", "Class Instantiation", "Variable Scope", "Object Oriented Programming", "Inheritance", and um... well I think that should get you started. What I think you want to do is really understand the "what and why" of classes. I think that will help a bunch.
DataDink
Just FYI - in my example "window1" is an instance of "Form2" - as long as you keep some kind of reference to that - i.e. pass it around through methods or events or whatever, then you can always work with it. the "Form2" you are used to working with just has what is called a "Global Scope" that you can access from anywhere. You could potentially make "window1" have a global scope - but it's not a very good practice ... oh ya - also google "Design Patterns and Practices" to reeeeeeeeally dive into the "why" for stuff.
DataDink