【问题标题】:Set ComboBox text on selection在选择时设置 ComboBox 文本
【发布时间】:2012-05-30 06:17:09
【问题描述】:

我正在开发一个应用程序,其中我有一个带有长文本值的组合框。由于文本值很大(以字符为 ..20 或更多),要显示在组合框中,要求显示在first 从下拉列表中选择后的字符。 就像在红色标记的图像中一样。如果用户选择第 3 项 3 0.5 to 1.25 Slight 我应该只在组合框中显示 3

所以我尝试了这个

   sTheSelectedValue : string;

  procedure TForm1.ComboBox1Select(Sender: TObject);
  begin
   sTheSelectedValue:=TrimTextAndDisplay(ComboBox1.Text); //send theselected value
   ComboBox1.Text :='';                                   //clear the selection
   ComboBox1.Text:=sTheSelectedValue;                     //now assign as text to combo box   
   Button1.Caption:=ComboBox1.Text;                      //just show the new value on the button.
  end;


   function TForm1.TrimTextAndDisplay(TheText : string): string;
   var
   sTheResult : string;
     begin
        sTheResult :=copy(TheText,0,1); //extract the first value..
        Result     :=sTheResult;
     end;

结果是

按钮似乎显示了正确的值,但没有显示组合框。

我想要的是在组合框中获得3,我似乎无法设置ComboBox1.Text:= 谁能告诉我该怎么做? 像这样从组合框中选择结果应该是

【问题讨论】:

    标签: delphi combobox delphi-7


    【解决方案1】:

    我建议所有者绘制 ComboBox 来处理这个问题。将TComboBox.Style 属性设置为csOwnerDrawFixed,然后仅将数字'1''2''3' 等存储在TComboBox.Items 属性本身中,并使用TComboBox.OnDrawItem 事件来呈现完整的字符串。下拉列表可见,例如:

    var
      sTheSelectedValue : string; 
    
    const
      ItemStrings: array[0..7] of string = (
        '0 to 0.1 Calm (rippled)',
        '0.1 to 0.5 Smooth (wavelets)',
        '0.5 to 1.25 Slight',
        '1.25 to 2.5 Moderate',
        '2.5 to 4 Rough',
        '4 to 6 Very rough',
        '6 to 9 High',
        '9 to 14 Very high');
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
    begin
      ComboBox1.Items.BeginUpdate;
      try
        for I := Low(ItemStrings) to High(ItemStrings) do begin
          ComboBox1.Items.Add(IntToStr(I+1));
        end;
      finally
        ComboBox1.Items.EndUpdate;
      end;
    end; 
    
    procedure TForm1.ComboBox1Select(Sender: TObject); 
    begin 
      sTheSelectedValue := IntToStr(ComboBox1.ItemIndex+1);
      Button1.Caption := sTheSelectedValue;
    end; 
    
    procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
    var
      s: String;
    begin
      if odSelected in State then begin
        ComboBox1.Canvas.Brush.Color := clHighlight;
        ComboBox1.Canvas.Font.Color := clHighlightText;
      end else begin
        ComboBox1.Canvas.Brush.Color := ComboBox1.Color;
        ComboBox1.Canvas.Font.Color := ComboBox1.Font.Color;
      end;
      ComboBox1.Canvas.FillRect(Rect);
      s := IntToStr(Index+1);
      if not (odComboBoxEdit in State) then begin
        s := s + ' ' + ItemStrings[Index];
      end;
      ComboBox1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, s);
      if (State * [odFocused, odNoFocusRect]) = [odFocused] then begin
        ComboBox1.Canvas.DrawFocusRect(Rect);
      end;
    end;
    

    【讨论】:

      【解决方案2】:

      您必须尝试将数据保存在记录中,例如:

       type
       TMyRec = record
        Num:Integer;
        Text:String;
      end;
      
      TMyRecArray = array of TMyRec;
      
      MyRecArray:TMyRecArray;
      

      然后您可以手动设置要在ComboBox中设置的项目(在OnFromCreate上),

      SetLength(MyRecArray,9);
      MyRecArray[0].Num:=1;
      MyRecArray[0].Text:='0 to 0.1 Calm Rippled';
      .
      .
      

      等等。

      然后在组合框中的字符串中只放置数字,并且

      procedure TForm1.ComboBox1Select(Sender: TObject);   
      var
        i:integer;     
      begin         
        for i:=0 to 9 do
        begin
          if ComboBox1.Text=IntToStr(MyRecArray[i].Num) then
            Button1.Caption:=MyRecArray[i].Text; 
        end;
      end;
      

      【讨论】:

      • 好的,但这会将combo box 文本设置为TrimTextAndDisplay result 选择项目?
      • 不,不会,因为我们不会使用 TrimTextAndDisplay,我们只会设置按钮的标题,这是您的目标吗?还是我在这里遗漏了什么?为什么不试一试呢?
      • na,不设置按钮的标题,该按钮仅用于测试。在ComboBox1Select 我正在这样做ComboBox1.Text:=sTheSelectedValue;Button1.Caption:=ComboBox1.Text; 。我将最新的值分配给组合框n,然后将组合框值分配给按钮..按钮显示我想要的值,但组合框仍将大文本值显示为@ 987654328@ 应该在哪里显示 3 就像按钮显示一样
      • 为什么不在 ComboBox 上方放置一个 TEdit,它看起来就像 Combobox,但在这种情况下,您可以在编辑中管理文本。你想看看它的样子吗?
      • 你刚刚提出了一个简单(很棒)的想法,我会尝试返回
      猜你喜欢
      • 2016-11-15
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多