Click here to Skip to main content
15,914,070 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to hide a form at the program startup? Pin
Alex Korchemniy11-Oct-04 16:35
Alex Korchemniy11-Oct-04 16:35 
GeneralHelp needed..Urgent.....using CreateBitmap function in c# Pin
Kiran Satish11-Oct-04 12:26
Kiran Satish11-Oct-04 12:26 
GeneralMSMQ over HTTP Pin
francis10011-Oct-04 11:02
francis10011-Oct-04 11:02 
QuestionCan you safely start a form on a new thread? Pin
eyoung7011-Oct-04 10:59
eyoung7011-Oct-04 10:59 
AnswerRe: Can you safely start a form on a new thread? Pin
eyoung7011-Oct-04 11:16
eyoung7011-Oct-04 11:16 
GeneralRe: Can you safely start a form on a new thread? Pin
Dave Kreskowiak11-Oct-04 15:30
mveDave Kreskowiak11-Oct-04 15:30 
QuestionHow to access a control on a form from another class? Pin
DTWC_Lawrence11-Oct-04 10:20
DTWC_Lawrence11-Oct-04 10:20 
AnswerRe: How to access a control on a form from another class? Pin
Heath Stewart11-Oct-04 10:52
protectorHeath Stewart11-Oct-04 10:52 
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 also read Access Modifiers[^] in the C# programmers reference.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles] [My Blog]
GeneralProblem reading Files Pin
ee9903511-Oct-04 8:24
ee9903511-Oct-04 8:24 
GeneralRe: Problem reading Files Pin
Mike Dimmick11-Oct-04 10:15
Mike Dimmick11-Oct-04 10:15 
GeneralTrying to copy a datatable... Pin
Carl Mercier11-Oct-04 7:54
Carl Mercier11-Oct-04 7:54 
GeneralRe: Trying to copy a datatable... Pin
Anonymous11-Oct-04 8:08
Anonymous11-Oct-04 8:08 
GeneralRe: Trying to copy a datatable... Pin
Carl Mercier11-Oct-04 8:16
Carl Mercier11-Oct-04 8:16 
GeneralSimple HTML editor for winforms Pin
Brett Slaski11-Oct-04 7:20
Brett Slaski11-Oct-04 7:20 
GeneralRe: Simple HTML editor for winforms Pin
Jacob Hammack11-Oct-04 10:53
Jacob Hammack11-Oct-04 10:53 
GeneralRe: Simple HTML editor for winforms Pin
Brett Slaski11-Oct-04 15:59
Brett Slaski11-Oct-04 15:59 
GeneralTime entry 'lost' in dtp Pin
DF_Bandit11-Oct-04 7:18
DF_Bandit11-Oct-04 7:18 
GeneralRe: Time entry 'lost' in dtp Pin
DF_Bandit11-Oct-04 12:59
DF_Bandit11-Oct-04 12:59 
GeneralTime stamp Pin
wk_vigorous11-Oct-04 6:51
wk_vigorous11-Oct-04 6:51 
GeneralRe: Time stamp Pin
Heath Stewart11-Oct-04 10:56
protectorHeath Stewart11-Oct-04 10:56 
GeneralHashtable Item Pin
3green11-Oct-04 5:32
3green11-Oct-04 5:32 
GeneralRe: Hashtable Item Pin
Brian Nottingham11-Oct-04 6:07
Brian Nottingham11-Oct-04 6:07 
GeneralRe: Hashtable Item Pin
Charlie Williams11-Oct-04 6:11
Charlie Williams11-Oct-04 6:11 
GeneralMaking function DragDrop to a treeNode Pin
ee9903511-Oct-04 5:22
ee9903511-Oct-04 5:22 
GeneralRe: Making function DragDrop to a treeNode Pin
Heath Stewart11-Oct-04 10:35
protectorHeath Stewart11-Oct-04 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.