?
Solved

How to accept input from a textBox on a .Net Form Application

Posted on 2006-04-27
9
Medium Priority
?
339 Views
Last Modified: 2010-04-24
Greetings:

I have a Form application and I need to a accept input from a text box and store it in an associated variable so that I can pass the variable throughout the application.

My questions are:

1)  How do I initialize the text box so that it is empty upon launch or so that it doesn't show 'textBox1' ?
2)  When the user types in the appropriate text, how does that text get assigned to a string variable  ?
3)  In order to accept the text does the user need to hit the 'Enter' key on the keyboard ?

I'm guessing the code begins with:

void Form1::textBox1_TextChanged(System::Object * sender, System::EventArgs * e)
{
      ???
}

Thanks
0
Comment
Question by:John500
  • 4
  • 4
9 Comments
 
LVL 10

Expert Comment

by:Eduard Ghergu
ID: 16561775
Hello!

1) You have the Text property in the Properties window. Just delete the displayed value.
2) You can use the TextChanged event, but I recomend you to use Leave instead, or Validating.
3) What means "accept" for you? No, it is not necessary...
0
 

Author Comment

by:John500
ID: 16564727
ghergu,

I need a little code example of how the text which is input by the user is accepted in the background.  What variable captures this input?  If I use the TextChanged() event or any other method how do I capture the input ?

Can you just leave me a simple code example and I'll close this question out.  I'm sure it can't be difficult.  I also think I prefer TextChanged() because I can see this declaration is ready for code - I just need to put something in it that works.  That's where you come in.

Thanks
0
 

Author Comment

by:John500
ID: 16595243
ghergu,

Any input here ?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:Flying-Kiwi
ID: 16727186
Umm, I've never written a C++ [rogram. I've just popped in to visit form the VB.NET forum, but the principleshould be the same:

A textbox has a property, Text. So, the value in the textbox can be gotten with TextBox1.Text

Create a string variable (sorry, I don't know how in C++), say 'name' if that's what the user will enter. then assign it to the value of TextBox1.Text, as in:

name = TextBox1.Text

...and now you can use the variable name as needed.

Hope it helps.

Let's see what a flaming I get for popping in here. He he.
0
 

Expert Comment

by:Flying-Kiwi
ID: 16727191
(and sorry for all the typing errors. doh.)
0
 

Accepted Solution

by:
Flying-Kiwi earned 500 total points
ID: 16727354
I don't know why I'm back, but I'll suggest an answer for your three probs:

1) TextBox1.Text = ""     <-- this assigns no text to the text box, making it look blank.
2) As I wrote above. To declare a string use:  
string name;

3) Put a button on the form. It's default name is 'Button1'. Put the above mentioned code in the Button1_Click event (double-click on the button and it should create a  Button1_Click method). When the user clicks the button, the string variable 'name' will have the contents of the textbox assigned to it.

Button1_Click

string name;

name = TextBox1.Text

// do something with name, e.g.
MessageBox.Show("Your name is: ", name)  //Dunno if this will work in Visual C++


Good luck with Visual C++. I respect you greatly for learning it. Maybe one day for me, too though VB.NET is so nice it's hard to leave. Oops, time for me to leave too.  (#_#)

0
 

Author Comment

by:John500
ID: 16736854
Flying-Kiwi,

Thanks !

I really should have known this.  Once you said what you did, I realized immediately.  I think the mind-block was that I expected the data had to be 'entered' first.  I forgot that by adding a button I could accomplish the same.

Incidently, this button is labeled 'OK'.  I noticed that it is only activated by selecting it with the mouse.  Shouldn't there be a property that allows the keyboard 'Enter' key to work also ?

Thanks again

0
 

Expert Comment

by:Flying-Kiwi
ID: 16738565
Super glad I could help. My first points at Experts Exchange have come from a C++ forum. I'll break outthe champagne tonight.  lol.

For the 'Enter' key to click the button, it needs to 'have focus'. For example, you may want to check the tab order so the user can enter the info, hit the tab key once - the Okay button now has focus - then hit the Enter key to trigger the button's click event.

You can change the tab order of controls in the IDE properties window: Look for TabIndex or something similar (could be ZOrder).

Good luck!
0
 

Author Comment

by:John500
ID: 16744305
... looks like the tab order was already correct.  I just needed to use the tab key to set focus.  Thanks man ...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 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