Autor Beitrag
oroblram
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21

Win XP
Delphi 7
BeitragVerfasst: Mo 24.04.06 13:29 
Hallo zusammen.

Ich habe ein Stringgrid, bei dem die Zellen editierbar sind (goEditing -> true).
Wenn die User etwas in einer Stringgrid-Zelle ändern, soll die Länge z.B. auf 10 beschränkt sein. Wie stell ich das an?
Sy-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 177



BeitragVerfasst: Mo 24.04.06 14:35 
Hi!

Ich hab da den folgenden Script gebastelt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
  const Value: string);
var
  bis:integer;
begin
  bis:=10;

  if(length(tstringgrid(sender).Cells[acol,arow])>=bis)then
  begin
    tstringgrid(sender).Cells[acol,arow]:=copy(tstringgrid(sender).Cells[acol,arow],1,bis);
    tstringgrid(sender).enabled:=false;
    tstringgrid(sender).enabled:=true;
  end;
end;


Also auf dem "onSetEditText" des stringgrids.
Du kannst nun auch das bis von der Spalte (aCol) abhängig machen.

Gruß
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 24.04.06 14:53 
Hallo,

für solche Zwecke stellt SetEditText Value zur Verfügung.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
  ARow: Integer; const ValueString);
var MaxCount : integer;
begin
  MaxCount := 10;
  if Length(Value) > 10 then
    begin
    StringGrid1.Cells[ACol,ARow] := Copy(Value,1,10);
    StringGrid1.EditorMode := True;
    end;
end;

Wird dann noch mit EditorMode der Inplaceeditor wieder aktiviert,
ist die Eingabe in solche eingeschränkten Zellen komfortabler.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Sy-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 177



BeitragVerfasst: Mo 24.04.06 15:01 
hehe,
ich hab mich damals damit abgequält und hab nur editormode auf false versucht weil ich editormode:=true als unwirksam gesehen habe. :wink:
oroblram Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21

Win XP
Delphi 7
BeitragVerfasst: Mo 24.04.06 15:26 
Danke für eure Hilfe!

Hab die zweite Version gewählt und noch ein bisschen abgeändert, damit ich den einzelnen Spalten
unterschiedliche Werte geben kann.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,  
  ARow: Integer; const Value: String);  
var MaxCount : integer;  
begin  
  MaxCount := 10;  
  if (Length(Value)) > 10 and (Stringgrid1.Col = 1) then  
    begin  
    StringGrid1.Cells[1,ARow] := Copy(Value,1,10);  
    StringGrid1.EditorMode := True;  
    end;  
end;
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mo 24.04.06 20:40 
Hallo,

in meinem Code hab ich auf die schnelle die Variable MaxCount deklariert und initialisiert, aber im nachfolgenden Code nicht verwendet :angel:
Der Code muss natürlich so aussehen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,  
  ARow: Integer; const Value: String);  
var MaxCount : integer;  
begin  
  MaxCount := 10;  
  if Length(Value) > MaxCount then  
    begin  
    StringGrid1.Cells[ACol,ARow] := Copy(Value,1,MaxCount);  
    StringGrid1.EditorMode := True;  
    end;  
end;


user profile iconoroblram hat folgendes geschrieben:
Hab die zweite Version gewählt und noch ein bisschen abgeändert, damit ich den einzelnen Spalten
unterschiedliche Werte geben kann.
hast Du in getestet :?:
Bei der Zeile :
ausblenden Delphi-Quelltext
1:
if (Length(Value)) > 10 and (Stringgrid1.Col = 1then					

wird der Compiler die Meldung ausgeben:
Operator ist auf diesen Operandentyp nicht anwendbar
In dem Fall muss man die Klammern anders setzen und außerdem ist es besser bei ACol zu bleiben, denn das ist die Spalte in der Editiert wird.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,  
  ARow: Integer; const Value: String);  
var MaxCount : integer;  
begin  
  MaxCount := 10;  
  if (Length(Value) > 10) and (ACol = 1then
    begin  
    StringGrid1.Cells[ACol,ARow] := Copy(Value,1,10);  
    StringGrid1.EditorMode := True;  
    end;  
end;

Aber unterschiedliche Werte wirst Du den Spalten damit nicht zuweisen können.

Um bei einzelnen Spalten unterschiedliche Grenz-Werte anzusetzen, schlage ich den folgenden Code vor:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol,
  ARow: Integer; const Value: String);
var MaxCount : integer;
begin
  with StringGrid1 do
    if ACol in [1,4then//<--- anpassen
      begin
      case ACol of
        1 : MaxCount := 5;  //<--- anpassen
        4 : MaxCount := 10//<--- anpassen
        // : MaxCount := ;  //<--- weitere
        end;
      if Length(Value) > MaxCount then
        begin
        Cells[ACol,ARow] := Copy(Value,1,MaxCount);
        EditorMode := True;
        end;
      end;
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )