Solved

populating a CheckListBox

Posted on 2004-09-27
12
305 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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

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

Accepted Solution

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

Expert Comment

by:Stuart_Johnson
Comment Utility
Damn you have to be fast :)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:annas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It's because it's assigned a value that's never used.

Stu.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now