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

Highlighting the text on a textBox
Goto page 1, 2  Next
 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sat May 26, 2007 2:13 pm    Post subject: Highlighting the text on a textBox Reply with quote

Greetings.

I am trying to highlight the text entered on a textbox, but I can not find any options on the text box. Well, I found a few seemingly choices, but they are not working. I used

txtVendor.select(0,10);
txtVendor.selectAll();

selectionStart(), etc., but but they do not seem to work.

So, how do I highlight the string on a txtBox?

thanks,

jic
Back to top
View user's profile Send private message
Chris Miller



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

PostPosted: Wed May 30, 2007 5:21 am    Post subject: Re: Highlighting the text on a textBox Reply with quote

select(start, length); or selectionStart = start; and selectionLength = length;

This only works if the control is created; I felt that it doesn't make sense to have a selection without a physical window. You can set it in the form's load event, etc.
Back to top
View user's profile Send private message
Laubi



Joined: 18 May 2007
Posts: 9

PostPosted: Wed May 30, 2007 10:14 am    Post subject: Reply with quote

I'm not sure whether I really understand this problem or not, but is the code below supposed to work or not?

Code:
private import dfl.all;

class MainForm: Form
{
   TextBox myTextBox;
   Button myButton;
   
   this()
   {
      with(myTextBox = new TextBox)
      {
         bounds = Rect(5, 5, 190, 22);
         parent = this;
      }
      
      with(myButton = new Button)
      {
         location = Point(200, 2);
         parent = this;
         
         click ~= &this.btn_click;
      }      
   }   
   
   private void btn_click(Object sender, EventArgs ea)
   {
        myTextBox.selectAll();
   }
}

int main()
{   
   Application.run(new MainForm);   
}
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Wed May 30, 2007 11:35 am    Post subject: Reply with quote

It does not work for you?

My problem is that highlighting the content of a textbox is not working for me. Everything else works.

I have not tried this, but it should work.

jic
Back to top
View user's profile Send private message
Chris Miller



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

PostPosted: Thu May 31, 2007 6:40 am    Post subject: Reply with quote

Aside from you forgetting to return from main, the code doesn't work as expected because when you click the button, the button has focus when you try to select text in the textbox. The following works,
myTextBox.select();
myTextBox.selectAll();

I am considering making the textbox selection functions also first select/set-focus to the textbox.
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Thu May 31, 2007 11:02 am    Post subject: Reply with quote

Chris Miller wrote:
Aside from you forgetting to return from main, the code doesn't work as expected because when you click the button, the button has focus when you try to select text in the textbox. The following works,
myTextBox.select();
myTextBox.selectAll();

I am considering making the textbox selection functions also first select/set-focus to the textbox.


this is not working:
Code:
  private void txtContact_keyDown(Object sender, KeyEventArgs ea)
  {
    if (!GotContactList || ContactList.length == 0)
    {
      ContactList    = GetFreshList("pmuk");
      GotContactList = true;
      saveTyping = "";
    }
    if(ea.keyCode == Keys.UP)
    {
      ContactListPTR = GetListPTR(-1,ContactList,saveTyping,ContactListPTR);
      txtContact.text = ContactList[ContactListPTR];
      txtContact.select();
      txtContact.selectAll();
      saveTyping = "";
    }
    else if(ea.keyCode == Keys.DOWN)
    {
      ContactListPTR = GetListPTR(1,ContactList,saveTyping,ContactListPTR);
      txtContact.text = ContactList[ContactListPTR];
      txtContact.select();
      txtContact.selectAll();
      saveTyping = "";
    }
    else
    {
      saveTyping ~= cast(char)ea.keyCode;
    }
  }


This code will use what the user has typed in the txtContact textbox and search on an array for the closest match and move the array pointer up or down and display the array content on the textbox. however, it is displaying the content, but it is not highlighted. I want it highlighted.

What am I doing wrong?
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Thu May 31, 2007 3:53 pm    Post subject: Reply with quote

Are you sure the function is getting called? Are you sure it's entering the "if" you want? I'd first try adding some MessageBox (or whatever its name is) to see it's getting there in the first place.
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sun Jun 03, 2007 4:59 pm    Post subject: Reply with quote

Carlos wrote:
Are you sure the function is getting called? Are you sure it's entering the "if" you want? I'd first try adding some MessageBox (or whatever its name is) to see it's getting there in the first place.


Yes, because,

txtContact.text = ContactList[ContactListPTR];

is getting updated. This is the initialization of txtContact:
Code:
      //~DFL dfl.textbox.TextBox=txtContact
      txtContact = new dfl.textbox.TextBox();
      txtContact.name = "txtContact";
      txtContact.hideSelection = false;
      txtContact.maxLength = 300;
      txtContact.bounds = dfl.all.Rect(240, 164, 130, 20);
      txtContact.parent = group;


I also thought that the highlight color may be invisible, but it I type anything, it should delete what is already there and it does not. The cursor does move to the semi-last character, ie. if the word is Carlos, the txtContact displays Carlo|s.

Any suggestions?
Back to top
View user's profile Send private message
Chris Miller



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

PostPosted: Sun Jun 03, 2007 6:32 pm    Post subject: Reply with quote

It might have to do with you messing with the textbox right as you're typing into it; as in, it doesn't like that you're messing with the selection when it's trying to manage the selection from the typed-in key. Try in the keyUp and maybe some other means of delaying it.
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sun Jun 03, 2007 9:19 pm    Post subject: Reply with quote

