ListView在delphi中的常用用法
<div>ListView在delphi中的常用用法</div><div>//增加 </div>
<div>
<div class="cnblogs_code">
<pre>i :=<span style="color: rgba(0, 0, 0, 1)"> ListView1.Items.Count;
</span><span style="color: rgba(0, 0, 255, 1)">with</span> ListView1 <span style="color: rgba(0, 0, 255, 1)">do</span>
<span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
ListItem:</span>=<span style="color: rgba(0, 0, 0, 1)">Items.Add;
ListItem.Caption:</span>=<span style="color: rgba(0, 0, 0, 1)">IntToStr(i);
ListItem.SubItems.Add(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">第</span><span style="color: rgba(128, 0, 0, 1)">'</span>+IntToStr(i)+<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"> 行</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
ListItem.SubItems.Add(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">第三列内容</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">end</span>;</pre>
</div>
<p>//按标题删除 </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">for</span> i:=ListView1.Items.Count-<span style="color: rgba(128, 0, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">downto</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> Do
</span><span style="color: rgba(0, 0, 255, 1)">if</span> ListView1.Items.Caption = Edit1.Text <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 0, 255, 1)">begin</span><span style="color: rgba(0, 0, 0, 1)">
ListView1.Items.Item.Delete(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除当前选中行 </span>
<span style="color: rgba(0, 0, 255, 1)">end</span>; </pre>
</div>
<p>//选中一行 </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">if</span> ListView1.Selected <> <span style="color: rgba(0, 0, 255, 1)">nil</span> <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
Edit1.Text :</span>=<span style="color: rgba(0, 0, 0, 1)"> ListView1.Selected.Caption;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> listview1.Items.Selected := True;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> listview1.Items.MakeVisible(True); </span></pre>
</div>
<p>//选择第一条 <br>procedure TForm1.Button2Click(Sender: TObject); <br>begin <br> listview1.SetFocus; <br> listview1.Items.Selected := True; <br>end; <br><br>//选择最后一条 <br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br> listview1.SetFocus; <br> listview1.Items.Selected := True; <br>end; <br><br>//此为调用过程,可以任意指定要移动的Item,下面是当前(Selected)Item <br>procedure ListViewItemMoveUpDown(lv : TListView; Item : TListItem; MoveUp, SetFocus : Boolean); <br>var <br>DestItem : TListItem; <br>begin <br> if (Item = nil) or <br> ((Item.Index - 1 < 0) and MoveUp) or <br> ((Item.Index + 1 >= lv.Items.Count) and (not MoveUp)) <br> then Exit; <br> lv.Items.BeginUpdate; <br> try <br> if MoveUp then <br> DestItem := lv.Items.Insert(Item.Index - 1) <br> else <br> DestItem := lv.Items.Insert(Item.Index + 2); <br> DestItem.Assign(Item); <br> lv.Selected := DestItem; <br> Item.Free; <br> finally <br> lv.Items.EndUpdate; <br> end; <br> if SetFocus then lv.SetFocus; <br> DestItem.MakeVisible(False); <br>end; <br><br><br>ListViewItemMoveUpDown(ListView1, ListView1.Selected, True, True);//上移 <br>ListViewItemMoveUpDown(ListView1, ListView1.Selected, False, True);//下移 <br><br><br>TListView组件使用方法 <br><br>引用CommCtrl单元 <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>ListView_DeleteColumn(MyListView.Handle, i);//i是要删除的列的序号,从0开始 <br>end; <br><br>用LISTVIEW显示表中的信息: <br>procedure viewchange(listv:tlistview;table:tcustomadodataset;var i:integer); <br>begin <br>tlistview(listv).Items.BeginUpdate; {listv:listview名} <br>try <br>tlistview(listv).Items.Clear; <br>with table do {table or query名} <br>begin <br>active:=true; <br>first; <br>while not eof do <br>begin <br>listitem:=tlistview(listv).Items.add; <br>listitem.Caption:=trim(table.fields.asstring); <br>// listitem.ImageIndex:=8; <br>next; <br>end; <br>end; <br>finally <br>tlistview(listv).Items.EndUpdate; <br>end; <br>end; <br><br><br>ListView使用中的一些要点。以下以一个两列的ListView为例。 <br>→增加一行: <br>with ListView1 do <br>begin <br>ListItem:=Items.Add; <br>ListItem.Caption:='第一列内容'; <br>ListItem.SubItems.Add('第二列内容'); <br>end; <br>→清空ListView1: <br>ListView1.Items.Clear; <br>→得到当前被选中行的行的行号以及删除当前行: <br>For i:=0 to ListView1.Items.Count-1 Do <br>If ListView1.Items.Selected then //i=ListView1.Selected.index <br>begin <br>ListView1.Items.Delete(i); //删除当前选中行 <br>end; <br>当然,ListView有OnSelectItem事件,可以判断选择了哪行,用个全局变量把它赋值出来。 <br>→读某行某列的操作: <br>Edit1.Text := listview1.Items.Caption; //读第i行第1列 <br>Edit2.Text := listview1.Items.SubItems.strings; //读第i行第2列 <br>Edit3.Text := listview1.Items.SubItems.strings; //读第i行第3列 <br>以次类推,可以用循环读出整列。 <br>→将焦点上移一行: <br>For i:=0 to ListView1.Items.Count-1 Do <br>If (ListView1.Items.Selected) and (i>0) then <br>begin <br>ListView1.SetFocus; <br>ListView1.Items.Item.Selected := True; <br>end; </p>
<div>
<div>引用内容</div>
<div>
<p>不过在Delphi7中,ListView多了一个ItemIndex属性,所以只要<br>ListView1.SetFocus;<br>ListView1.ItemIndex:=3;<br>就能设定焦点了。</p>
<p> delphi中ListView的某项某列的值必须先定义然后才能使用。比如你用ListView控件建立了一个表,严格说来这个表就好比一个二维数组。如果我们新建一行,只是第一行Caption有值,其它列为空,那也必须对后面的列执行Add(''),这相当于把空串赋给了后面的列,这样以后才能像操作二维数组一样直接访问任意行的任意列,否则的话访问caption后面的列时会报错。这相当于对于ListView中的每一个值(某项某列的值)都必须初始化。</p>
</div>
</div>
</div>
</div>
<div id="MySignature" role="contentinfo">
好的代码像粥一样,都是用时间熬出来的<br><br>
来源:https://www.cnblogs.com/jijm123/p/14124261.html
頁:
[1]