是不是不是一种类型的不能赋值复制?
procedure TForm7.Button1Click(Sender: TObject);begin
Memo2.Lines.Assign(Memo1.Lines); OK!
ListBox1.Items.Assign(Memo1.Lines); OK!
//StringGrid1.Assign(Memo1.Lines); 报错!
//Memo1.Lines.Assign(StringGrid1); 报错!
end; StringGrid1.Assign(Memo1.Lines);
你这样写肯定是不行的
StringGrid1 里有那么多属性,Memo1.Lines 应该复制给谁呢?
TPersistent 类派生的对象才可以 Assign
你写成 StringGrid1.Cols.Assign(Memo1.Lines); 就不会报错了
同样 Memo1.Lines.Assign(StringGrid1.Rows);就没问题
lies2014 发表于 2024-2-22 23:09
StringGrid1.Assign(Memo1.Lines);
你这样写肯定是不行的
StringGrid1 里有那么多属性,Memo1.Lines 应该 ...
还有一问题 StringGrid1怎么保存数据成.txt? 冥界3大法王 发表于 2024-2-23 08:00
还有一问题 StringGrid1怎么保存数据成.txt?
自己写代码读取stringgrid1的内容,保存到txt文件 homejun 发表于 2024-2-23 09:02
自己写代码读取stringgrid1的内容,保存到txt文件
@homejun 大神能不能表演下:
procedure TForm7.给每一个单元格赋值Click(Sender: TObject); //给每一个单元格赋值
var
c, r: Integer;
begin
for c := 0 to StringGrid1.ColCount - 1 do
for r := 0 to StringGrid1.RowCount - 1 do
StringGrid1.Cells := Format('%d,%d', );
end;
能不能用这个修改下?
使之能简单的遍历读取和写入TXT于StringGrid1? unit Unit7;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.Grids;
type
TForm7 = class(TForm)
按行读取方式一: TButton;
Edit1: TEdit;
按行分割字符串: TButton;
按行读取方式二: TButton;
StringGrid1: TStringGrid;
表格添加数据: TButton;
c: TButton;
得到txt总行数: TButton;
遍历StringGrid: TButton;
写入空行: TButton;
procedure 按行读取方式一Click(Sender: TObject);
procedure 按行分割字符串Click(Sender: TObject);
procedure 按行读取方式二Click(Sender: TObject);
procedure 表格添加数据Click(Sender: TObject);
procedure cClick(Sender: TObject);
procedure 得到txt总行数Click(Sender: TObject);
procedure 遍历StringGridClick(Sender: TObject);
procedure 写入空行Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form7: TForm7;
implementation
{$R *.dfm}
procedure TForm7.按行读取方式一Click(Sender: TObject);
var
txtlist: TStringList;
m: integer;
begin
txtlist := TStringList.Create;
txtlist.LoadFromFile(Edit1.Text);
for m := 0 to txtlist.Count - 1 do
begin
ShowMessage(txtlist.Strings);
end;
end;
procedure TForm7.表格添加数据Click(Sender: TObject);
var
i, j: integer;
arr: array of string; //200行
begin
StringGrid1.Cells := '路径';
StringGrid1.Cells := '完成度';
StringGrid1.Cells := '当前思绪';
StringGrid1.Cells := '翠翠1';
StringGrid1.Cells := '艳艳2';
StringGrid1.Cells := '灵灵3';
StringGrid1.Cells := '风风4';
StringGrid1.Cells := '火火5';
StringGrid1.Cells := 'aaa';
StringGrid1.Cells := 'bbb';
StringGrid1.Cells := 'ccc';
StringGrid1.Cells := 'ddd';
StringGrid1.Cells := 'eee';
StringGrid1.Cells := '111';
StringGrid1.Cells := '222';
StringGrid1.Cells := '333';
StringGrid1.Cells := '444';
StringGrid1.Cells := '555';
StringGrid1.ColWidths := 100; //把第一列隐藏即可
StringGrid1.ColWidths := 1100;
StringGrid1.ColWidths := 400;
StringGrid1.ColWidths := 400;
end;
procedure TForm7.遍历StringGridClick(Sender: TObject);
var
i, j: Integer;
begin
for i := 0 to StringGrid1.RowCount - 1 do
begin
for j := 0 to StringGrid1.ColCount - 1 do
begin
ShowMessage('Cell[' + IntToStr(i) + ',' + IntToStr(j) + ']: ' + StringGrid1.Cells); // 访问StringGrid1中每个单元格的内容
if StringGrid1.Cells = '' then
begin
ShowMessage('读取到的单元格为为空'); //那就写入空行
end;
end;
end;
end;
procedure TForm7.cClick(Sender: TObject);
var
ATextFile: TextFile;
Line: string;
LineNumber: Integer;
begin
AssignFile(ATextFile, 'X:\要读取的文件.txt');
Reset(ATextFile);
try
LineNumber := 1;
for LineNumber := 1 to 3 do
begin
Readln(ATextFile, Line);
ShowMessage(Line);
Writeln('Line ', LineNumber, ': ', Line);
end;
finally
CloseFile(ATextFile);
end;
end;
procedure TForm7.按行分割字符串Click(Sender: TObject);
var
StringList: TStringList; //与分割有关
要分割的字符串: string; //与分割有关
AI: integer; //与分割有关
begin
StringList := TStringList.Create;
try
要分割的字符串 := 'Apple▲Banana▲Orange';
StringList.Delimiter := '▲'; //设置分隔符为▲
StringList.DelimitedText := 要分割的字符串; //使用DelimitedText属性分割字符串
for AI := 0 to StringList.Count - 1 do //遍历列表并打印每个分割后的字符串
begin
ShowMessage(StringList + '+' + AI.ToString);
end;
finally
StringList.Free;
end;
end;
procedure TForm7.按行读取方式二Click(Sender: TObject);
var
txtFile: TextFile;
s: string;
begin
AssignFile(txtFile, 'D:\d.txt');
Reset(txtFile);
while not eof(txtFile) do
begin
readln(txtFile, s);
ShowMessage(s);
end;
end;
procedure TForm7.写入空行Click(Sender: TObject);
var
TextFile: TStreamWriter;
begin
// 创建或打开要写入的.txt文件
TextFile := TStreamWriter.Create('example.txt', False);
try
// 写入空行
TextFile.WriteLine(); //空行这么写?
finally
TextFile.Free;
end;
end;
procedure TForm7.得到txt总行数Click(Sender: TObject);
var
A_TextFile: TextFile;
总行数: Integer;
读取到的行内容: string;
begin
AssignFile(A_TextFile, '要读取的文件.txt');
Reset(A_TextFile);
总行数 := 0;
try
while not Eof(A_TextFile) do
begin
Readln(A_TextFile, 读取到的行内容);
Inc(总行数);
end;
// Writeln('Total number of lines in a.txt: ', LineCount);
ShowMessage('总行数是:' + 总行数.ToString);
finally
CloseFile(A_TextFile);
end;
end;
end.
页:
[1]