Solved

populating a CheckListBox

Posted on 2004-09-27
12
307 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 100
Delphi application Soap connection 5 103
RESTRequest Parameter 4 30
Twebbrowser in Fire monkey android scrolling to bottom 1 30
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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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