Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Reading from a textarea (http://www.programmingforums.org/showthread.php?t=7547)

groovicus Dec 12th, 2005 11:11 PM

Reading from a textarea
 
In java, when one wants to read text from a text area, they do something like
:

string[] s = textArea.getText();

I am assuming that C++ has something similar, but I sure can't find it in any of the APIs. Can someone point me in the proper direction please?

ivan Dec 13th, 2005 4:00 AM

There are some ways to do it:

:

UINT GetDlgItemText(

    HWND hDlg,                // handle of dialog box
    int nIDDlgItem,        // identifier of control
    LPTSTR lpString,        // address of buffer for text
    int nMaxCount        // maximum size of string
  );


:

int GetWindowText(

    HWND hWnd,              // handle of window or control with text
    LPTSTR lpString,        // address of buffer for text
    int nMaxCount        // maximum number of characters to copy
 
      );


:

LRESULT SendMessage(

    HWND hWnd,                            // handle of destination window
    WM_GETTEXT,                          // message to send
    (WPARAM) cchTextMax,        // number of characters to copy
  (LPARAM) lpszText                    // address of buffer for text
  );


Hope that helps.

EDIT: sorry for the tab-spaces, they got corrupted when I pasted the text

lectricpharaoh Dec 13th, 2005 6:57 AM

Quote:

Originally Posted by groovicus
In java, when one wants to read text from a text area, they do something like
:

string[] s = textArea.getText();

I am assuming that C++ has something similar, but I sure can't find it in any of the APIs. Can someone point me in the proper direction please?

If you're using some framework like .NET or MFC, there will be easy-to-use methods to get the text of pretty much any control that can have text assigned to it, including text boxes (ie edit controls), buttons, labels, etc.

If you're using the raw Win32 API, you need to realize that all the controls like buttons, scrollbars, etc are just windows. When you create one of these predefined controls, you're creating a window of the appropriate 'window class'. A window class, not to be confused with C++ classes, is a sort of 'template' from which you can create instances. Each class has its own class name (a string), and has been pre-registered by Windows for you, which explains why you don't need to write a WinProc to catch the messages aimed at the control. In fact, the WinProc for most (all?) of these controls will handle events like clicking, and translate those into WM_COMMAND messages fired back at the parent window.

Anyways, the upshot of all this is that since controls are just windows, you can use SetWindowText() and GetWindowText() to manipulate the text. On regular windows, it's the caption in the title bar, on buttons, labels (static controls in raw API terminology), etc, it's the text on the control, and for some, like scrollbars, it will be ignored. Remember, these functions will just send WM_SETTEXT or WM_GETTEXT to the target window.

groovicus Dec 13th, 2005 9:38 AM

I am using the .net framework... I'm pretty sure that once I have time to understand everything that the IDE is throwing at me, I'll be able to figure it out.

Thanks for the input. :)

lectricpharaoh Dec 14th, 2005 7:57 AM

Quote:

Originally Posted by groovicus
I am using the .net framework... I'm pretty sure that once I have time to understand everything that the IDE is throwing at me, I'll be able to figure it out.

Thanks for the input. :)

What I say below about using properties might not work in C++. I know it won't work in normal C++, but I believe you can enable managed extensions that let VC++ use properties. It should be in your compiler switches somewhere, or perhaps it's automatically set (likely if you created a .NET application/project).

Properties are essentially a hybrid between methods and attributes. They are like attributes in that you just access them like object.property or objectPtr->property (note the lack of parentheses), but they are like methods in that the return value is determined by code (giving you encapsulation). They are kinda neat, but by no means essential; the only real benefit is they can help the code look cleaner. The downside is that you can't tell from a line of code whether something is accessing a property, or a public data member.

That said, try the 'Text' property of the relevant control. Basic ones like buttons, textboxes, and labels all have it. For some of the more complex controls, like comboboxes and stuff, they either don't have it, or have it plus other properties/methods to get the additional bits of text. Usually the text is a String, but sometimes you need to convert it (like if it's an item in a listbox).

groovicus Dec 14th, 2005 10:45 AM

I have been using the pre-defined templates, and you are describing pretty much what I am seeing, only it is a horrible jumble of files... when I double click on a button in the IDE, it takes me to the prototype in the header file of the form. There is also a .cpp for the form, which I always thought the relevant code went, but the way it is presented, it appears that it wants you to put the execution code in the header ??.. anyway, it's a mess.

I think at this point I would be better off rolling my own code so at least I can understand what the heck is going on. :) But I will look to the Text properties and see what I can come up with.

lectricpharaoh Dec 15th, 2005 2:45 AM

Quote:

Originally Posted by groovicus
I have been using the pre-defined templates, and you are describing pretty much what I am seeing, only it is a horrible jumble of files... when I double click on a button in the IDE, it takes me to the prototype in the header file of the form. There is also a .cpp for the form, which I always thought the relevant code went, but the way it is presented, it appears that it wants you to put the execution code in the header ??.. anyway, it's a mess.

I think at this point I would be better off rolling my own code so at least I can understand what the heck is going on. :) But I will look to the Text properties and see what I can come up with.

Yeah, I don't really have any experience using the .NET framework from C++. I believe the limits of my experience were in creating a .NET C++ 'windows forms application', and seeing how it insisted on code in headers, at which point I went 'ugh' and stopped.


All times are GMT -5. The time now is 8:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC