在stringgrid中每格中只能输入0或1(每格中只能有1个字符,0或者是1,100、000等都是不允许的),怎么做?高手指教

解决方案 »

  1.   

    onkeypress事件
    if key 不在0和1 then
    key=#0
      

  2.   

    或者你把那個輸入的框改為ListBox,然後在裡面賦值為0與1,還有一個空值,讓用戶只能選擇0與1
      

  3.   

    procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
    begin
      if not (Key in ['1','0']) then Key:=#0
      //建议写成
      //if not (Key in ['1','0',#13,#8]) then Key:=#0
      //这样就可以回车和退格了
    end;
      

  4.   

    stringgrid的onkeypress事件中
    if not (key in ['0','1',#13,#8]) and 
            (length(stringgrid1.cells[stringgrid1.row,stringgrid1.col]) <> 0) then
    begin
      key := #0;
    end;
      

  5.   

    procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char);
    begin
       if key in ['1'] then
       begin
         stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='';
         stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='1';
       end
       else
       if key in ['0'] then
       begin
         stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='';
         stringgrid1.Cells[stringgrid1.Col,stringgrid1.Row]:='0';
       end
       else
         begin
           key:=#0;
           exit;
         end
    end;
      

  6.   

    ////////////////
    ..........
      private
        { Private declarations }
        currvalue : String;
    ///////////////.............
    procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol,
      ARow: Integer; var Value: String);
    begin
       currvalue := value;
    end;procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
      ARow: Integer; const Value: String);
    begin  if ( (StringGrid1.Cells[ACol,ARow] <> '1') and (StringGrid1.Cells[ACol,ARow] <>'0')) then
       StringGrid1.Cells[ACol,ARow] := currvalue;
    end;_____________________________________________________________________暮春三月,羊欢草长,天寒地冻,问谁饲狼?人心怜羊,狼心独怆,天心难测,世情如
    霜……