CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    Accessing Components in a Class

    C# 2008 Forms

    I got a bunch of components in the Form.cs > Form.Designer.cs like Labels and such that I want to manipulate from a different class.

    how do I access them?

    label1.Text....

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Accessing Components in a Class

    You should expose properties that allow external classes to modify UI appearance and not give them direct access to the controls.

    Code:
    class ChildForm : Form
    {
         public string LabelText
         {
              get { return myLabel.Text; }
              set { myLabel.Text = value; }
         }
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: Accessing Components in a Class

    That's nifty, and I have forgotten that I see those all the time. Only problem is I get an Object ref required for non-static field in my ClassBox class when using StrText.

    If I go back to my Form class and make

    public string StrText

    this

    public static string StrText

    I get Object ref required for non-static field in my form class. Dammed if you do dammed if you don't situation.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Accessing Components in a Class

    I think that you are trying to do this:

    Code:
     ClassBox.LabelText = "some text";
    That would be calling a static method of the ClassBox class. When you made the property static you got another error because the label is not static. You need to do this when you show the dialog:

    Code:
    ClassBox cb = new ClassBox();
    cb.LabelText = "Some text";
    BTW, the Form class implements IDisposabl, so you should call the Dispose() method after you are done with it. An easy way to do this is by using the "using" keyword:

    Code:
    using (ClassBox cb = new ClassBox)
    {
        cb.LabelText = "some text";
    }
    That is really just syntactic sugar. It compiles to this:

    Code:
    try
    {
        ClassBox cb = new ClassBox();
        cb.LabelText = "some text";
    }
    finally
    {
        cb.Dispose();
    }

  5. #5
    Join Date
    Jun 2008
    Posts
    154

    Re: Accessing Components in a Class

    hrmm... seems weird. And it looks circular. And after testing..it is. It creates an infinite loop.

    Code:
    namespace Form1
    {
        public partial class Form1: Form
        {
    
        public string StrText
        {
            get { return lbl_STRrate1.Text; }
            set { lbl_STRrate1.Text = value; }
        }
    and then

    Code:
    class ClassBox : ComboBox
        {
            Form1.Form1 formtext = new Form1.Form1();
    
            void SetCommonerStats()
            {
                int iStrength = 10 + Strength;
                string sStrength = Convert.ToString(iStrength);
                formtext.StrText = sStrength;
            }
    so instead, I am going to save a new variable in ClassBox and pass it to Form1 and have a function in Form1 that updates it stats. I think this might solve the circular problem.

  6. #6
    Join Date
    Jun 2008
    Posts
    154

    Re: Accessing Components in a Class

    I just realized I need to completely reorganize the code. I don't need a ClassBox : ComboBox. Instead I need a ClassStatistics Struct. And just have the Form1 access the struct to manipulate a default combobox.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Accessing Components in a Class

    There is no reason that you should get an infinite loop. You must be doing something else in another area which is causing it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured