[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-22
4
Medium Priority
?
315 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
Comment
Question by:stevetheski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:tomvergote
ID: 12382337
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
ID: 12383266
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
ID: 12384054
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:
AaronReams earned 1200 total points
ID: 12384075
That should hopefully give you the basic idea.

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Progress

650 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