1.11M Members

Get controls and code from different forum

CriticalError
Deleted Member
 
0
 

So I been stuck with this problem for quite a few days. Since I used VB.net as my first language C# is quite difficult for me.

In VB.net when you needed to refer to a different control on a different forum you would write something like Form1.Mainmenu etc

In C# you write, I don't know!:'(

So here is the problem. I have made a text-editor with one form (called Editor). Now I want to make the second form which will be the options form. (called Options)

From the Options form I want to be able to access the controls on Editor and change their backcolor text, and other things etc. I also want to be able use code from Editor.

Now here is the code I want. I made a method called Void InstalledFonts()

void InstalledFonts()
        {

            InstalledFontCollection fonts = new InstalledFontCollection();

            for (int i = 0; i < fonts.Families.Length; i++)
            {
                fontSelection.Items.Add(fonts.Families[i].Name);
            }

        
        }

(OK so for those who don't know what this code is doing it basically get a list of system fonts and putting them in to a comboBox when the program loads. )

I want to use this same code in the Options form as well. How can I refer to it? I don't want to retype it, I heard cut and paste is the sworn enemy of a programmer :icon_cheesygrin:

I hope someone understands and can help me out here.

 
0
 

Having one form directly change the appearance of another form is bad OO design. It couples the forms tightly. You should pass back information to the form so it can change itself.

That said, by default C# controls are private. If you click on the control and look in the Design section of the properties you'll see a 'Modifier' parameter set to 'private'. You can make this 'public' and it will act like the VB form. This, too, is bad design as you should make properties to access the controls :)

As for the repeated code, create a helper class to hold the methods. Call the methods to get the data you need.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article