Recent

Author Topic: Complete Newby  (Read 1742 times)

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 82
Complete Newby
« on: July 24, 2024, 03:59:52 pm »
Greeting All

Moved over from Powerbasic & Freebasic /WINFBE after 35 years.
Pascal syntax is going to be a challenge, but your Lazarus Designer is a work of art.
There will be many questions. I have worked through a number of web pages and is trying to see how the Pascal code will look compared to my Basic code for certain functions.
This may have been dealt with here before.

First Question:
Limiting a Textbox or COMBOBOX to only Numbers.
I assume it will be through the keypress / keydown event.

Please show me how you do that on a Combobox and once i understand the syntax differences, we can go a step forward.

Thank you kindly

Peter

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 559
Re: Complete Newby
« Reply #1 on: July 24, 2024, 05:15:18 pm »
Here's an example of what I do:  EUpdateRate is an edit box on my form and this is the "KeyPress" assigned to it.  I want to update a variable named "UpdateTime" from the edit box:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.EUpdateRateKeyPress(Sender: TObject; var Key: char);
  2. { Set update rate from control on control panel. }
  3. var NewInt: integer;
  4. begin
  5.  if (Key = #13) {return} then
  6.   begin
  7.    Try
  8.     Newint := StrToInt(EUpdateRate.text)
  9.    Except
  10.     NewInt := -1
  11.    end;
  12.    If (NewInt > 0) then UpdateTime := NewInt;
  13.   end;
  14. end;  
  15.  

This is the simplest approach I know of, but provides no help to a user.  In my case, I'm the only user, so I don't have to be friendly.

Dzandaa

  • Sr. Member
  • ****
  • Posts: 388
  • From C# to Lazarus
Re: Complete Newby
« Reply #2 on: July 24, 2024, 05:17:01 pm »
Hi and welcome.

For a TEdit, go to properties and set number only to True.
For Combobox, on Keydown:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.  if(Key >= 97) and (Key <= 106) then // 0->9
  4.   exit
  5.  else key := 0;
  6. end;  
  7.  

If you just want to input number, there are TSpinEdit and TFloatSpinEdit in the "Misc" entry.

Happy coding, Pascal Lazarus is a great choice, I's free and your code will run on Windows, Linux and Mac :)

B->
Regards,
Dzandaa

d4eva

  • New Member
  • *
  • Posts: 26
Re: Complete Newby
« Reply #3 on: July 24, 2024, 05:38:14 pm »
Do you actually need to be able to add the value in the ComboBox or you just want to pick a value from a pre-defined list?
If the latter, then you can set the Style of the ComboBox to csDropDownList and pre-populate the ComboBox with values e.g.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.    i: integer;
  4. begin
  5.    for i := 1 to 100 do begin
  6.      ComboBox1.Items.Add(IntToStr(i));
  7.    end;
  8.    ComboBox1.ItemIndex := 0;
  9. end;
  10.  

Aruna

  • Hero Member
  • *****
  • Posts: 513
Re: Complete Newby
« Reply #4 on: July 25, 2024, 01:26:36 am »
Please show me how you do that on a Combobox and once i understand the syntax differences, we can go a step forward.
Peter
For a TComboBox use the Object Inspector and in the events tab OnChange event use the code below:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1Change(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   // Loop through each character in ComboBox1.Text
  6.   for i := 1 to Length(ComboBox1.Text) do
  7.   begin
  8.     // Check if the character is not a digit
  9.     if not (ComboBox1.Text[i] in ['0'..'9']) then
  10.     begin
  11.       // Remove the invalid character
  12.       ComboBox1.Text := Copy(ComboBox1.Text, 1, i - 1) + Copy(ComboBox1.Text, i + 1, Length(ComboBox1.Text) - i);
  13.       // Set the cursor position after the valid characters
  14.       ComboBox1.SelStart := i - 1;
  15.     end;
  16.   end;
  17. end;

For a TEdit like @Dzandaa said you can go to properties and set numbers only to True. You can also do this programmatically.
The exact same code as for the TComboBox but change TComboBox to TEdit in the code. Use the Object Inspector and in the events tab
OnChange event use the code below:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.   // Loop through each character in Edit1.Text
  6.   for i := 1 to Length(Edit1.Text) do
  7.   begin
  8.     // Check if the character is not a digit
  9.     if not (Edit1.Text[i] in ['0'..'9']) then
  10.     begin
  11.       // Remove the invalid character
  12.       Edit1.Text := Copy(Edit1.Text, 1, i - 1) + Copy(Edit1.Text, i + 1, Length(Edit1.Text) - i);
  13.       // Set the cursor position after the valid characters
  14.       Edit1.SelStart := i - 1;
  15.     end;
  16.   end;
  17. end;

« Last Edit: July 25, 2024, 01:30:42 am by Aruna »

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Complete Newby
« Reply #5 on: July 25, 2024, 05:32:23 am »
I guess you guys showed off enough now  :o

But I am sure a "." maybe needed in the equation too.

In any case, simply dropping a MaskEdit on the form and setting mask to ###.## and enable the sets just under it you can
get a nice edit box with formatted decimal entry.

 Also, there are the SpinEdit buttons which support both the integer and fractional formats.

Happy camping.
 :(
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 2737
Re: Complete Newby
« Reply #6 on: July 25, 2024, 08:50:12 am »
I guess you guys showed off enough now  :o

But I am sure a "." maybe needed in the equation too.

In any case, simply dropping a MaskEdit on the form and setting mask to ###.## and enable the sets just under it you can
get a nice edit box with formatted decimal entry.

 Also, there are the SpinEdit buttons which support both the integer and fractional formats.

Happy camping.
 :(
And now i'm going to throw in Backspace, DEL-Key and whatever else i might have forgotten.... :P
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 82
Re: Complete Newby
« Reply #7 on: July 25, 2024, 10:46:18 am »
Thanks everyone.

I later found some simple example on this forum.
Code: Pascal  [Select][+][-]
  1. begin
  2.     if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;
  3. end;

Like my previous app, it needs to add phone numbers to the combo without duplicating it. Adding works. Keypress now works.
Now to filter out too short numbers and preventing duplication.

I will get there. There are many functional similarities between Basic and Pascal, but to get the syntax right will be a challenge.

Thanks

Peter

Handoko

  • Hero Member
  • *****
  • Posts: 5376
  • My goal: build my own game engine using Lazarus
Re: Complete Newby
« Reply #8 on: July 26, 2024, 09:47:56 am »
You're an experienced programmer, it should be easy for you to learn Pascal Programming Language. I visit FreeBASIC forum occasionally, I see many users use it to write games and graphics related programs. Lazarus as a rapid application development tool, you can write wide range of programs using it relatively easy.

Below are some of the software developed using Lazarus:
https://wiki.freepascal.org/Projects_using_Lazarus_-_Business_Software
https://wiki.freepascal.org/Projects_using_Lazarus_-_Medical_and_Scientific_software
https://wiki.freepascal.org/Projects_using_Lazarus_-_Multimedia
https://wiki.freepascal.org/Projects_using_Lazarus_-_Hobby_software
https://wiki.freepascal.org/Projects_using_Lazarus_-_User_utilities
https://wiki.freepascal.org/Projects_using_Lazarus_-_Educational_software
https://wiki.freepascal.org/Projects_using_Lazarus_-_Communications_software

If you're interested to see how to use Lazarus to solve some programming tasks, the link below has many downloadable source codes available for you to try:
https://wiki.freepascal.org/Portal:HowTo_Demos

Petrus Vorster

  • Jr. Member
  • **
  • Posts: 82
Re: Complete Newby
« Reply #9 on: July 30, 2024, 02:25:08 pm »
Thank you greatly.

I am picking up great speed. Lots of trial and error.
I have to say that this is one heck of a tool.

Lots of language shared with Basic, methods a bit strange here and there, but I will get there.
Rapid indeed. You fellows did some good work here.

Also a big group of people with lots of help.

-Peter

 

TinyPortal © 2005-2018