FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

HowTo: Selection in textbox?

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
Lynn



Joined: 27 Aug 2004
Posts: 89

PostPosted: Thu Dec 23, 2004 6:00 pm    Post subject: HowTo: Selection in textbox? Reply with quote

I'm unclear on how to cause visible selection of a char[] within a multiline viewerTextBox.
I've tried various combinations of the following calls. I'm expecting that the selected text will be either highlighted or reverse video, but the viewerTextBox's appearance doesn't change.

int len = curSearchVerse.length;
int index = find(viewerTextBox.text, curSearchVerse);
viewerTextBox.hideSelection = false;
viewerTextBox.selectionStart = index;
viewerTextBox.selectionLength = len;
viewerTextBox.select(index, len);

Actually, I'm fuzzy on how to cause selection highlighting (reverse video) of a non-multiline TextBox. Nothing happens with the following code:

searchWordsTextBox.text = "1234567890";
searchWordsTextBox.hideSelection = false;
searchWordsTextBox.select(3, 5);
searchWordsTextBox.selectionStart(3);
searchWordsTextBox.selectionLength(5);
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Fri Dec 24, 2004 12:05 am    Post subject: Re: HowTo: Selection in textbox? Reply with quote

Lynn wrote:
I'm unclear on how to cause visible selection of a char[] within a multiline viewerTextBox.

The handle has to be created. Call it's createControl() function or wait until onHandleCreated().
Back to top
View user's profile Send private message
Lynn



Joined: 27 Aug 2004
Posts: 89

PostPosted: Fri Dec 24, 2004 6:05 am    Post subject: Reply with quote

I'm being slow and don't understand.

In a plain TextBox that is already showing text "1234567890", I don't see how to Select "45678". Doesn't the "handle" already exist? The code I've tried doesn't do anything. Can you provide an example that Selects text in a non-multiline and multiline TextBox?

In a multiline TextBox that is overfilled (scrollbars activated), I want to be able to Select something like (offset=2000, len=200) in a TextBox that contains about 4000 chars, with only 1000 visible. The Selected line should be automagically scrolled into view. With MFC, this is:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cedit.3a3a.setsel.asp

In the Win32 Api, this is:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/editcontrols/editcontrolreference/editcontrolmessages/em_setsel.asp

Thanks for your patience with this newbie, and very prompt replies!
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Fri Dec 24, 2004 8:34 am    Post subject: Reply with quote

I see.. when I was making an example I found that setting the selection in some events doesn't work as expected.
For the record,
Code:
selectionStart = pos;
selectionLength = len;

is the same as
Code:
select(pos, len);

and they use EM_SETSEL, so I guess it's a Windows thing. But here's my example that works. I found that setting the selection in a multiline textbox won't scroll to the caret, so I forced it to:
Code:
class MainForm: Form
{
   TextBox single, multi;
   this()
   {
      Button sbtn;
      with(sbtn = new Button)
      {
         text = "&s btn";
         parent = this;
         
         click ~= &sbtn_click;
      }
     
      with(single = new TextBox)
      {
         left = sbtn.right + 4;
         text = "Open Source Development for D Open Source Development for D Open Source Development for D"
            " dsource.org";
         hideSelection = false;
         parent = this;
      }
     
      Button mbtn;
      with(mbtn = new Button)
      {
         top = 40;
         text = "&m btn";
         parent = this;
         
         click ~= &mbtn_click;
      }
     
      with(multi = new TextBox)
      {
         text = "Open Source Development for D\r\nOpen Source Development for D\r\nOpen Source Development for D"
            "\r\nOpen Source Development for D\r\nOpen Source Development for D\r\nOpen Source Development for D"
            " dsource.org";
         hideSelection = false;
         bounds = Rect(mbtn.right + 4, mbtn.top, 200, 60);
         multiline = true;
         acceptsReturn = true;
         parent = this;
      }
   }
   private void sbtn_click(Object sender, EventArgs ea)
   {
      int epos;
      epos = std.string.rfind(single.text, 'e');
      if(epos != -1)
      {
         single.select(epos, 1);
      }
   }
   private void mbtn_click(Object sender, EventArgs ea)
   {
      int epos;
      epos = std.string.rfind(multi.text, 'e');
      if(epos != -1)
      {
         multi.select(epos, 1);
         multi.scrollToCaret();
      }
   }
}
Back to top
View user's profile Send private message
Lynn



Joined: 27 Aug 2004
Posts: 89

PostPosted: Mon Dec 27, 2004 7:26 pm    Post subject: Reply with quote

Thanks MUCHO for the example.

I tweaked it a bit into a complete program suitable to be a tutorial. Also, the revised code does more than look for the single character 'e' and makes sure that caret scrolling to take place.

Submitted to dfl.tutorial as:
http://dsource.org/tutorials/index.php?show_example=138
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group