Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Imhotep, opm), 785 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How to check the data entered in the text box and save? #479119
02/20/20 08:05
02/20/20 08:05
Joined: Jan 2012
Posts: 108
G
gamers Offline OP
Member
gamers  Offline OP
Member
G

Joined: Jan 2012
Posts: 108
Hi there,
I need some help, again.
How to save the result to the log file in desired conditions by checking the data entered in the textbox?

For example, how can I ensure that the data is saved in the log file only when a value between 0 and 100 is entered in the text box, and that the user is warned when any data is entered or a character is written outside of mentioned values?

In the example below, I was able to save the data entered in textbox, but I could not check the data type.
I'll be happy if you can help me.
Yours truly,

Here is the example: https://we.tl/t-uS1ItW0oRb

Re: How to check the data entered in the text box and save? [Re: gamers] #479120
02/20/20 08:20
02/20/20 08:20
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Hey,

So basically you only need to save input from user in range of 0...99 into the file, and everything different from that shouldn't be saved + user should be warned that entered data is incorrect?

Greets


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: How to check the data entered in the text box and save? [Re: 3run] #479121
02/20/20 09:35
02/20/20 09:35
Joined: Jan 2012
Posts: 108
G
gamers Offline OP
Member
gamers  Offline OP
Member
G

Joined: Jan 2012
Posts: 108
Originally Posted by 3run
Hey,

So basically you only need to save input from user in range of 0...99 into the file, and everything different from that shouldn't be saved + user should be warned that entered data is incorrect?

Greets


That's true,
If I can check the data entered in the textbox, it can already save sample code, but I was not able to integrate the data control in to the sample code.

Best regards,

Re: How to check the data entered in the text box and save? [Re: gamers] #479123
02/20/20 10:03
02/20/20 10:03
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
Well, you needed to use stuff I've made in this thread.

Try this code:
Code
BMAP *pointer_tga = "pointer.png";

STRING *name_str = "Click to Enter Value Here!";
STRING *enter_value_str = "Click to Enter Value Here!";
STRING *error_msg_str = "You need to enter a number from 0 to 99!";
STRING *data_saved_msg_str = "Data was successfully saved into the log file.";

function input_name(); 

PANEL *input_pan = 
{
	layer = 10;
	bmap = "textbox.png";
	on_click = input_name;
	flags = SHOW;
}

TEXT *name_txt = 
{
	layer = 11; // just above the textbox panel
	font = "Calibri#30";   
	string(name_str);
	flags = SHOW | CENTER_X | CENTER_Y;
}

// receives a STRING and position (num) of a character in that STRING
// returns a single character as a STRING
STRING *get_next_char(STRING *src_str, int num)
{
	if(!src_str)
	{
		return NULL;
	}
	
	if(num > str_len(src_str))
	{
		return NULL;
	}
	
	STRING *temp_str = "";
	str_cpy(temp_str, src_str);
	str_trunc(temp_str, str_len(src_str) - num);
	str_clip(temp_str, num - 1);
	
	return temp_str;
}

// returns TRUE (1) if string is numeric
// otherwise returs FALSE (0)
var is_numeric_str(STRING *src_str)
{
	if(!src_str)
	{
		return false;
	}
	
	int i = 0, j = 0, res = true;
	
	for (i = 0; i < str_len(src_str); i++)
	{
		STRING *temp_str = get_next_char(src_str, i + 1);
		
		for (j = 0; j < 10; j++)
		{
			if (!str_cmpi(temp_str, str_for_num(NULL, j)))
			{
				res = false;
				continue;
			}
			
			res = true;
			break;
		}
		
		if(res == false)
		{
			break;
		}
	}
	
	return res;
}

