300 point EASY C# Question Get form field Value in different class

Posted on 2004-10-22
Last Modified: 2010-04-17
This has to be an easy question but i just cant find the answer.
I have a sample windows app in C#
namespace MyNameSpace
Formname = Form1
codebehind = Form1.cs
extra ClassName = Class1.cs

now Form1 has a textbox on it named Text1

and i want the value to be accessible in Class1

How would i go about it.  I tried making the textbox Public but i get an error about a static


Question by:stevetheski
    LVL 8

    Expert Comment

    what's the exact error or code?

    it's kind of hard to guess what's going on at this point. either you are calling static stuff where you can't or something.
    If you want to make variables available to other classes it's best to add a property for it.

    public property myprop
       get{return myTextBox.Text}

    or something along those lines.
    LVL 4

    Author Comment

    basically i dont get the intellisense option for TextBox1 when i am editing class1 when i type
    LVL 8

    Expert Comment

    You need to do two things here:

    First you need to make the textbox public inside of your form class.  (This will make it show up in intellisense)

    ex. public System.Windows.Forms.TextBox Text1;

    The static error is probably because you are trying to access the textbox without having a reference to a Form1 object.  
    (ex. Form1.Text1.Text will generate an error because there isn't an instance of the Form1 object)

    You need to pass a reference of the form object to your class;

    For example:

    class Class1
        public Form1 myFormReference = null;
        public void AccessForm1TextBox()
              if(myFormReference != null)
                      myFormReference.Text1.Text = "Hello";

    class Form1
         public System.Windows.Forms.TextBox Text1;
         public CreateClass1Object()
               Class1 myClass1 = new Class1();
               myClass1.myFormReference = this;

    Hope this helps clear things up.  Good luck -Aaron
    LVL 8

    Accepted Solution

    That should hopefully give you the basic idea.

    "public CreateClass1Object()"  should be "public void CreateClass1Object()" in the Form1 class definition.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    Assistance with an AppInventor question 3 60
    code issue 8 57
    pre4 challenge 19 56
    wordsWithoutList  challenge 24 40
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    This article will show, step by step, how to integrate R code into a R Sweave document
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now