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. How to access a control on a form from another class?

How to access a control on a form from another class?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
2 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.
  • D Offline
    D Offline
    DTWC_Lawrence
    wrote on last edited by
    #1

    C# newbie looking for a simple way to access a control on a form, a label or progress bar, from another class. If not a simple, a difficult one, or even the correct one will suffice. BBB

    H 1 Reply Last reply
    0
    • D DTWC_Lawrence

      C# newbie looking for a simple way to access a control on a form, a label or progress bar, from another class. If not a simple, a difficult one, or even the correct one will suffice. BBB

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      It all comes down to references and access. One class must first have a reference to another. So, your form that wants to access a control on another form must have a reference to that form. You could pass a reference as a property, for example. Second, that control has to be accessible. By default, the Windows Forms designer makes all controls you drag-and-drop onto the designer private, so other classes - even derivative classes - can't access the field. You can either change the accecss modifier (you can do this both in the PropertyGrid from the designer or in the code file). You could, however, enumerate the form's Controls collection and find the control by name, which would be accessible (just because the control's field is private doesn't mean the control itself is private - only the field that holds the reference to it is). If these are parent/child forms, then you can use the Parent property, for example, of the child form, but you must make sure to cast it to the parent form's type in order to access fields by name. An example follows:

      using System;
      using System.Drawing;
      using System.Windows.Forms;
       
      class ParentForm : Form // Default access for class is internal
      {
      static void Main()
      {
      Application.Run(new ParentForm());
      }
       
      Button openChild; // Default access is private for class members
      internal TextBox childText; // Make accessible to child form in this assembly
      internal ParentForm()
      {
      Text = "Example: Parent Form";
       
      openChild = new Button();
      Controls.Add(openChild);
      openChild.Location = new Point(8, 8);
      openChild.Text = "Open Child";
      openChild.Click += new EventHandler(openChild_Click);
       
      childText = new TextBox();
      Controls.Add(childText);
      childText.Location = new Point(8, openChild.Bottom + 8);
      childText.ReadOnly = true;
      }
       
      void openChild_Click(object sender, EventArgs e)
      {
      using (ChildForm form = new ChildForm())
      form.ShowDialog(this);
      }
      }
       
      class ChildForm : Form
      {
      TextBox myText;
      internal ChildForm()
      {
      Text = "Example: Child Form";
       
      myText = new TextBox();
      Controls.Add(myText);
      myText.Location = new Point(8, 8);
      myText.TextChanged += new EventHandler(myText_TextChanged);
      }
       
      void myText_TextChanged(object sender, EventArgs e)
      {
      ParentForm parent = Owner as ParentForm;
      if (parent != null)
      parent.childText.Text = myText.Text;
      }
      }

      You should a

      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