We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,866 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Get controls and code from different forum

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.

2
Contributors
1
Reply
55 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
CriticalError
Junior Poster in Training
64 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Momerath
Senior Poster
3,728 posts since Aug 2010
Reputation Points: 1,322
Solved Threads: 624
Skill Endorsements: 12

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0669 seconds using 2.64MB