Solved

populating a CheckListBox

Posted on 2004-09-27
12
306 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 62
Delphi XE10, MySQL Query 4 126
how to center only a line in richedit? 4 52
control image tags in a string ? 12 110
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

17 Experts available now in Live!

Get 1:1 Help Now