[Solved] How do I make textbox to start with a number?

Get help using Construct 2

Post » Thu Feb 04, 2016 10:50 pm

Hey Guys!
How do i make textbox to start with a number? I need to create a textbox for numbers only, but the first number want to be "9 "..
.. to start with 9 always without delete it.
Any help? Thanks!
Last edited by odiusfly on Mon Feb 08, 2016 6:34 am, edited 1 time in total.
B
53
S
32
G
14
Posts: 242
Reputation: 12,358

Post » Thu Feb 04, 2016 11:07 pm

Ok! Per the support forum read first. Please show code or .capx file. So we can see what is your issue and give guidance from there. I know I am overzealous and Like giving out free advice on how to fix stuff. But I to need to follow the rules. So no code = No Help. Sorry Jeremy C.
B
29
S
13
G
10
Posts: 355
Reputation: 8,317

Post » Thu Feb 04, 2016 11:25 pm

A slightly advanced technique would be to use regular expressions (regex) to check the contents of the box every tick, and if it doesn't match your specified expression (doesn't start with 9, or contains a letter) you can delete the offending characters or replace with a 9, ect.

I would store the contents of a text field in a variable. Every time a user tries to input a character, I would test the new string against a regex - if it passes, the variable gets updated with the new string. If it doesn't, the text field gets replaced by whatever was previously in the variable.

Carbin - sometimes people don't even know where to start, so there wouldn't be much to show in a capx ;) If he needed help for something like building his specific regex, then it would be more useful to see what he has tried so far.
Mistakes were made.
B
52
S
26
G
111
Posts: 1,630
Reputation: 62,681

Post » Fri Feb 05, 2016 8:30 am

Guys, first of all thanks for your posts!

@ Carbincopy -> Αre you read my topic title and my post carefully? Why ask about capx? I know the rules.. ;)

So, we open construct 2, a new empty area, we add a text box and boom! What is the magic code? That's it!
It is not about rules, is just a clean question. Thanks!

P.S: 9 was an example.. I want to start with a letter "A", or a symbol.. Whatever, but can't delete this first number/letter. I don't want if user right something instead 9 to delete it, i want 9 for example to be as first always. Whatever user want to write, 9 to be first.
B
53
S
32
G
14
Posts: 242
Reputation: 12,358

Post » Fri Feb 05, 2016 11:15 am

I solved by myself but it's 99% completed.. :(
First of all here is the capx: http://odiusfly.com/TextBox_v1.capx

The problem is:
1. If you start write something, it starts with 9. All good!
2. If you use backspace to start remove the numbers until you clear all field, and try to start write again it starts with 9. All good again!
3. BUT if you have already numbers and select all together, and instead use delete or backspace, you write a number, the number 9 don't appear as first number.

Any ideas? Thanks again.. :)
B
53
S
32
G
14
Posts: 242
Reputation: 12,358

Post » Fri Feb 05, 2016 5:05 pm

Maybe something like this?
https://www.dropbox.com/s/b1oqul8f3igx1 ... .capx?dl=0

How do we do it?
First, set the type of the textbox to "Number" – that means only numbers can be entered, and on mobile devices, it shows the number-only keyboard.
(pic: https://www.dropbox.com/s/jqmmrjkn3up7sw9/type.png?dl=0)

Then, we every tick compare the first character of the textbox
Code: Select all
len(TextBox.Text, 1)

Well, we compare an integer of that
Code: Select all
int(len(TextBox.Text, 1))
with number 9. Integer because we compare with 9 (number). I guess you could do it without integer when comparing to "9" (as text; note the quote marks).
If it doesn't equal, we set the text of the TextBox to
Code: Select all
"9"&TextBox.Text

That means it adds number 9 to the start of the text and keeps the things you have already typed inside.
B
32
S
7
G
4
Posts: 398
Reputation: 4,591

Post » Fri Feb 05, 2016 9:06 pm

grigrizljac wrote:
Code: Select all
int(len(TextBox.Text, 1))


@grigrizljac i was just about to make a new thread when i see this thread luckily this is the line of code i was looking for lol :lol: :lol: :lol: :D
Image
B
38
S
11
G
2
Posts: 315
Reputation: 3,797

Post » Mon Feb 08, 2016 6:30 am

@ grigrizljac
BIG THANKS!!! : -)
B
53
S
32
G
14
Posts: 242
Reputation: 12,358


Return to How do I....?

Who is online

Users browsing this forum: dop2000, JackTYGA, zill and 33 guests