TRzComboBox

Viewing 3 reply threads
  • Author
    Posts
    • #397
      Alvaro Perez
      Participant

        Hi Ray .. I am pretty new with your components and I am trying to use the ComboBox .

        After adding lines to this combobox with the AddItemValue property, how can I access
        the value of the selected item ??

        Many thanks

        Best Regards

      • #398
        Ray Konopka
        Keymaster

          Hi,
          You can simply use the Value property of the combo box. If an item in the combo box is selected then the Value property returns the corresponding value for that item. If no item is selected, then Value returns an empty string.

          Ray

        • #402
          Alvaro Perez
          Participant

            Hi Ray, thanks for the answer, but can you help me please to do that ?

            Assuming that I have 10 rows with String and value for each in the combo, how can I retrieve the value for the selected row ??

            I’ve addes lines to the combo this way.

            Combobox.AddValue( ‘string1’, 1 )
            Combobox.AddValue( ‘string2’, 2 ) and so on..

            How can I retrieve the value 2 for the second row if selected ??..

            Maybe its combobox.items[combo.itemindex].value ?? did not try it..just thinking..

            Thanks so much.

          • #406
            Ray Konopka
            Keymaster

              I apologize for the delay in responding. I did not realize you asked a follow-up question. Consider the following code. I have dropped a TRzComboBox, a TRzButton, and a TRzLabel onto a form.

              procedure TForm29.FormCreate(Sender: TObject);
              begin
                RzComboBox1.AddItemValue( 'Item One', '1' );
                RzComboBox1.AddItemValue( 'Item Two', '2' );
                RzComboBox1.AddItemValue( 'Item Three', '3' );
                RzComboBox1.AddItemValue( 'Item Four', '4' );
                RzComboBox1.AddItemValue( 'Item Five', '5' );
              end;

              The above code adds several items and values to the list. You can get the Values by using the Values list property. For example:

              procedure TForm29.RzButton1Click(Sender: TObject);
              begin
                RzLabel1.Caption := RzComboBox1.Values[ RzComboBox1.ItemIndex ];
              end;

              But the problem with this approach is that you need to protect against ItemIndex being -1, which will lead to an Index Out Of Bounds exception. A better approach is to simply use the Value property as shown below:

              procedure TForm29.RzButton1Click(Sender: TObject);
              begin
                RzLabel1.Caption := RzComboBox1.Value;
              end;

              Ray

          Viewing 3 reply threads
          • You must be logged in to reply to this topic.