locked
Getting input from text boxes into variables RRS feed

  • Question

  • So I'm just beginning to learn C++ using VS10 express, I just upgraded to VS10Pro (free software for students is awesome!). I've been writing console apps and everything is going fine. I decided to try to make one of my console apps into a windows app. It's pretty simple, it takes a few integers from the user, performs a few calculations on them, then gives a few integers as output. I have successfuly written it as a console app, now I want to try and mae a windows version.

    So I've created a form that has a few text boxes for the user to input information, a few text boxes where I want the results displayed, and a button that I would like to use to begin the calculation once it is clicked. I have no idea how to get the user input from the text boxes and get it into the appropriate variables.

    I have a class called Property, it stores the name a property, the monthly income from the property, how many units of the proerty are owned, and has the appropriate functions to access and modify member variables. Suppose I have an instance of a Property called House, and the number of units owned is stored in a variable House.itsUnitsOwned. Now I have a from with a textbox called HouseUnitsOwned and I want to get the value in this textbox into the variable House.itsUnitsOwned. In the console version I would get an int from the user, say unitsOwned then assign the value of unitsOwned to House.itsUnitsOwned by House.setUnitsOwned(unitsOwned).

    The class is defined in property.h, VC++ created Form1.h for me and I've given it the appearance I want but no functionality, and the main code is in propertyIncome.cpp.

    I have looked and cannot find up to date information on how to do this in VC++10 on the web. I figured by trying to write simple windows apps it would help me learn both C++ and windows programming at the same time, and they don't get much simpler than this. Any help would be greatly appreciated.

    Thanks

    D

    Wednesday, December 8, 2010 4:39 AM

