• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

populating a CheckListBox

I have a simple setup. It consist of a CheckListBox, editbox, a button to  sort the list and a button each time it is  clicked will transfer a number to the listbox. I don't need to do anything with the number, just sort it. I tried this: clb1.AddItem(edit1.text) but the compiler complains of not enough parameters. Sure enough, when I search help I find that the correct number of parameters is two. The second parameter is Aobject of type Tobject. I don't know wht this is or where I could find out it's value. Could someone help me.
Also is there a way(maybe a property) to ensure that the text entered is a number and not  alphanumeric characters?
Thanks,
Paul
0
annas
Asked:
annas
  • 4
  • 3
  • 2
  • +2
1 Solution
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
use
clb1.items.add(edit1.text);
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
and -->) to ensure that the text entered is a number and not  alphanumeric characters

you could use VAL

function IsANumber(Value: String): Boolean;
var
  I, Code: Integer;
begin
    Val(Value, I, Code);
  result := Code = 0;
end;

then

   if IsANumber(Edit1.Text) then
      clb1.Items.Add(Edit1.text)
   else
      begin
          MessageBox(0,'Insert a valid numeric value','Error',0);
          Edit1.SetFocus;
     end;
 
0
 
Stuart_JohnsonCommented:
Hi Paul,

The easiest way to test to see if the text entered is alpha or numeric is to try to convert it first:

var
  InputString: String;
  ResultValue: Real;
  ErrorCode: Integer;

begin
  InputString := Edit1.Text;
  Val(InputString, ResultValue, ErrorCode);
  if ErrorCode = 0 then
    clb1.Items.Add(Edit1.Text)
  else
   MessageDlg('The value entered was not numeric', mtError, [mbOK], 0);
end;

The other thing you can do is check when you're entering the text into the Edit box like this:

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  if not (ord(Key) in [48..57, 46, VK_TAB, VK_BACK, VK_DELETE]) then
    Key := #0;
end;

This will check each key press and only allow 0-9 and the delete, back and tab to be pressed.

Hope this helps.

Stu.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Stuart_JohnsonCommented:
Damn you have to be fast :)
0
 
Wim ten BrinkSelf-employed developerCommented:
Guys, please answer the COMPLETE question!

"Also is there a way(maybe a property) to ensure that the text entered is a number and not  alphanumeric characters?"
This has been answered.

"The second parameter is Aobject of type Tobject. I don't know wht this is or where I could find out it's value. Could someone help me."
This one isn't! :-)

Okay, since it's a TObject, it has a few properties that will tell you more about it. The function ClassName will return the class name as string. With ClassNameIs you could check if the class name equals a certain string value. ClassInfo retrurns a pointer to the RTTI information about the class. With ClassParent you could retrieve the parent of the current TObject class to go up the inheritance tree. But all these methods are class methods.
A more interesting way is by using the ClassType function to compare "if Object.ClassType = classtype". Then you can check if the class is exactly the one you think it is. Or use Object.InheritsFrom(classtype) to see if it's an inherited class of classtype (or classtype itself). Finally, there are, of course the use of the 'is' and 'as' keywords. Thus: "if Object is TComponent then (Object as TComponent.DoSomeTComponentThing;"
Which you could also write as: "if Object.InheritsFrom(TComponent) then (Object as TComponent.DoSomeTComponentThing;"
But if you use: "if (Object.ClassType = TComponent) then (Object as TComponent.DoSomeTComponentThing;" then the code only works for TComponent objects but not for anything that is inherited from TComponent!
And if you use: "if Object.Parent.InheritsFrom(TComponent) then (Object as TComponent.DoSomeTComponentThing;" then you check for everything that inherits from TComponent but not TComponent itself.
And there are a lot more interesting combinations to be created this way.

But okay, it's a bit unfair to say the answer wasn't complete since you don't even have to know this. You used the wrong method. You need to use "clb1.items.add(edit1.text);" like Ferruccio68 said. He deserves all the credits. ;-)
0
 
annasAuthor Commented:
Thanks to ALL of you who provide such a fast answer! And thanks for setting me straight. I wonder why I didn't see that method when I looked in the help file?
Annas
0
 
annasAuthor Commented:
Oops! I spoke too soon. I used the code Val(Value, I, Code); but The Val procedure expects an integer and the number I want to input is a decimal number. actually a number less than one. I have a class of fifth graders that I want to teach how to order numbers in a list. The decimal numbers smaller than one is what throws them. The numbers input as a string and I can still sort them but some kid is bound to put something like "cat" in the editbox and I want to check for that.

Since Val won't do it I wonder if StrtoFloat will? How would I put that in the function Ferruccio68 wrote?
Thanks,
Annas
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
val can be used also with real type values....
so you can rewrite it as follows

function IsANumber(Value: String): Boolean;
var
  I: Double;
  Code: Integer;
begin
    Val(Value, I, Code);
  result := Code = 0;
end;

0
 
annasAuthor Commented:
Oops! My mistake! . I separated the assignments and made code the var of type real and I of type integer and it should have been the reverse. I see now that code is the result variable so it doesn't matter(to me) what type it is.
Again, Thanks, you guys!
Annas
0
 
Wim ten BrinkSelf-employed developerCommented:
One warning, though... The val function will also accept '$E' and '1e1' as valid numbers. Guess what? They are valid numbers in Delphi! ;-)
It's just a warning so you won't be suprised... The first one is a hexadecimal value, which delphi converts to 15. The second one is 10 written in "scientific" mode.
0
 
annasAuthor Commented:
Thanks, one odd thing though, during compilation Delphi says "Hint: the variable I is never used!" It doesnt stop compiling and the program has worked just fine.
annas
0
 
SJohnsonCommented:
It's because it's assigned a value that's never used.

Stu.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now