Kuan
Kuan

Reputation: 11389

How to implement char limit in wxTextctrl

All

I wonder how to implement text char number limit in wxTextCtrl "multiline" style.

I need: 1) Block user input when limit reached(just keep char which is in limit range). 2) When user input or paste in any place of the string,keep cursor in right place(just right after the insert string), and make sure 1)

For example,I limit the number to 10: When I input in the wxtextctrl:

1) I can input until char number reaches 10, then I can not do any insertions(but I can delete some chars, then input). such as: I can input 1234567890

2) If char number not reachs limit, I can input or paste some char in any position of exsiting text until reach limit. such as: I input 1234, then I want to insert 34567890 between 2 and 3 in exsiting text, the result is 1234567834 and the cursor is right after 8

How can I do this?

Thanks

Upvotes: 2

Views: 1666

Answers (1)

dario_ramos
dario_ramos

Reputation: 7309

Just use the wxTextCtrl::SetMaxLength method (available only since version 2.3.2, mind it)

Edit: The solution above does not apply under GTK+. If you're in that case, you'll have to write a class which extends wxTextCtrl and does what you need. To do that, a possible approach:

  1. Override the SetMaxLength function to save the max length in an attribute of your class
  2. Handle the EVT_TEXT(id, func) event. In the handler, if you reached the max length, start ignoring input (you'll need to figure out how); if you went below max length, accept input again.

Upvotes: 2

Related Questions