Answers

  • When myButton is pressed what I want to do is read the user inputted integer from myTextBox1 and assign the value to a local integer. I actually want to do more than that, but for now this is what I need to do.
    Did you read my previous response (not the last one, the one before that)?
     
    GetDlgItemInt() is for Win32 -- you are using Winforms.
     
    In Winforms, in your button handler, do
     
    int myInt = Int32::Parse(myTextBox1->Text);
     
    You sound as if you might need a book. A good introductory book on both standard C++ and C++/CLI (both Console and GUI) is Ivor Horton's "Beginning Visual C++ 2010".
     
    By the way, if you write your GUI using C++/CLI then you are using managed code (like C#). If you are concerned about the performance of non-GUI parts of your application, then you can write these parts in native C++, and build a managed C++/CLI class Library to access this code. Then you can write the GUI in C#.
     
    If you want to stick entirely with native C++, the most common way to write Windows programs is using MFC, which is a native class library that wraps the Win32 API. There are also non-Microsoft-supported frameworks (e.g. WTL, Qt and wxwidgets).
     
    Seec this trhread for a recent discussion of the state of C++ within Microsoft:
     
    http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/49d091cf-884a-46ed-8179-3ae8b691b7fa
     

    David Wilkinson | Visual C++ MVP
    Thursday, December 9, 2010 8:38 PM
  • One last question, if after some computation I have __int64 myInt, how do I get this to diplay in myTextBox?
    You can use Int64::ToString() and the Text property of your text box.
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by DanInSauga Thursday, December 16, 2010 5:44 PM
    Thursday, December 16, 2010 12:43 PM
  • You should be aware that the code you're working with is not C++, it is C++/CLI. If you have intentions of using C++, you need to use a Win32 project template, not a CLR project template.

    Thursday, December 9, 2010 8:34 PM

All replies

  • For bare bones Win32 APIs: if you have the handle to the text box (HWND) you can call GetWindowText(). If you have the window ID, then you can call one of the GetDlgItem* functions.
    Microsoft Test - http://tester.poleyland.com/
    Wednesday, December 8, 2010 4:56 AM
  • GIven that this is the first windows app I've tried writing when I start a project which would be the wiser choice Win32 or Windows Forms app? Does the answer to this change the answer to my first question?

     

    D

    Wednesday, December 8, 2010 6:50 AM
  • GIven that this is the first windows app I've tried writing when I start a project which would be the wiser choice Win32 or Windows Forms app? Does the answer to this change the answer to my first question?
    Win32 Windows application and Windows forms are completely different GUI methodologies (though the latter uses the former under the hood).
     
    Win32 application is written in standard C++ (or in C). The easiset way to get an integer variable from a control is using ::GetDlgItemInt().
     
    Windows forms applications are written in C++/CLI. To get an intger from a control you can do
     
    int var = Int32::Parse(control->Text);
     

    David Wilkinson | Visual C++ MVP
    Wednesday, December 8, 2010 10:58 AM
  • Thanks for the replies, I have a few more questions.

    I am doing this as a Windows Forms project and it's very simple. The app is just one form, a couple text boxes for input, a couple text boxes for output, and a button that will execute the code that performs calculations on the inputs when pressed.

    1) If my form, say myForm.h,  is going to display variables stored in an instance of a class I have created, do I add "#include "myClass.h" in myForm.h?

    2) Which file does to code for the functionality of my app go, myForm.h?

    3) If I have a buttom, myButton, on myForm where do I place the code to be executed when myButton is clicked?

    On a different note, what tools are used to design a form in a Win32 app? When I tried starting a Win32 project and adding a form the project was converted to a Windows Forms project. Is there somewhere else I should be looking, or would the form be created by coding it and not by a graphical tool?

    Finally given I'm just learning C++ should I even bother trying to learn Windows programming at the same time or should I just stick to console apps until I am comfortable with the language?

    I'm a complete beginner, so I appreciate your patience when helping me!

     

    Thanks

    D

    Wednesday, December 8, 2010 7:35 PM
  • I've figured out (1) and (3) in the above, I am still stuck on (2) though.

    Suppose I have a function void doStuff() that I want to be able to call from myForm. In which file do I place the doStuff prototype and code?

    Thanks

    D

    PS

    Im having such a hard time finding beginner level information. One more question, so I'm writing the code executed when  myButton is clicked. Can anyone translate my pseudocode into real code for me? I would really appreciate it.

    private: System::Void myButtonn_Click(System::Object^ sender, System::EventArgs^ e) 
    {
          .. begin pseudocode..
           int myInt = 0;
           set myInt = (value in textBox1);
           set (value in textBox2) = myInt;
          .. end pseudocode
    }

    this is in my myForm.h file.

     

    D

     

    Wednesday, December 8, 2010 9:56 PM
  • I've figured out (1) and (3) in the above, I am still stuck on (2) though.
     
    Suppose I have a function void doStuff() that I want to be able to call from myForm. In which file do I place the doStuff prototype and code?
    I assume you are going the Windows Forms route...
     
    What is doStuff()? Is it a method on some other (non-GUI) class, OtherClass say? If so, you should put the class definition in OtherClass.h and the implementation in OtherClass.cpp (the usual C++ practice), and #include OtherClass.h in myForm.h (or perhaps in myForm.cpp if you have separated the myForm implementation from its definition). You will also need an instance of OtherClass on which to call the method (unless it is a static method).
     
    One big problem with Windows Forms in C++/CLI is that the wizard puts all the code in the header file, which is not the usual C++ paradigm. Sometimes (perhaps not here) it is necessary to rearrange the wizard code in order to avoid circular dependencies. This is one reason that doing WinForms in C# is an easier (and often better) choice.
     

    David Wilkinson | Visual C++ MVP
    Wednesday, December 8, 2010 11:24 PM
  • Thanks for the input, it helped me understand how to solve a few problems. HAving all the code in the header file was confusing me because it is not consistint with the standard paractice.

    I really need to learn specifically c++ and eventually fortran for the field I want to enter. C# is not used in the field as far as I know, from what I've been told it does not have the performance needed for really high end scientific computing and mathematical modeling.

    I have resolved all my problems except one. I need to be able to read the value of a textbox into a variable in my class. 

    Visual studio has generated a new c++ windows forms app for me. It is called myApp, the generated form is called Form1. On Form1 I have a textbox called myTextBox1 and a button called myButton. 

    When myButton is pressed what I want to do is read the user inputted integer from myTextBox1 and assign the value to a local integer. I actually want to do more than that, but for now this is what I need to do.

    I tried using GetDlgItemInt, but I have no idea about the syntax and I got undeclared identifier compile errors.

    Here is the code this far

    private: System::Void myButton_Click(System::Object^ sender, System::EventArgs^ e) 
    		 {
    			
    		 int myInt = 0;
    		 //Insert code to read the integer from myTextBox1 here and assign this value to myInt
    		 
    
    			
    		 }

    Can you please give me the exact line of code that should replace the comment "//Insert code to...". Once  I see the proper syntax to use GetDlgItemInt then I will be able to continue and experiment doing other things.

    Thanks

    Thursday, December 9, 2010 8:08 PM
  • You should be aware that the code you're working with is not C++, it is C++/CLI. If you have intentions of using C++, you need to use a Win32 project template, not a CLR project template.

    Thursday, December 9, 2010 8:34 PM
  • When myButton is pressed what I want to do is read the user inputted integer from myTextBox1 and assign the value to a local integer. I actually want to do more than that, but for now this is what I need to do.
    Did you read my previous response (not the last one, the one before that)?
     
    GetDlgItemInt() is for Win32 -- you are using Winforms.
     
    In Winforms, in your button handler, do
     
    int myInt = Int32::Parse(myTextBox1->Text);
     
    You sound as if you might need a book. A good introductory book on both standard C++ and C++/CLI (both Console and GUI) is Ivor Horton's "Beginning Visual C++ 2010".
     
    By the way, if you write your GUI using C++/CLI then you are using managed code (like C#). If you are concerned about the performance of non-GUI parts of your application, then you can write these parts in native C++, and build a managed C++/CLI class Library to access this code. Then you can write the GUI in C#.
     
    If you want to stick entirely with native C++, the most common way to write Windows programs is using MFC, which is a native class library that wraps the Win32 API. There are also non-Microsoft-supported frameworks (e.g. WTL, Qt and wxwidgets).
     
    Seec this trhread for a recent discussion of the state of C++ within Microsoft:
     
    http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/49d091cf-884a-46ed-8179-3ae8b691b7fa
     

    David Wilkinson | Visual C++ MVP
    Thursday, December 9, 2010 8:38 PM
  •  

    Hi DanInSauga,

     

    I'm very interested in hearing more about your issue, did you solve your problem?

    If so please share the solution with us and mark the right answers.

    If not please let me know and I will tray to help you find further solutions.

     

    Have a nice day!

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 13, 2010 2:48 AM
    Moderator
  • Thanks so much!

    Right now performance isn't an issue, but it will be in the future. I have a pure math background, I've never thought of doing anything applied until recently, hence the need to learn programming. At some point once my programming is at a high enough level to implement the models I'm interested in, then performance will become the incredibly important, so that's why I want to stick wih learning C++ primarily. Others have suggested to do exactly as you said write the computational parts in C++ and the front end in whatever is most convenient, C# or VB.net. The eventual plan is to have nice windows front end so the user can set the models parameters, then have it pass the settings to the computational parts. This is a few years away still.

    I havn't had a chance to look at C# yet, I assumes its fairly similar to C++ and will look at it soon. Does Hortons book assume a knowledge of C++ or does it cover both the language and Windows programming at the same time?

    Can Visual Studio be used for Fortran?

    One last question, if after some computation I have __int64 myInt, how do I get this to diplay in myTextBox?

    Thanks for the help!

    D

    Thursday, December 16, 2010 5:17 AM
  • Jesse,

    I'm sure my issue is trivial to anyone with even a weeks experience with Windows Forms, so I have to say I'm really impressed that there are people here willing to take the time to help without telling me to google it or some some other brush off.

    I have it solved, sort of. I now know how to do exactly one thing, to take the value in a text box and place it into a variable. Actually I figured out a little more, I've added my class and figured out how to create an instance of it and call its methods when this or that button is pressed. The only thing left to figure out is how to get the results of my calculation to display in another text box. My class returns a pointer to a dynamically sized array, I need to get the value in this array into my text boxes.

    As a beginner I'm actually amazed at just how easy the basics have been made. I remember when I bought visual c++ 1.0 when I was around 14 and only knew qbasic. I had some idea in my head that I could drag and drop an application into existence. At that time it came with a stack of books at least 8 inches thick and MFC was being pushed very hard in them. Trying to make the leap from qbasic to MFC 1.0 and C++ was too much at the time, just looking that the books it came with was enough to put me off learning for years. In hind sight it was a huge mistake to not stick it out. Resources like this were not around then either.

    Just thought mention that I appreciate the help.

    Thanks,

    D

    Thursday, December 16, 2010 6:27 AM
  • One last question, if after some computation I have __int64 myInt, how do I get this to diplay in myTextBox?
    You can use Int64::ToString() and the Text property of your text box.
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by DanInSauga Thursday, December 16, 2010 5:44 PM
    Thursday, December 16, 2010 12:43 PM
  • You can use Int64::ToString() and the Text property of your text box.
     

     

    I figured this out about five minutes fter I posted it, thanks though. On an unrelated note, I got used to intellisense when using C++, now I miss it in C++\CLI, its a great aid for learning. I suppose I could download VS2008 from dreamspark (what an awesome gift to students!). I've seen a whole lot of ranting and complaining about the issue, but nothing specific from MS. Has MS made any statement on the issue, specifically if there will be a SP for VS2010 that has intellisense for C++\CLI?

     

    Thanks


    D

    Thursday, December 16, 2010 5:49 PM
  • >I've seen a whole lot of ranting and complaining about
    >the issue, but nothing specific from MS. Has MS made
    >any statement on the issue

    See here:

    Intellisense not working for C++

    https://connect.microsoft.com/VisualStudio/feedback/details/571228/intellisense-not-working-for-c

    Excerpt:

    ---------------------------------------------
    Posted by Microsoft on 7/19/2010 at 9:44 AM

    ...

    Thank you for submitting this feedback. C++/CLI Intellisense
    is not supported in Visual Studio 2010; however, we plan on
    reinstating it in a future release of Visual Studio. We will
    be announcing our future plans on our team blog
    (http://blogs.msdn.com/vcblog/) shortly.

    Thanks,
    Mark Roberts
    Visual C++ Compiler Team
    ---------------------------------------------

    - Wayne
    Thursday, December 16, 2010 6:15 PM
  • >... specifically if there will be a SP for VS2010
    >that has intellisense for C++\CLI?

    I see no mention of that in this thread:

    VS2010 SP1 Beta: What's in It for C++ Developers

    http://blogs.msdn.com/b/vcblog/archive/2010/12/09/vs2010-sp1-beta-what-s-on-it-for-c-developers.aspx

    - Wayne
    Thursday, December 16, 2010 6:25 PM
  • So I downloaded VS2008 and for the beginning work I'm doing it is worth using an older version to have intellisense. I do prefer the look and feel of 2010 though. Are there any third party tools for VS2010 that can be used until either VS2011 is available or intellisense is added to VS2010?

    Thanks

    D

    Friday, December 17, 2010 6:36 PM
  • So I downloaded VS2008 and for the beginning work I'm doing it is worth using an older version to have intellisense. I do prefer the look and feel of 2010 though. Are there any third party tools for VS2010 that can be used until either VS2011 is available or intellisense is added to VS2010?
    I think Visual Assist X (VAX)
     
    http://www.wholetomato.com/default.asp
     
    has Intellisense support for C++/CLI in VS2010, but you cannot use VAX with the Express Edition.
     

    David Wilkinson | Visual C++ MVP
    Friday, December 17, 2010 7:01 PM