2012-05-30 26 views
6

Estoy trabajando en una aplicación en la que tengo un cuadro combinado con valores de texto largos.Puesto que los valores de texto son grandes (en términos de caracteres ..20 o más), para mostrar en el cuadro combinado, el requisito debía mostrarse en el carácter first después de seleccionar desde el menú desplegable. Como en la imagen marcada en rojo. si el usuario selecciona el 3º elemento 3 0.5 to 1.25 Slight, solo debería mostrar el 3 en el cuadro combinado.Establecer texto de ComboBox en la selección

enter image description here

Así que probé este

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; 

El resultado es enter image description here

El botón parecen mostrar el valor apropiado, pero no en el cuadro combinado.

lo que quiero es conseguir 3 en el cuadro combinado, no puedo parecen establecer ComboBox1.Text:= puede alguien decirme cómo hacerlo? como este en la selección de en el cuadro combinado el resultado debe ser enter image description here

Respuesta

12

Yo sugeriría propietario-dibujar el cuadro combinado para manejar esto. Establecer la propiedad TComboBox.Style a csOwnerDrawFixed, a continuación, guardar sólo los números de '1', '2', '3', etc en la propiedad TComboBox.Items sí mismo y utilizar el evento TComboBox.OnDrawItem para hacer que las cuerdas completos cuando la lista desplegable es visible, por ejemplo:

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. funcionó – PresleyDias

-1

Tienes que tratar de guardar los datos en un registro, por ejemplo:

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

TMyRecArray = array of TMyRec; 

MyRecArray:TMyRecArray; 

entonces se puede establecer manualmente los elementos a ser establecer en el ComboBox (en la OnFromCreate),

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

y así sucesivamente.

continuación, en el cuadro combinado strigns lugar sólo los números, y

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

bien, pero esto establecerá el texto del 'cuadro combinado' como' resultado TrimTextAndDisplay' en la selección del elemento? – PresleyDias

+0

no, no lo hará, porque no vamos a usar TrimTextAndDisplay, solo estableceremos el título del botón, ¿es este tu objetivo? o me estoy perdiendo algo aquí? ¿Por qué no intentarlo? – Zeina

+0

na, sin configurar el título del botón, ese botón es solo para prueba. en 'ComboBox1Select' estoy haciendo esto' ComboBox1.Text: = sTheSelectedValue; Button1.Caption: = ComboBox1.Text; '.i estoy asignando el valor más nuevo a la caja de la caja n, luego el valor del cuadro combinado al botón ... el botón muestra el valor que quiero, pero la caja todavía muestra el valor de texto grande como '3 0.5 a 1.25 Ligero' donde debería mostrar' 3', como el botón muestra – PresleyDias

Cuestiones relacionadas