:: MVP ::
|
|
:: RSS ::
|
|
|
Как удалить строку из TStringGrid?
// Способ первый
procedure GridDeleteRow( Grid: TstringGrid; RowNumber: integer );
var
i: integer;
begin
if RowNumber < Grid.RowCount then
begin
Grid.Row := RowNumber;
if ( Grid.Row = Grid.RowCount-1 ) then
Grid.RowCount := Grid.RowCount-1
else
begin
for i := RowNumber to Grid.RowCount-1 do
Grid.Rows[i] := Grid.Rows[i+1];
Grid.RowCount := Grid.RowCount-1;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridDeleteRow( StringGrid1, 1 );
end;
// Способ второй
procedure GridDeleteRow( SG: TStringGrid; RowToDelete: integer );
var
i: integer;
begin
with SG do
begin
if ( RowToDelete >= 0 ) and ( RowToDelete < RowCount ) then
begin
for i := RowToDelete to RowCount - 2 do
Rows[i].Assign( Rows[i+1] );
RowCount := RowCount-1;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridDeleteRow( StringGrid1, 2 );
end;
|
Как удалить колонку из TStringGrid?
// Способ первый
procedure GridDeleteCol( Grid: TstringGrid; ColNumber: integer );
var
i: integer;
begin
if ColNumber < Grid.ColCount then
begin
Grid.Col := ColNumber;
if ( Grid.Col = Grid.ColCount-1 ) then
Grid.ColCount := Grid.ColCount-1
else
begin
for i := ColNumber to Grid.ColCount-1 do
Grid.Cols[i] := Grid.Cols[i+1];
Grid.ColCount := Grid.ColCount-1;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridDeleteCol( StringGrid1, 2 );
end;
// Способ второй
procedure GridRemoveColumn( StrGrid: TStringGrid; DelColumn: integer );
var
i: integer;
begin
if DelColumn <= StrGrid.ColCount then
begin
for i := DelColumn to StrGrid.ColCount-1 do
StrGrid.Cols[i-1].Assign( StrGrid.Cols[i] );
StrGrid.ColCount := StrGrid.ColCount-1;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridRemoveColumn( StringGrid1, 1 );
end;
|
Как проверить, выделена ли ячейка TStringGrid?
function IsCellSelected( StringGrid: TStringGrid; X, Y: Longint ): boolean;
begin
Result := false;
if ( X >= StringGrid.Selection.Left ) and
( X <= StringGrid.Selection.Right ) and
( Y >= StringGrid.Selection.Top ) and
( Y <= StringGrid.Selection.Bottom ) then
Result := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsCellSelected( StringGrid1, 2, 2 ) then
ShowMessage( 'Ячейка [2,2] выделена.' );
end;
|
Как добавить строку в TStringGrid?
procedure GridAddRow(StrGrid: TStringGrid; NewRow: Integer);
var
i: integer;
begin
if NewRow > StrGrid.FixedRows-1 then
begin
StrGrid.RowCount := StrGrid.RowCount+1;
for i := StrGrid.RowCount-1 downto NewRow do
StrGrid.Rows[i].Assign(StrGrid.Rows[i-1]);
StrGrid.Rows[NewRow-1].Text := '';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridAddRow(StringGrid1, 1);
end;
|
Как добавить колонку в TStringGrid?
procedure GridAddCol(StrGrid: TStringGrid; NewCol: Integer);
var
i: integer;
begin
if NewCol > StrGrid.FixedCols-1 then
begin
StrGrid.ColCount := StrGrid.ColCount+1;
for i := StrGrid.ColCount-1 downto NewCol do
StrGrid.Cols[i].Assign( StrGrid.Cols[i-1] );
StrGrid.Cols[NewCol-1].Text := '';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
GridAddCol(StringGrid1, 1);
end;
|
Как программно изменить позицию колонки в StringGrid??
// Способ первый
type
THackStringGrid = class(TStringGrid)
public
procedure MoveColumn(FromIndex, ToIndex: Longint);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
THackStringGrid(StringGrid1).MoveColumn(1, 3);
end;
{ THackStringGrid }
procedure THackStringGrid.MoveColumn(FromIndex, ToIndex: Integer);
begin
inherited;
end;
// Способ второй
procedure MoveCol(SG: TStringGrid; FromCol, ToCol: Integer);
var
TempList: TStringList;
i: Integer;
begin
with SG do
if (FromCol in [0..ColCount-1]) and (ToCol in [0..ColCount-1]) then
begin
TempList := TStringList.Create;
TempList.Assign(Cols[FromCol]);
if FromCol > ToCol then
for i := FromCol downto ToCol+1 do
Cols[i].Assign(Cols[i-1])
else
for i := FromCol to ToCol-1 do
Cols[i].Assign(Cols[i + 1]);
Cols[ToCol].Assign(TempList);
TempList.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MoveCol(StringGrid1, 1, 3);
end;
|
Как программно изменить позицию строки в StringGrid?
// Способ первый
type
THackStringGrid = class(TStringGrid)
public
procedure MoveRow(FromIndex, ToIndex: Longint);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
THackStringGrid(StringGrid1).MoveRow(1, 3);
end;
{ THackStringGrid }
procedure THackStringGrid.MoveRow(FromIndex, ToIndex: Integer);
begin
inherited;
end;
// Способ второй
procedure MoveRow(SG: TStringGrid; FromRow, ToRow: Integer);
var
TempList: TStringList;
i: Integer;
begin
with SG do
if (FromRow in [0..RowCount-1]) and (ToRow in [0..RowCount-1]) then
begin
TempList := TStringList.Create;
TempList.Assign(Rows[FromRow]);
if FromRow > ToRow then
for i := FromRow downto ToRow+1 do
Rows[i].Assign(Rows[i-1])
else
for i := FromRow to ToRow-1 do
Rows[i].Assign(Rows[i+1]);
Rows[ToRow].Assign(TempList);
TempList.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
MoveRow(StringGrid1, 1, 3);
end;
|
Как создать отдельную подсказку (hint) для каждой ячейки StringGrid?
procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
const
{$J+}
OldRow: Integer = -1;
OldCol: Integer = -1;
{$J+}
var
R: Integer;
C: Integer;
begin
StringGrid1.MouseToCell(X, Y, C, R);
with StringGrid1 do
if (C > 0) and (C < ColCount) and (R > 0) and (R < RowCount) then
begin
if ((OldRow <> r) or(OldCol <> c)) then
begin
OldRow := r;
OldCol := c;
StringGrid1.Hint := IntToStr(r) + #32 + IntToStr(c);
Application.ActivateHint(Mouse.CursorPos);
// или так
// Application.ActivateHint(StringGrid1.ClientToScreen(Point(X, Y)));
end;
end
else
begin
OldRow := -1;
OldCol := -1;
Application.CancelHint; // Application.HideHint;
end;
end;
// Или немного по-другому, если хотим
// чтобы подсказка перемещалась за курсором
procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
const
{$J+}
OldRow: Integer = -1;
OldCol: Integer = -1;
{$J+}
var
R: Integer;
C: Integer;
begin
StringGrid1.MouseToCell(X, Y, C, R);
with StringGrid1 do
if (C > 0) and (C < ColCount) and (R > 0) and (R < RowCount) then
begin
if ((OldRow <> r) or(OldCol <> c)) then
begin
OldRow := r;
OldCol := c;
StringGrid1.Hint := IntToStr(r) + #32 + IntToStr(c);
end;
Application.ActivateHint(Mouse.CursorPos);
// или так
// Application.ActivateHint(StringGrid1.ClientToScreen(Point(X, Y)));
end
else
begin
OldRow := -1;
OldCol := -1;
Application.CancelHint; // Application.HideHint;
end;
end;
|
При использовании материала - ссылка на сайт обязательна
|
|