PDA

View Full Version : Numeric Textbox input only with the permitted punctuation marks (period, comma, minus


gazybara
03-04-2012, 03:22 AM
Hi,
I created a textbox event handler to only allow my textboxes to accept numbers only
with three permitted punctuation marks: period (decimal), comma and minus (substract). The script contains five text fields.
>>>The first of the three text fields should behave exactly as spinners do. The conditions are as follows:
- TB_Spiner1 [minimum:0 ; maximum:1000 ; decimal Places:3] [FloatValue]
- TB_Spiner2 [minimum:1 ; maximum:100 ; decimal Places:0] [IntegerValue]
- TB_Spiner3 [minimum:-200 ; maximum:200 ; decimal Places:2] [FloatValue]
- *MouseWheel* Increment are 1 (when hold CTRL Key Increment are 10)
- *KeyArrows* (Up and Down) Increment are always 1 (one).
- *Escape* Key reset a value to the default
- *Return* Key executes and checks if the value is in acceptable limits.
- *Tab* Key select next active control in form.
I also allow to input *minus* only if selection start at the beginning, and *Period* can be set only once. (for floats)

>>>Description and Conditions for the fourth text field:
*TB_valueInput1*: Fill the text filds with the numbers [Float or Integer] while the sum are not > 300.
If the last entry exceeds the maximum (300) then it will be deleted after you press *Return* key.
Allowed punctuation marks: (period, comma, minus). *Space* between the numbers are disallowed.
NOTE: The input must be *in one direction* i.e. all numbers must have either a positive or negative values e.g.([-10,-20,-30,] or [10,20,30,],
so that the absolute value of the sum is greater than three hundred [abs(theSum) < 300]. Three hundred is the limit.
- *Escape* Key clear text field.
- *Return* Key executes and checks if the value is in acceptable limits.
- *Tab* Key select next active control in mxsForm.

>>>Description and Conditions for the fifth text field:
*TB_valueInput2*: Fill the text filds with the positive and negative numbers [Float or Integer].
Only limitation is: the value of a number must be in range [min:-90 ; max:90].
If the value is greater than the allowed range then is replaced by zero when press *Return* key.
- *Escape* Key clear text field.
- *Return* Key executes and checks if the value is in acceptable limits.
- *Tab* Key select next active control in mxsForm.

I have several questions:
1. Is there an easier and better way to solve this? (show some example)
2. When i press (CTRL+V) or (RMB and Paste from contex menu) how to put Clipboard text through
filter functions (from this script *fn numConvert* or *fn summerize*) before it is displayed in the text field.
I almost started to explore possibilities of .NET integration in maxscript and generally
most of the things I learned from this forum. So thank you all for that.
Any ideas and suggestions are welcome! Thank you in advance and sorry for my english.
Branko

lo
03-04-2012, 08:14 AM
Don't have time at the moment to go over the whole thing but on the point of spinner validation, you don't have to reinvent the wheel, dotnet already has methods to determine if a string is a valid float:

fn spnTB_KD s e = --KeyDown
(
blockChar = not ((dotnetclass "System.Single").tryParse (s.text + e.keyCode.ToString()) &result)

if (e.keyCode.ToString()) == "Up" do (s.Text = numConvert s.Text 1 dp rangeArr[itm].x rangeArr[itm].y ; s.Select s.TextLength 0) --UpKey
if (e.keyCode.ToString()) == "Down" do (s.Text = numConvert s.Text -1 dp rangeArr[itm].x rangeArr[itm].y ; s.Select s.TextLength 0) --DownKey
)

gazybara
03-04-2012, 10:35 AM
Thanks, lo. That was exaclty what I needed, shorter way. On my way work fine, but most of the
is redundant. I read something about Char.TryParse Method .Сan you explain to me how these method works and where should I make changes in the code. Thank you for your help,again.

gazybara
03-04-2012, 10:36 AM
Thanks, lo. That was exaclty what I needed, shorter way. On my way work fine, but most of the
is redundant. I read something about Char.TryParse Method.
Сan you explain to me how these method works and where should I make changes in the code.
Thank you for your efforts.

lo
03-04-2012, 11:04 AM
I read something about Char.TryParse Method

What did you read about it? It just tries to convert a string to a unicode character. What relation does it have to your situation?

p.s. - there was small typo in my code, I fixed it

gazybara
03-04-2012, 12:08 PM
Ok, converting. I understand now. But what about my 2nd question (about Paste).
Do you have any solution for that?

lo
03-04-2012, 12:16 PM
Try using the textChanged event of the textbox instead of the key events.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged.aspx

gazybara
03-04-2012, 12:45 PM
This is an excellent example, thanks. I am not currently on my computer but I'll try it later.
If I understand you correctly this example should solve the *Paste* problem?

lo
03-04-2012, 12:52 PM
Haven't tried it, but I think so.

CGTalk Moderation
03-04-2012, 12:52 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.