6

テキスト値が長いコンボボックスを使用しているアプリケーションで作業しています。テキスト値が大きいため(文字数..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:= は誰もそれを行う方法を教えてもらえないようです?コンボボックスからの選択でこのように結果は次のようになります ここに画像の説明を入力してください

4

2 に答える 2

13

これを処理するために、所有者がComboBoxを描画することをお勧めします。TComboBox.Styleプロパティをに設定し、プロパティ自体にcsOwnerDrawFixed数字、、、などのみを格納し'1'、ドロップダウンリストが表示されているときにイベントを使用して完全な文字列をレンダリングします。例:'2''3'TComboBox.ItemsTComboBox.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;
于 2012-05-30T08:44:01.100 に答える
-1

たとえば、データをレコードに保存する必要があります。

 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;
于 2012-05-30T07:03:12.377 に答える