delphi listbox 使用
- 格式:doc
- 大小:28.50 KB
- 文档页数:2
DelphiListView基本⽤法⼤全[delphi] Delphi ListView基本⽤法⼤全//增加项或列(字段)ListView1.Clear;ListView1.Columns.Clear;ListView1.Columns.Add;ListView1.Columns.Add;ListView1.Columns.Add;ListView1.Columns.Items[0].Caption:='id';ListView1.Columns.Items[1].Caption:='type';ListView1.Columns.Items[2].Caption:='title';ListView1.Columns.Items[2].Width:=300;Listview1.ViewStyle:=vsreport;Listview1.GridLines:=true; //注:此处代码也可以直接在可视化编辑器中完成,也可写成以下这样beginwith listview1 dobeginColumns.Add;Columns.Add;Columns.Add;ViewStyle:=vsreport;GridLines:=true;columns.items[0].caption:='进程名';columns.items[1].caption:='进程ID';columns.items[2].caption:='进程⽂件路径';Columns.Items[0].Width:=100;Columns.Items[1].Width:=100;Columns.Items[2].Width:=150;endend;//增加记录with listview1.items.add dobegincaption:='1212';subitems.add('hh1');subitems.add('hh2');end;//删除listview1.items.delete(0);//从数据库表⾥读取数据写⼊ListviewvarTitem:Tlistitem; //此处⼀定要预定义临时记录存储变量.beginListView1.Items.Clear;with adoquery1 dobeginclose;sql.Clear;sql.Add('select spmc,jg,sl from kcxs');Open;ListView1.Items.Clear;while not eof dobeginTitem:=ListView1.Items.add;Titem.Caption:=FieldByName('spmc').Value;Titem.SubItems.Add(FieldByName('sl').Value);Titem.SubItems.Add(FieldByName('jg').Value);next;//删除ListView1.DeleteSelected;//如何取得ListView中选中⾏的某⼀列的值procedure TForm1.Button2Click(Sender: TObject);beginShowMessage(ListView1.Selected.SubItems.Strings[1]); //返回选中⾏第三列中的值end;showMessage(listView1.Selected.Caption); //返回选中⾏第⼀列的值.第1列的值: -->>> ListView1.Selected.Caption第i列的值(i>1):-->>> ListView1.Selected.SubItems.Strings[i]ListView1.Items.Item[1].SubItems.GetText); //取得listview某⾏某列的值Edit2.Text := listview1.Items[i].SubItems.strings[0]; //读第i⾏第2列返回选中⾏所有⼦列值.是以回车符分开的,你还要从中剥离出来你要的⼦列的值。
delphi listbox 实例Delphi ListBox 实例ListBox 是 Delphi 中常用的控件之一,用于显示列表或者集合中的数据。
它提供了简单而灵活的功能,方便用户对数据进行选择和操作。
在本文中,我们将介绍如何使用Delphi 中的ListBox 控件,并给出一个实例来演示其功能和用法。
一、ListBox 控件的基本介绍ListBox 是一个用于显示文本数据的列表框控件。
它可以显示多个项目,每个项目可以包含一个文本字符串。
用户可以使用鼠标或键盘来选择列表中的项目,并进行相应的操作。
ListBox 控件提供了一系列属性和事件,使开发者可以方便地操作和控制列表中的数据。
二、ListBox 的基本用法使用 ListBox 控件非常简单,只需要以下几个步骤:1. 在Delphi 的窗体设计器中,找到ListBox 控件,并将其拖拽到窗体上。
2. 可以通过设置ListBox 的属性来改变其外观和行为,例如设置列表的大小、边框样式、字体等。
3. 通过代码向ListBox 中添加项目。
可以使用Items 属性来访问ListBox 中的项目集合,并使用 Add 方法来添加新的项目。
例如:```ListBox1.Items.Add('项目1');ListBox1.Items.Add('项目2');ListBox1.Items.Add('项目3');```4. 通过事件来响应用户对列表的操作。
ListBox 提供了一系列的事件,例如 OnClick、OnDblClick、OnKeyDown 等。
开发者可以根据需要来选择注册相应的事件,并在事件处理程序中编写相应的代码来实现功能。
三、ListBox 实例演示下面我们通过一个实例来演示ListBox 的使用。
假设我们需要开发一个简单的学生信息管理系统,其中包含一个窗体和一个ListBox 控件。
我们可以使用ListBox 来显示学生的姓名列表,并提供添加、编辑和删除学生姓名的功能。
delphi listbox 使用listbox删除删除选中的某个:procedure TForm1.Button1Click(Sender: TObject); vari:integer;begin i:=0; while i<listbox1.Count do//删除掉前一个后,下一个会上移,所以用while if listbox1.selected[i] then listbox1.Items.delete(i) else i:=i+1;end; 删除listbox中所有的内容:procedure TForm1.Button2Click(Sender: TObject);var i:integer; begin for i:=0 to listbox1.Items.Count-1 do listbox1.Items.Clear;// ShowMessage(listbox1.Items[listbox1.ItemIndex]); 显示当前数据按钮事件向上移动procedure TFormMain.RzBmpButton5Click(Sender: TObject);varActiveItem: Integer;beginwith lst2 dobeginActiveItem := ItemIndex;if (ItemIndex > 0) thenbeginItems.Move(ItemIndex, ItemIndex - 1);lst2.SetFocus;lst2.Selected[ActiveItem - 1] := True;end;end;end;向下移动procedure TFormMain.RzBmpButton6Click(Sender: TObject);varActiveItem: Integer;beginwith lst2 dobeginActiveItem := ItemIndex;if (ItemIndex >= 0) and (ItemIndex < Items.Count - 1) thenbeginItems.Move(ItemIndex, ItemIndex + 1);lst2.SetFocus;lst2.Selected[ActiveItem + 1] := True;end;end;end;拖拽事件procedure TFormMain.lst2MouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);beginStartingPoint.X := X;StartingPoint.Y := Y;end;procedure TFormMain.lst2DragDrop(Sender, Source: TObject; X, Y: Integer); varDropPosition, StartPosition: Integer;DropPoint: TPoint;beginDropPoint.X := X;DropPoint.Y := Y;with Source as TRzListBox dobeginStartPosition := ItemAtPos(StartingPoint, True);DropPosition := ItemAtPos(DropPoint, True);Items.Move(StartPosition, DropPosition);end;end;procedure TFormMain.lst2DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);beginAccept := Source = lst2;end;。
Delphi获取⽂件夹路径的三种⽅式问题⼀:在Win98中右击“我的⽂档”,选属性,在弹出的“我的⽂档属性”窗⼝中点击“浏览”按钮就会弹出⼀个“浏览⽂件夹”对话框。
请问这个对话框是怎么做出来的? 答案:要做这个对话框有三种⽅法。
(⼀)第⼀种⽅法是⽤Delphi提供的组件(在Win 3.1⾯板上)模仿在上⾯看到的对话框⾃⼰组装⼀个“浏览⽂件夹”窗体。
具体的做法是: 1.在你的Project⾥增加⼀个BorderStyle为bsDialog的新窗体; 2.放置⼀个DirectoryListBox组件; 3.放置⼀个DriveComboBox组件,设置DirList为DirectoryListBox1; 4.然后再放上两个Button。
⼀个“确定”(ModalResult为mrOk),⼀个“取消” (ModalResult为mrCancel); 5.最后只要在调⽤这个浏览⽂件夹的地⽅加上⼀下代码就算⼤功告成了: if Form2.ShowModal = mrOk then Memo1.Lines.Add(Form2.DirectoryListBox1.Directory); (⼆)第⼆种⽅法,在Delphi中可以通过调⽤SelectDirectory函数得到这种效果。
SelectDirectory在Delphi 4中的申明如下(请注意,⼀共有两个重载的申明): type TSelectDirOpt = (sdAllowCreate, sdPerformCreate, sdPrompt); TSelectDirOpts = set of TSelectDirOpt; function SelectDirectory(var Directory: string; Options: TSelectDirOpts; HelpCtx: Longint): Boolean; overload; function SelectDirectory(const Caption: string; const Root:WideString; out Directory: string): Boolean; overload; 第⼀种语法的Options参数指定了“浏览⽂件夹”对话框的⼀些选项;参数HelpCtx指定上下⽂敏感的Help ID;Directory初始化了对话框的值,并且携带返回值。
Delphi小技巧杂记-winstonbonaparte的专栏-CSDN博客Delphi小技巧杂记收藏//获取任务栏尺寸procedure TForm1.Button1Click(Sender: TObject);varTaskList: HWnd;Bordered: TRect;beginTaskList := FindWindow(’Shell_TrayWnd’, nil);GetWindowRect(TaskList, Bordered);Label1.Caption := ’Left: ’ + IntToStr(Bordered.Left);Label2.Caption := ’Right: ’ + IntToStr(Bordered.Right);Label3.Caption := ’Top: ’ + IntT oStr(Bordered.Top);Label4.Caption := ’Bottom: ’ + IntT oStr(Bordered.Bottom);end;//修改指定路径下的文件只读属性function PathSetAttr(sFilePath: string): Boolean;varSearchRec: TSearchRec;beginResult := False;if Copy(sFilePath, Length(sFilePath) - 1, Length(sFilePath)) <> ’\’ thensFilePath := sFilePath + ’\’;if DirectoryExists(sFilePath) thenbeginif FindFirst(sFilePath+’*.*’, faAnyFile, SearchRec) = 0 thenbeginFileSetAttr(, 32);while FindNext(SearchRec) = 0 doFileSetAttr(, 32);end;Result := True;end;end;//为控件加边框procedure WMNCPaint(var Msg: TWMNCPaint); message WM_NCPAINT;procedure TForm1.WMNCPaint(var Msg: TWMNCPaint);vardc: hDC;Pen: hPen;OldPen: hPen;OldBrush: hBrush;begininherited;dc := GetWindowDC(Handle);msg.Result := 1;Pen := CreatePen(PS_SOLID, 1, RGB(255, 0, 0));OldPen := SelectObject(dc, Pen);OldBrush := SelectObject(dc, GetStockObject(NULL_BRUSH));Rectangle(dc, 0, 0, Self.Width, Self.Height);SelectObject(dc, OldBrush);SelectObject(dc, OldPen);DeleteObject(Pen);ReleaseDC(Handle, Canvas.Handle);end;//制作透明窗体procedure CreateParams(var Params: TCreateParams); override; //重载 CreateParams 过程的实现procedure TForm1.CreateParams(var Params: TCreateParams);begin//先继承原来的 CreateParams 过程inherited;//然后修改 Param.ExStyle 的值,使窗体具有透明效果Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;end;procedure TForm1.FormCreate(Sender: TObject);begininherited;//将画布的笔刷样式改成bsClear 以及时清除窗体原来的内容,这样窗体就有透明效果Canvas.Brush.Style := bsClear;end;//Memo 组件的光标定位procedure TForm1.Memo1MouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);varLpos, Cpos, Linelength: integer;beginLpos := SendMessage(Memo1.Handle, EM_LineFromChar, Memo1.SelStart, 0);Cpos := SendMessage(Memo1.Handle, EM_LineIndex, Lpos, 0);LineLength := SendMessage(Memo1.Handle, EM_LineLength, Cpos, 0);Cpos := Memo1.SelStart - Cpos;Label1.Caption := IntToStr(Lpos);Label2.Caption := IntToStr(Cpos);Label3.Caption := IntToStr(LineLength);end;//点击客户区也能拖动窗口procedure WmNCHitTest(var Msg: TWMNCHitTest); message WM_NCHitTest;procedure TForm1.WmNCHitTest(var Msg: TWMNCHitT est);beginDefaultHandler(Msg);if Msg.Result = HTClient thenMsg.Result := HTCaption;end;//自定义Memo控件的边界procedure TForm1.Button1Click(Sender: TObject);varRect: TRect;beginSendMessage(Memo1.Handle, EM_GETRECT, 0, LongInt(@Rect));Rect.Left := 20;Rect.Top := 20;Rect.Right := Rect.Right - 19;Rect.Bottom := Rect.Bottom - 19;SendMessage(Memo1.Handle, EM_SETRECT, 0, LongInt(@Rect));Memo1.Refresh;end;//在系统菜单上添加自定义菜单项procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;constidMyFunc = $f200;procedure TForm1.FormCreate(Sender: TObject);beginAppendMenu(GetSystemMenu(Handle, False), MF_STRING, idMyFunc, ’我的系统菜单项’);end;procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);begininherited;if Msg.CmdType and $FFF0 = idMyFunc thenShowMessage(’我的系统菜单项’);end;COOLBAR内嵌TOOLBAR,但是TOOLBAR的按钮偏上的解决办法:把COOLBAR的COOLBAND的BORDERSTYLE设为SINGLE。
delphi tchecklistbox使用方法【原创版】目录1.Delphi TCheckListBox 简介2.Delphi TCheckListBox 基本属性3.Delphi TCheckListBox 常用方法4.Delphi TCheckListBox 事件处理5.Delphi TCheckListBox 示例代码正文【Delphi TCheckListBox 简介】Delphi TCheckListBox 是 Borland 公司开发的 Delphi 编程环境下,用于创建检查列表框(Check List Box)的控件。
检查列表框允许用户从一系列选项中选择一个或多个项目,这使得它在需要用户选择多个选项的程序中非常有用。
【Delphi TCheckListBox 基本属性】Delphi TCheckListBox 具有以下基本属性::检查列表框的名称。
2.Caption:检查列表框的标题。
3.Items:检查列表框中的项目。
4.Selected:当前选中的项目。
5.CheckMark:项目的复选标记。
6.DragMode:拖动模式,用于设置是否允许拖动检查列表框。
7.SortOrder:项目的排序顺序。
8.AutoCheck:自动检查功能,用于设置是否自动选中项目。
9.ShowHint:提示显示,用于设置是否显示提示文本。
【Delphi TCheckListBox 常用方法】Delphi TCheckListBox 提供了以下常用方法:1.AddItem:向检查列表框中添加项目。
2.InsertItem:在检查列表框中插入项目。
3.DeleteItem:从检查列表框中删除项目。
4.EditItem:编辑检查列表框中的项目。
5.GetItemCount:获取检查列表框中的项目数量。
6.GetSelectedItems:获取当前选中的项目列表。
7.SetSelectedItems:设置当前选中的项目列表。
1.2.4.3 汉化界面及字体选取如果您的Windows95系统中安装了中文系统,对界面做汉化是极其方便的。
例如,您可以将Button1的Caption属性改成“&R矩形”,同样地可改变其它部件的属性,将窗体做成中文的操作界面。
若对中、西文字体不满意,则可以调整Font属性以满足您的要求。
Font属性的前面,有一个小小的“+”号,这说明它表征的是集属性,也即属性不再是一个单值,而是一个属性的集合。
双击Font,Object Inspector将在下面扩充显示它的其它属性。
Color用来表示文本的颜色,Name定义了字体名,如System,MsSerif,Arial,宋体,黑体等。
Style下又拥有四个属性:fsBold,fsItalic,fsUnderline和fsTrikeOut。
如果想让字体有其中的某种风格,可把相应的属性值设成True。
Font对话框可以更为直接地设定以上属性。
Font属性右侧值段有带省略号的按钮,单击它可以激活Font对话框,如图1.7所示:这一对话框中包含了上述的全部属性,使得调整字形更为直接方便。
但要申明的是,对于窗体Form来讲,Font属性改变的是窗体中其它部件的显示字形,它本身的标题字体是缺省的System字体,不能通过属性来改变。
1.2.4.4使用图形编辑对话框最后,我们用图标按钮BitBtn1来控制Shape部件的颜色。
选中图标按钮,将它的Cap tion属性改为“&Color”(或“&C颜色”)。
可以在按钮中加入一个图标来形象地表述它的功能。
选用Object Inspector中的Glyph属性,点动值段的三点按钮,弹出图形编辑对话框。
您可以通过图形编辑对话框装入图标,在本例程中,按动Load按钮,选择\Delphi 2. 0\Images\Buttons\Brush.bmp文件装入,Bitbtn1就成为一个图标按钮。
另外,Delphi还为您提供了丰富的Windows标准图标按钮。
《Delphi程序设计》复习题四、程序填空题1、应用程序的用户界面如下图所示:功能说明:该界面是一个教师信息录入界面。
用户在编辑框(Edit1)中输入姓名,利用单选按钮(RadioGroup1)指定性别,利用组合框(ComboBox1)选择或直接输入所在系,利用列表框(ListBox1)指定职称。
当输入完上述信息后,单击“确定”按钮,教师信息将显示在右边的Memo1编辑窗口中。
下面是开发过程,请完成填空部分:通过RadioGroup1的Items属性设置两个单选按钮(“男”和“女”),并将属性①设置为2(按钮摆两列)。
通过ComboBox1的②属性设置下拉选单中的系名。
通过ListBox1的Items属性设置列表框中的职称列表。
“确定”按钮的单击事件处理程序如下:procedure TForm1.Button1Click(Sender: TObject);beginmemo1.Lines.Add(edit1. ③); //加入编辑框的内容if radiogroup1. ④= 0 then //判断选择了哪个按钮memo1.Lines.Add('男')elsememo1.Lines.Add('女') ;memo1.Lines.Add(combobox1.Text); //加入系的文本memo1.Lines.Add( listbox1. ⑤[listbox1.itemindex] ); //加入职称end;2、数据库mydb中有关系表student,其结构如下:student (sno 学号,sname 姓名,sex 性别,birthday 生日)现编写一个学生信息浏览程序,设计界面和运行界面如下图所示:在下划线处填写相应数据库组件的属性值:Database1. DatabaseName : mydbTable1. DatabaseName : mydbTable1. TableName : studentTable1. Active : ①Datasource1.DataSet : ②DBNavigator1. DataSource : ③DBEdit2. DataSource : ④DBEdit2. DataField : ⑤ 4、本程序根据用户选择的运算符对输入的二个操作数进行运算,并显示运算结果。
delphi listbox 使用
listbox删除
删除选中的某个:procedure TForm1.Button1Click(Sender: TObject); var
i:integer;begin i:=0; while i<listbox1.Count do//删除掉前一个后,下一个会上移,所以用while if listbox1.selected[i] then listbox1.Items.delete(i) else i:=i+1;end; 删除listbox中所有的内容:procedure TForm1.Button2Click(Sender: TObject);var i:integer; begin for i:=0 to listbox1.Items.Count-1 do listbox1.Items.Clear;
// ShowMessage(listbox1.Items[listbox1.ItemIndex]); 显示当前数据
按钮事件向上移动
procedure TFormMain.RzBmpButton5Click(Sender: TObject);
var
ActiveItem: Integer;
begin
with lst2 do
begin
ActiveItem := ItemIndex;
if (ItemIndex > 0) then
begin
Items.Move(ItemIndex, ItemIndex - 1);
lst2.SetFocus;
lst2.Selected[ActiveItem - 1] := True;
end;
end;
end;
向下移动
procedure TFormMain.RzBmpButton6Click(Sender: TObject);
var
ActiveItem: Integer;
begin
with lst2 do
begin
ActiveItem := ItemIndex;
if (ItemIndex >= 0) and (ItemIndex < Items.Count - 1) then
begin
Items.Move(ItemIndex, ItemIndex + 1);
lst2.SetFocus;
lst2.Selected[ActiveItem + 1] := True;
end;
end;
end;
拖拽事件
procedure TFormMain.lst2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
StartingPoint.X := X;
StartingPoint.Y := Y;
end;
procedure TFormMain.lst2DragDrop(Sender, Source: TObject; X, Y: Integer); var
DropPosition, StartPosition: Integer;
DropPoint: TPoint;
begin
DropPoint.X := X;
DropPoint.Y := Y;
with Source as TRzListBox do
begin
StartPosition := ItemAtPos(StartingPoint, True);
DropPosition := ItemAtPos(DropPoint, True);
Items.Move(StartPosition, DropPosition);
end;
end;
procedure TFormMain.lst2DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
Accept := Source = lst2;
end;。