Solved

populating a CheckListBox

Posted on 2004-09-27
12
309 Views
Last Modified: 2012-05-05
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
Comment
Question by:annas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12159086
use
clb1.items.add(edit1.text);
0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 100 total points
ID: 12159122
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
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 12159142
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 12159146
Damn you have to be fast :)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12159500
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
 

Author Comment

by:annas
ID: 12161394
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
 

Author Comment

by:annas
ID: 12162785
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12162860
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
 

Author Comment

by:annas
ID: 12162881
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12164460
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
 

Author Comment

by:annas
ID: 12166586
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
 
LVL 1

Expert Comment

by:SJohnson
ID: 12166595
It's because it's assigned a value that's never used.

Stu.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question