今天第一次使用TShellListView控件,用它的目的是为了向资源管理器一样显示一个目录的文件,用TListView的话显示文件图标有困难,也就是这点求完美的性格让我尝试着TShellListView控件的使用,网上相关资料很少,为此我也浪费了近一天的时间,原来这个控件在非windows中文版操作系统上当对应的目录autorefalshe设置为true时会报错,在就要换控件之前我想了一种变通的解决方法当对应目录内容有改变时先将shelllistview对应的目录改为另外一个目录,然后再指向现有的目录,结果算解决了这个问题。感觉TshellListView跟TListview差不多,主要是对已有的文件以及选中文件的判断,可参考下面:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ShellCtrls, StdCtrls;
type
TForm1 = class(TForm)
ShellListView1: TShellListView;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function SelectedFiles(AShellView: TShellListView): TStringList;
var
i:integer ;
begin
Result := TStringList.Create;
for i := 0 to AShellView.Items.Count - 1 do
// is the item selected?
if AShellView.Items[i].Selected = True then
// Folders can also refer to files, which is why we check isFolder
// before adding the filepath to the result
if AShellView.folders[i].IsFolder = False then
// add filepath and filename to result
Result.Add(AShellView.Folders[i].PathName);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
FileList: TStringList;
begin
try
FileList := TStringList.Create; //create stringlist to contain filenames
FileList := SelectedFiles(ShellListView1); //populate tstringlist
if FileList.Count = 0 then Exit; //exit if no files selected
for i := 0 to FileList.Count - 1 do
Memo1.Lines.add(FileList[i]); //cycle through each filename and do something
finally
FreeAndNil(FileList); //free tstringlist when finished
end;
end;
end.
如何取得TShellListView控件选中的文件(夹)的路径,包括文件(夹)名?ShellListView1.SelectedFolder.PathName
如何用sql语句修改表结构,参考如下:
--1. 删除原主键
if exists(select * from sysobjects where xtype= 'PK ' and parent_obj=object_id(N 'uinfor '))
begin
--使用动态语句可以保证无论主键名是怎么定义都可以正常删除
declare @s nvarchar(4000)
select @s=N 'alter table uinfor drop constraint '+quotename(name)
from sysobjects where xtype= 'PK ' and parent_obj=object_id(N 'uinfor ')
exec(@s)
end
--2. 删除字段
if exists(select * from syscolumns where id=object_id(N 'uinfor ') and name= 'u_id ')
alter table uinfor drop column u_id;
--3. 添加字段
alter table uinfor add u_note varchar(1) NULL;
--4. 修改字段
if exists(select * from syscolumns where id=object_id(N 'uinfor ') and name= 'u_key ')
ALTER TABLE uinfor ALTER COLUMN u_key VARCHAR(18) NOT NULL;
--5. 添加新主键
if not exists(select * from sysobjects where xtype= 'PK ' and parent_obj=object_id(N 'uinfor '))
alter table uinfor add primary key(u_key ,u_name );
没有评论:
发表评论