Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Spilled on June 20, 2005, 11:53 AM

Title: Sending return command in 'Edit' class
Post by: Spilled on June 20, 2005, 11:53 AM
I am currently having trouble with sending the return key in the 'edit' class. I've made a subclass to detect return and key downs but I don't know how to send a new line to another textbox. For example, take a string, add a newline, and then add another string on the next line. I try putting the '/n' into a const char array but it won't work. Help??
Title: Re: Sending return command in 'Edit' class
Post by: MyndFyre on June 20, 2005, 01:28 PM
Newline is \n, not /n.
Title: Re: Sending return command in 'Edit' class
Post by: Spilled on June 20, 2005, 02:46 PM
\n** i meant, typo sorry. Heres my code:

LRESULT CALLBACK MyTextProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
        {
               case WM_KEYDOWN:
                   if ((int) wParam == 13)
                   {
                   int SendTextLength = GetWindowTextLength(SendBar) + 1;
                   char MyText[SendTextLength + 1];
                   GetWindowText(SendBar, MyText, SendTextLength);
                   MyText[SendTextLength + 1] = '\n';
                   std::string TextString = MyText;
                   SetWindowText(TextBox, TextString.c_str());
                   SetWindowText(SendBar, NULL);   


Ideas?
Title: Re: Sending return command in 'Edit' class
Post by: OnlyMeat on June 20, 2005, 02:54 PM
Quote from: Spilled[DW] on June 20, 2005, 02:46 PM
\n** i meant, typo sorry. Heres my code:

LRESULT CALLBACK MyTextProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
        {
               case WM_KEYDOWN:
                   if ((int) wParam == 13)
                   {
                   int SendTextLength = GetWindowTextLength(SendBar) + 1;
                   char MyText[SendTextLength + 1];
                   GetWindowText(SendBar, MyText, SendTextLength);
                   MyText[SendTextLength + 1] = '\n';
                   std::string TextString = MyText;
                   SetWindowText(TextBox, TextString.c_str());
                   SetWindowText(SendBar, NULL);   


Ideas?

Two things.
(1) MyText[SendTextLength + 1], The last character should be the null terminator if im not mistaken. Replacing it with \n might be causing you problems.

(2) If i remember correctly normally the line delimiters for text streams are carriage return + line feed, which would be \r\n. Could be wrong on that though, if it's not just stick with \n.

Try this:


std::string TextString = MyText;
TextString += "\r\n";


Also a small note, you can actually use a std::string as the buffer, it's not necessary to use a char tempory.
Title: Re: Sending return command in 'Edit' class
Post by: UserLoser. on June 20, 2005, 10:51 PM
Eww, use constant.


if((int)wParam == VK_RETURN) { ...
Title: Re: Sending return command in 'Edit' class
Post by: Spilled on June 21, 2005, 12:38 PM
Now that i have got this working, thanks to the fast help, i have ran into another problem. Now when i type it goes to the text box but it doesn't auto scroll down. Now in the createwindow api i have include AUTOVSCROLL. Any ideas? Thanks again!
Title: Re: Sending return command in 'Edit' class
Post by: OnlyMeat on June 21, 2005, 02:03 PM
Quote from: Spilled[DW] on June 21, 2005, 12:38 PM
Now that i have got this working, thanks to the fast help, i have ran into another problem. Now when i type it goes to the text box but it doesn't auto scroll down. Now in the createwindow api i have include AUTOVSCROLL. Any ideas? Thanks again!

From what i remember the ES_AUTOVSCROLL style enables automatic scrolling when the user hits the return key. Setting the text programmatically will not have the same effect. You have to scroll to the line you want in code.

You can use the EM_LINESCROLL message to force the multiline edit control to scroll to a number of lines. Use that in conjunction with EM_GETLINECOUNT to determine how many lines to scroll to the end of the control, if thats what you want to do that is.

EM_LINEINDEX and EM_LINEFROMCHAR, can be used to determine the current line.

Another quick way of scrolling to the end of the control is using the EM_SETSEL message, and specifying the last character index.
Title: Re: Sending return command in 'Edit' class
Post by: Spilled on June 22, 2005, 02:40 AM
Appreciate your help OnlyMeat, thanks.