// returns TRUE (1) is string's length is zero
// otherwise returns FALSE (0)
var is_empty_str(STRING *str)
{
	if(!str)
	{
		return false;
	}
	
	if(str_len(str) > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function input_name()
{
	str_cpy(name_str, "   ");
	var input = inkey(name_str);
	
	if(input != 13) // terminated input with ESC or anything else ?
	{
		// then return "Click to Enter Value Here!" message back
		// same for all errors bellow !
		str_cpy(name_str, enter_value_str);
	}
	else // pressed enter ?
	{
		// string is empty ? then show error message
		if(is_empty_str(name_str))
		{
			error(error_msg_str);
			str_cpy(name_str, enter_value_str);
		}
		else // if not ?
		{
			// if numeric ? then save the file
			if(is_numeric_str(name_str))
			{
				// we can also check for the range here
				// if it's more than 2 characters, then we are out of the range
				if(str_len(name_str) > 2)
				{
					error(error_msg_str);
					str_cpy(name_str, enter_value_str);
				}
				else // 0 ... 99
				{
					var filehandle;
					filehandle = file_open_write("log.xml"); 
					file_str_write(filehandle, name_str); 
					file_close(filehandle);
					error(data_saved_msg_str);
				}
			}
			else // not numeric ? then show error message
			{
				error(error_msg_str);
				str_cpy(name_str, enter_value_str);
			}
		}
	}
}

function mouse_startup()
{
	warn_level = 6; // make sure to show all errors
	fps_max = 60; // limit fps to 60
	
	mouse_mode = 4; // this will take care of mouse_cursor/mouse_pos stuff
	mouse_map = pointer_tga;
	
	while(!key_esc)
	{
		// we need to do this in order to have proper panel positions with different screen resolutions
		input_pan->pos_x = (screen_size.x / 2) - (bmap_width(input_pan->bmap) / 2);
		input_pan->pos_y = (screen_size.y / 2) - (bmap_height(input_pan->bmap) / 2) - 64;
		
		name_txt->pos_x = input_pan->pos_x + (bmap_width(input_pan->bmap) / 2);
		name_txt->pos_y = input_pan->pos_y + (bmap_height(input_pan->bmap) / 2);
		
		wait(1);
	}
}


Greets!

Last edited by 3run; 02/20/20 10:04.

Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: How to check the data entered in the text box and save? [Re: 3run] #479124
02/20/20 12:12
02/20/20 12:12
Joined: Jan 2012
Posts: 108
G
gamers Offline OP
Member
gamers  Offline OP
Member
G

Joined: Jan 2012
Posts: 108
Originally Posted by 3run
Well, you needed to use stuff I've made in this thread.

Try this code:
Code
BMAP *pointer_tga = "pointer.png";

STRING *name_str = "Click to Enter Value Here!";
STRING *enter_value_str = "Click to Enter Value Here!";
STRING *error_msg_str = "You need to enter a number from 0 to 99!";
STRING *data_saved_msg_str = "Data was successfully saved into the log file.";

function input_name(); 

PANEL *input_pan = 
{
	layer = 10;
	bmap = "textbox.png";
	on_click = input_name;
	flags = SHOW;
}

TEXT *name_txt = 
{
	layer = 11; // just above the textbox panel
	font = "Calibri#30";   
	string(name_str);
	flags = SHOW | CENTER_X | CENTER_Y;
}

// receives a STRING and position (num) of a character in that STRING
// returns a single character as a STRING
STRING *get_next_char(STRING *src_str, int num)
{
	if(!src_str)
	{
		return NULL;
	}
	
	if(num > str_len(src_str))
	{
		return NULL;
	}
	
	STRING *temp_str = "";
	str_cpy(temp_str, src_str);
	str_trunc(temp_str, str_len(src_str) - num);
	str_clip(temp_str, num - 1);
	
	return temp_str;
}

// returns TRUE (1) if string is numeric
// otherwise returs FALSE (0)
var is_numeric_str(STRING *src_str)
{
	if(!src_str)
	{
		return false;
	}
	
	int i = 0, j = 0, res = true;
	
	for (i = 0; i < str_len(src_str); i++)
	{
		STRING *temp_str = get_next_char(src_str, i + 1);
		
		for (j = 0; j < 10; j++)
		{
			if (!str_cmpi(temp_str, str_for_num(NULL, j)))
			{
				res = false;
				continue;
			}
			
			res = true;
			break;
		}
		
		if(res == false)
		{
			break;
		}
	}
	
	return res;
}

// returns TRUE (1) is string's length is zero
// otherwise returns FALSE (0)
var is_empty_str(STRING *str)
{
	if(!str)
	{
		return false;
	}
	
	if(str_len(str) > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function input_name()
{
	str_cpy(name_str, "   ");
	var input = inkey(name_str);
	
	if(input != 13) // terminated input with ESC or anything else ?
	{
		// then return "Click to Enter Value Here!" message back
		// same for all errors bellow !
		str_cpy(name_str, enter_value_str);
	}
	else // pressed enter ?
	{
		// string is empty ? then show error message
		if(is_empty_str(name_str))
		{
			error(error_msg_str);
			str_cpy(name_str, enter_value_str);
		}
		else // if not ?
		{
			// if numeric ? then save the file
			if(is_numeric_str(name_str))
			{
				// we can also check for the range here
				// if it's more than 2 characters, then we are out of the range
				if(str_len(name_str) > 2)
				{
					error(error_msg_str);
					str_cpy(name_str, enter_value_str);
				}
				else // 0 ... 99
				{
					var filehandle;
					filehandle = file_open_write("log.xml"); 
					file_str_write(filehandle, name_str); 
					file_close(filehandle);
					error(data_saved_msg_str);
				}
			}
			else // not numeric ? then show error message
			{
				error(error_msg_str);
				str_cpy(name_str, enter_value_str);
			}
		}
	}
}

function mouse_startup()
{
	warn_level = 6; // make sure to show all errors
	fps_max = 60; // limit fps to 60
	
	mouse_mode = 4; // this will take care of mouse_cursor/mouse_pos stuff
	mouse_map = pointer_tga;
	
	while(!key_esc)
	{
		// we need to do this in order to have proper panel positions with different screen resolutions
		input_pan->pos_x = (screen_size.x / 2) - (bmap_width(input_pan->bmap) / 2);
		input_pan->pos_y = (screen_size.y / 2) - (bmap_height(input_pan->bmap) / 2) - 64;
		
		name_txt->pos_x = input_pan->pos_x + (bmap_width(input_pan->bmap) / 2);
		name_txt->pos_y = input_pan->pos_y + (bmap_height(input_pan->bmap) / 2);
		
		wait(1);
	}
}


Greets!


Thank you @3run laugh That was great solution! This is exactly what I want to do.

Re: How to check the data entered in the text box and save? [Re: gamers] #479125
02/20/20 12:51
02/20/20 12:51
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
You are welcome! Glad to be helpful.


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: How to check the data entered in the text box and save? [Re: 3run] #479126
02/20/20 15:05
02/20/20 15:05
Joined: Jan 2012
Posts: 108
G
gamers Offline OP
Member
gamers  Offline OP
Member
G

Joined: Jan 2012
Posts: 108
Originally Posted by 3run
You are welcome! Glad to be helpful.


Thank you again, @3run. I just want to ask because I'm curious about. If the user provided a condition between 0 and 150 (instead of 0 to 100 value), how could we do that?

Re: How to check the data entered in the text box and save? [Re: gamers] #479127
02/20/20 15:17
02/20/20 15:17
Joined: May 2009
Posts: 5,370
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,370
Caucasus
I would change 2 to 3 in this part
Code
// we can also check for the range here
// if it's more than 2 characters, then we are out of the range
if(str_len(name_str) > 2)

So we will allow to enter value which is made out of 3 characters.
Then I would convert string into the variable using str_to_num.
And at the end I would check if this variable is higher than 150 or not. If it is, then give error, if it is not then save data.

Best regards!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
Re: How to check the data entered in the text box and save? [Re: gamers] #479129
02/20/20 17:57
02/20/20 17:57
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
I think that when you work with numbers is better to use numerical variables instead of strings.

Here goes a simple implementation with the use of 'key_any' and 'key_lastpressed'

Code
function num_edit (var *_ptr_num, var _max) {
	var _old_value = *_ptr_num;
	var _last_pressed = key_lastpressed;
	while(1) {
		if(key_any) {
			if(key_lastpressed != _last_pressed) {
				_last_pressed = key_lastpressed;
				switch(_last_pressed) {
					case 1: // esc
						*_ptr_num = _old_value;
						return;
					
					case 14: // back space
						*_ptr_num = floor(*_ptr_num / 10);
						break;
					
					case 28: // enter
						return;
					
					default: // any other key
						if(_last_pressed > 11) // from 1 to 0 keys -> from 2 to 11 scancodes
							break;
						var _res = *_ptr_num * 10 + (_last_pressed - 1) % 10; // 11 = scancode of 0 character
						if(_res > _max)
							break;
						*_ptr_num = _res;
				}
			}
		} else {
			_last_pressed = 0;
		}
		wait(1);
	}
}

// --------------------------------------------------

function main () {
	static var my_num = 20;
	num_edit(my_num, 150);
	
	while(1) {
		DEBUG_VAR(my_num, 100);
		wait(1);
	}
}


Salud!

Re: How to check the data entered in the text box and save? [Re: gamers] #479134
02/21/20 06:33
02/21/20 06:33
Joined: Jan 2012
Posts: 108
G
gamers Offline OP
Member
gamers  Offline OP
Member
G

Joined: Jan 2012
Posts: 108
Thank you so much @3run & @txesmi


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1