Chris Miller wrote:
It might have to do with you messing with the textbox right as you're typing into it; as in, it doesn't like that you're messing with the selection when it's trying to manage the selection from the typed-in key. Try in the keyUp and maybe some other means of delaying it.


Chris,

you are correct. This code works for 1500 milliseconds:
Code:
  private void txtContact_keyDown(Object sender, KeyEventArgs ea)
  {
    if (!GotContactList || ContactList.length == 0)
    {
      ContactList    = GetFreshList("pmuk");
      GotContactList = true;
      saveTyping = "";
    }
    if(ea.keyCode == Keys.UP)
    {
      ContactListPTR = GetListPTR(-1,ContactList,saveTyping,ContactListPTR);
      txtContact.text = ContactList[ContactListPTR];
      txtContact.select();
      msleep(1500);
      //txtContact.selectAll();
      saveTyping = "";
    }
    else if(ea.keyCode == Keys.DOWN)
    {
      ContactListPTR = GetListPTR(1,ContactList,saveTyping,ContactListPTR);
      txtContact.text = ContactList[ContactListPTR];
      msleep(1500);
      txtContact.select();
      txtContact.selectAll();
      saveTyping = "";
    }
    else
    {
      saveTyping ~= cast(char)ea.keyCode;
    }
  }


However, as soon as I the 1500 milliseconds go, the highlight disappears. which means that as soon as it leaves txtContact_keyDown, the highlight disappears. Why?
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Sun Jun 03, 2007 9:46 pm    Post subject: Reply with quote

I am talking about the Key.UP side. Sorry. The other one does not. I am sure that if I change the msleep(1500) after the select(), it would work.
Back to top
View user's profile Send private message
Chris Miller



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

PostPosted: Sun Jun 03, 2007 10:32 pm    Post subject: Reply with quote

I didn't mean that type of delay.. I mean some time after the key event, like with a timer or delayInvoke, or something.
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Mon Jun 04, 2007 8:38 am    Post subject: Reply with quote

Chris Miller wrote:
I didn't mean that type of delay.. I mean some time after the key event, like with a timer or delayInvoke, or something.


I don't understand, but let me read about delayInvoke and get back to you. However, here is what I did:
Quote:
private void txtContact_keyDown(Object sender, KeyEventArgs ea)
{
if (!GotContactList || ContactList.length == 0)
{
ContactList = GetFreshList("pmuk");
GotContactList = true;
saveTyping = "";
}
if(ea.keyCode == Keys.UP)
{
ContactListPTR = GetListPTR(-1,ContactList,saveTyping,ContactListPTR);
txtContact.text = ContactList[ContactListPTR];
txtContact.select();
//txtContact.selectAll();
saveTyping = "";
}
else if(ea.keyCode == Keys.DOWN)
{
ContactListPTR = GetListPTR(1,ContactList,saveTyping,ContactListPTR);
txtContact.text = ContactList[ContactListPTR];
txtContact.select();
txtContact.selectAll();
saveTyping = "";
}
else
{
saveTyping ~= cast(char)ea.keyCode;
}
msleep(1500);
}


This causes the highlight for 15 milliseconds. Which points to the problem being leaving the txtContact_keyDown function. So, apparently, it has nothing to do with messing with the content of txtContact. Do you see it that way also?
Back to top
View user's profile Send private message
jicman



Joined: 22 Dec 2004
Posts: 298
Location: Rochester, NY

PostPosted: Mon Jun 04, 2007 8:51 am    Post subject: Reply with quote

Sorry, here is the code in a nicer way...
Code:
  private void txtContact_keyDown(Object sender, KeyEventArgs ea)
  {
    if (!GotContactList || ContactList.length == 0)
    {
      ContactList    = GetFreshList("pmuk");
      GotContactList = true;
      saveTyping = "";
    }
    if(ea.keyCode == Keys.UP)
    {
      ContactListPTR = GetListPTR(-1,ContactList,saveTyping,ContactListPTR);
      txtContact.text = ContactList[ContactListPTR];
      txtContact.select();
      //txtContact.selectAll();
      saveTyping = "";
    }
    else if(ea.keyCode == Keys.DOWN)
    {
      ContactListPTR = GetListPTR(1,ContactList,saveTyping,ContactListPTR);
      txtContact.text = ContactList[ContactListPTR];
      txtContact.select();
      txtContact.selectAll();
      saveTyping = "";
    }
    else
    {
      saveTyping ~= cast(char)ea.keyCode;
    }
    msleep(1500);
  }
Back to top
View user's profile Send private message
Chris Miller



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

PostPosted: Tue Jun 05, 2007 12:33 pm    Post subject: Reply with quote

You should never block the GUI thread with sleeps, it locks everything up. By delay I meant to run that code after the key events have returned. delayInvoke posts a new window message (event) and runs your specified function after the current window messages have processed.

Perhaps something like this...
Code:
delayInvoke(function(Control cc, size_t[] params) { (cast(MyForm)cc).doKeyStuff(cast(Keys)params[0]); }, ea.keyCode);

where doKeyStuff is a method in MyForm and takes a Keys which is the keyCode.

It's an odd solution because what you're doing is quite odd (changing a textbox as you're typing into it).
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
Goto page 1, 2  Next
Page 1 of 2

 
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