Solved

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

Posted on 2004-10-22
261 Views
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

Thanks

Steve
0
Question by:stevetheski
    4 Comments
     
    LVL 8

    Expert Comment

    by:tomvergote
    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.
    0
     
    LVL 4

    Author Comment

    by:stevetheski
    basically i dont get the intellisense option for TextBox1 when i am editing class1 when i type
    MyNameSpace.Form1.
    0
     
    LVL 8

    Expert Comment

    by:AaronReams
    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;
               myClass1.AccessForm1TextBox();
         }      
    }

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

    Accepted Solution

    by:
    That should hopefully give you the basic idea.

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

    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