Valhalla Legends Forums Archive | C/C++ Programming | Sending return command in 'Edit' class

AuthorMessageTime
Spilled[DW]
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??
June 20, 2005, 4:53 PM
Myndfyr
Newline is \n, not /n.
June 20, 2005, 6:28 PM
Spilled[DW]
\n** i meant, typo sorry. Heres my code:
[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);   
[/code]

Ideas?
June 20, 2005, 7:46 PM
OnlyMeat
[quote author=Spilled[DW] link=topic=11905.msg116669#msg116669 date=1119296808]
\n** i meant, typo sorry. Heres my code:
[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);   
[/code]

Ideas?
[/quote]

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:

[code]
std::string TextString = MyText;
TextString += "\r\n";
[/code]

Also a small note, you can actually use a std::string as the buffer, it's not necessary to use a char tempory.
June 20, 2005, 7:54 PM
UserLoser.
Eww, use constant.

[code]
if((int)wParam == VK_RETURN) { ...
[/code]
June 21, 2005, 3:51 AM
Spilled[DW]
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!
June 21, 2005, 5:38 PM
OnlyMeat
[quote author=Spilled[DW] link=topic=11905.msg116759#msg116759 date=1119375480]
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!
[/quote]

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.
June 21, 2005, 7:03 PM
Spilled[DW]
Appreciate your help OnlyMeat, thanks.
June 22, 2005, 7:40 AM

Search