Welcome to Dream.In.Code
Become a C# Expert!

Join 150,411 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 973 people online right now. Registration is fast and FREE... Join Now!




TextBoxes cross Class

 
Reply to this topicStart new topic

TextBoxes cross Class

Vaune
25 Mar, 2008 - 10:22 AM
Post #1

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 81



Thanked: 2 times
My Contributions
Hello all,

I'm new here and this would be my first post! I'm quite the beginner at C# programming, so I'm sorry if this is a no-brainer. Also I searched and couldn't find any relavent material, if a post exists that solves this problem, feel free to direct me there and close this thread.

I'm making a grading program that takes X number of scores, averages them, then finds the equivilent letter grade. I have everythingworking except the empty text box checker. I use a seperate class for the bulk of my error checking (entitled Class1.cs). In said class I have a method that checks the Form1 textboxes for empty values. So far I've tried:

CODE

public void emptyTextMethod()
    {
        if (Form1.txtName.Text==String.Empty)
        {
MessageBox.Show("Please enter a name.");
        }//end if
    }//end method


Thats really the only way I know how to call it. However, when I attempt that code, it states "that an object is required for the nonstatic field, method, or property." I'm not sure what this error is and have searched google for an answer, but found nothing that applies to the code I'm working on.

I've also tried declaring Form1 in Class1 and calling the Textbox by name, ie:
CODE

Form1 Form1Object=new Form1();
....

if (Form1Object.txtName.Text==String.Empty)
{
MessageBox.Show("Please enter a name.");
}//end if


However I get a Stack Overflow Exception and don't know how to solve it. Any tips would be greatly appreciated!

~Vaune
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: TextBoxes Cross Class
25 Mar, 2008 - 10:30 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Why not just use a RequiredFieldValidator?
User is offlineProfile CardPM
+Quote Post

Vaune
RE: TextBoxes Cross Class
25 Mar, 2008 - 10:40 AM
Post #3

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 81



Thanked: 2 times
My Contributions
Because I don't know how, and this is the way my teacher specified he wanted it done, sorry should have mentioned that in the OP.

This post has been edited by Vaune: 25 Mar, 2008 - 10:40 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: TextBoxes Cross Class
25 Mar, 2008 - 10:54 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I would keep the Form completely out of the equation, doing this will make the class reusable for any Form or TextBox you wish to validate. I would make a generic method, which you pass a reference to the TextBox to it. Something like this


csharp

public bool emptyTextMethod(TextBox name)
{
//check the value of the textbox
if (name.Text == string.Empty)
{
//it is empty so return true
return true;
}
else
{
//otherwise return false
return false;
}
}//end method



Then when doing your Form validation you could use it like so


csharp

if(emptyTextMethod(NameTextBox))
{
MessageBox.Show("Please enter a name.");
NameTextBox.Focus();
}



Since you are doing this in a separate class file, you will either need a reference to the System.Windows.Forms Namespace, or in the signature of the method use System.Windows.Forms.TextBox instead of TextBox.

Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

Vaune
RE: TextBoxes Cross Class
25 Mar, 2008 - 11:11 AM
Post #5

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 81



Thanked: 2 times
My Contributions
That answers my problem exactly! Thanks to you PsychoCoder, I got it working and my project is complete! Muchos gracias senior!

**Just editing to reiterate how thankful I am. That had been bothering me all day and I couldn't figure it out! Thanks for the quick, clear, and very helpful response Pycho.**

~Vaune

This post has been edited by Vaune: 25 Mar, 2008 - 11:29 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: TextBoxes Cross Class
25 Mar, 2008 - 11:27 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
No problem, glad I could help. Now in the words of our fearless leader, tell all your friends about us here smile.gif
User is offlineProfile CardPM
+Quote Post

Vaune
RE: TextBoxes Cross Class
25 Mar, 2008 - 11:30 AM
Post #7

D.I.C Head
**

Joined: 25 Mar, 2008
Posts: 81



Thanked: 2 times
My Contributions
Will do! biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:41PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month