2012-05-30 67 views
6

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

enter image description here

所以我想这

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; 

结果是enter image description here

按钮似乎显示正确的值,但不是组合框。

我想要的是让3在ComboBox,我似乎无法设置ComboBox1.Text:= 任何一个可以告诉我怎么办呢? 这样从组合框中选择的结果应该是 enter image description here

回答

12

我建议业主绘制组合框来处理这个问题。在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; 
+0

G.E.N.I.U.S。有效 – PresleyDias

-1

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

type 
TMyRec = record 
    Num:Integer; 
    Text:String; 
end; 

TMyRecArray = array of TMyRec; 

MyRecArray:TMyRecArray; 

,那么你可以手动设置的项目是在组合框设置(上OnFromCreate

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

等。

然后在下拉列表strigns地方只有数字和

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; 
+0

好的,但是这会在选择的项目上将“组合框”文本设置为“TrimTextAndDisplay结果”? – PresleyDias

+0

不,它不会,因为我们不会使用TrimTextAndDisplay,我们只会设置按钮的标题,这是你的目标吗?或者我在这里错过了什么?为什么不试一试呢? – Zeina

+0

na,不设置按钮的标题,该按钮仅用于测试。在'ComboBox1Select'中我是这么做的'ComboBox1.Text:= sTheSelectedValue; Button1.Caption:= ComboBox1.Text;'我将最新的值分配给combox框n然后combobox的值到按钮..按钮显示我想要的值,但combox仍然显示大的文本值为'3 0.5到1.25轻微',它应该显示'3',就像现在按钮显示 – PresleyDias

相关问题