Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing form objects

Accessing form objects

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cshivaprasad
    wrote on last edited by
    #1

    Hi, I am having a class Btree, its has function to print pre-order of binary tree on the form. problem is that, i am not able to access label on the form from this BTree.cs file. I made that label as public in form.cs file, even i am not able to access. can any one pls tell me, how can i access form objects from other class file.

    T 1 Reply Last reply
    0
    • C cshivaprasad

      Hi, I am having a class Btree, its has function to print pre-order of binary tree on the form. problem is that, i am not able to access label on the form from this BTree.cs file. I made that label as public in form.cs file, even i am not able to access. can any one pls tell me, how can i access form objects from other class file.

      T Offline
      T Offline
      Travis D Mathison
      wrote on last edited by
      #2

      Your binary tree class (Btree) needs to get a reference to the Form class of which the label is on. There're a few different ways to handle the interaction between the two in regards to how loosely coupled you want them to be however the easiest thing to do would be to pass in a reference to the form object through the Btree constructor...

      C 1 Reply Last reply
      0
      • T Travis D Mathison

        Your binary tree class (Btree) needs to get a reference to the Form class of which the label is on. There're a few different ways to handle the interaction between the two in regards to how loosely coupled you want them to be however the easiest thing to do would be to pass in a reference to the form object through the Btree constructor...

        C Offline
        C Offline
        cshivaprasad
        wrote on last edited by
        #3

        Hi Thanku 4 reply Can U pls give some example code. or refer me to the any tutorial.

        T 1 Reply Last reply
        0
        • C cshivaprasad

          Hi Thanku 4 reply Can U pls give some example code. or refer me to the any tutorial.

          T Offline
          T Offline
          Travis D Mathison
          wrote on last edited by
          #4

          Sure, so if we had a label on the form that we wanted to update by code in some other class you would do the following: In your form code you would expose the label as a property...

          delegate void Func();
          ...
          public string MessageText
          {
          get
          {
          return label1.Text;
          }
          set
          {
          Func del = delegate
          {
          label1.Text = value;
          };

              Invoke(del);
          }
          

          }

          Then we can create a new instance of the Btree class (in this case I'll just create it from the Load method of the form)

          Btree myBtree = new Btree(this);

          "this" refers to the Form class instance itself.. so for the constructor of the Btree class I'd have

          public Btree(Form1 frm)
          {
          frm.MessageText = "This is a test";
          }

          Since you have a reference to Form1 you can get/set properties and call methods on it. It is, however, better practice to create an interface that states what properties/methods the form should support of which other classes would make calls to so you dont tie yourself down to specific class/form names... but I dont think that would be a big concern for you at this point in time :P

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups