Advertisement
7_2009-2012 Data Structures #224383

_ Beginners Listview LVW tutorial: Various difernt views, add items WITH ICONS, spreadsheet style! _

BEGINNER TREEVIEW TUTORIAL how to use the treeview and add items with icons to the treeview. + Spreadsheet view etc. First release, update with properties, events etc coming soon. Source code examples included

AI

Resumo por IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Código fonte
original-source
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="description" content="ListView tutorial - Learn to use the listview correctly! Different features explained.">
<link rel="File-List" href="lv_files/filelist.xml">
<LINK REL=StyleSheet HREF="http://thecleaner.publication.org.uk/tips.css" TYPE="text/css">
<title>ListView Tutorial</title>
<style>
v\:*  { behavior: url(#default#VML) }
o\:*  { behavior: url(#default#VML) }
.shape { behavior: url(#default#VML) }
</style>
<xml><o:shapedefaults v:ext="edit" spidmax="1027"/>
</xml>
</head>
<body leftmargin="20" topmargin="20" style="text-align: left; line-height: 150%">
<h1><v:shapetype id="_x0000_t136"
 coordsize="21600,21600" o:spt="136" adj="10800" path="m@7,l@8,m@5,21600l@6,21600e">
 <v:formulas>
 <v:f eqn="sum #0 0 10800"/>
 <v:f eqn="prod #0 2 1"/>
 <v:f eqn="sum 21600 0 @1"/>
 <v:f eqn="sum 0 0 @2"/>
 <v:f eqn="sum 21600 0 @3"/>
 <v:f eqn="if @0 @3 0"/>
 <v:f eqn="if @0 21600 @1"/>
 <v:f eqn="if @0 0 @2"/>
 <v:f eqn="if @0 @4 21600"/>
 <v:f eqn="mid @5 @6"/>
 <v:f eqn="mid @8 @5"/>
 <v:f eqn="mid @7 @8"/>
 <v:f eqn="mid @6 @7"/>
 <v:f eqn="sum @6 0 @5"/>
 </v:formulas>
 <v:path textpathok="t" o:connecttype="custom" o:connectlocs="@9,0;@10,10800;@11,21600;@12,10800"
 o:connectangles="270,180,90,0"/>
 <v:textpath on="t" fitshape="t"/>
 <v:handles>
 <v:h position="#0,bottomRight" xrange="6629,14971"/>
 </v:handles>
 <o:lock v:ext="edit" text="t" shapetype="t"/>
</v:shapetype><v:shape id="_x0000_s1025" type="#_x0000_t136" style='width:531pt;
 height:41.25pt' fillcolor="#369" stroked="f">
 <v:shadow on="t" color="#b2b2b2" opacity="52429f" offset="3pt"/>
 <v:textpath style='font-family:"Times New Roman";v-text-kern:t' trim="t"
 fitpath="t" string="Using the VB.NET ListView Control"/>
</v:shape><![if !vml]><img border=0 width=712 height=58
src="lv_files/image001.gif" alt="Using the VB.NET ListView Control" v:shapes="_x0000_s1025"><![endif]></h1>
<p>Stage 1 - Adding items to the ListView, and the  various different views 
available.</p>
<p><font face="Verdana"><b>The ListView control can be used to display a list of 
icons in your program, similar to the explorer file view, or a list of data in 
the form of a table, with icons at the side.</b></font></p>
<p><font face="Verdana">Abbreviation: When naming controls, you name them 
xxxYyyy, where x is the abbreviated control type, and y is a one word 
description of what the control is for.  For the ListView, 'lvw' is the 
abbreviation. Therefore, in the examples below I will be referring to the 
ListView as lvwTest. (Naming controls is a good practice to get into, since when 
looking through your code the lvwTest_Click event is far easier to find than 
Listview23_Click between all the other similar named controls!)</font></p>
<p><i><b><font face="Verdana" color="#0000FF">The code for each example can be downloaded near the 
bottom of the page, at the normal Planet-Source-Code download-code link</font></b></i></p>
<hr color="#008080">
<p> </p>
<h3>Example 1: Filling the ListView control with 3 different items, in view1: 
LargeIcon</h3>
<ul>
 <li><font face="Verdana">Add a ListView control to the form you are working on</font></li>
 <li><font face="Verdana">Call it lvwTest</font></li>
</ul>
<p style="text-align: center"><font face="Verdana">Picture of finished item:</font></p>
<p style="text-align: center">
<img border="0" src="http://thecleaner.publication.org.uk/tutorials/listview/pic1.jpg" width="432" height="312"></p>
<h4><font face="Verdana"><b>This was achieved with the following code in the 
Form_Load event.</b></font></h4>
<font COLOR="#0000ff">
<pre>Private </font><font COLOR="#0000ff">Sub</font> frmLVWTest_Load(<font COLOR="#0000ff">ByVal</font> sender <font COLOR="#0000ff">As</font> System.Object, <font COLOR="#0000ff">ByVal</font> e <font COLOR="#0000ff">As</font> System.EventArgs) <font COLOR="#0000ff">Handles </font><font COLOR="#0000ff">MyBase</font>.Load
     lvwTest.View = View.LargeIcon        <font color="#008080"> </font><font color="#009933">'Set the display type of the ListView.  In this example, we want the LargeIcon display (1)</font>
     lvwTest.Items.Add("Item 1")           <font color="#009933">'Add each item to the ListView. Look at the picture above to see how each item turns out</font>
     lvwTest.Items.Add("Item 22")
     lvwTest.Items.Add("Item 333")
<font COLOR="#0000ff">End</font> <font COLOR="#0000ff">Sub</font>
</pre>
<p><font face="Verdana">You can see here that I'm adding 3 items to the 
ListView, with the captions 'Item 1', 'Item 22' and 'Item 333'.  These are 
the values that are displayed and can be clicked on when the program is 
executed. You could also try adding more items to the ListView, by copying the 
line, and changing the item caption.</font></p>
<p><font face="Verdana">Certainly not very impressive, but this was just with 4 
lines of code!</font></p>
<hr color="#008080">
<h3> </h3>
<h3>Example 2: Adding 32x32 icons to the ListView in view1</h3>
<ul>
 <li><font face="Verdana">Add a ListView control to the form you are working on</font></li>
 <li><font face="Verdana">Call it lvwTest</font></li>
 <li><font face="Verdana">Add an ImageList to the form</font></li>
 <li><font face="Verdana">Call it 'imgIcons'</font></li>
 <li><font face="Verdana">Set the 'Width' and 'Height' properties to 32</font></li>
 <li><font face="Verdana">Add any 3 icons to the ImageList by clicking on it 
 and hitting the '<b>...</b>' button on the 'Images' property of the control. 
 Now press the 'Add' button to find an icon</font></li>
</ul>
<p style="text-align: center"><font face="Verdana">Picture of finished item:</font></p>
<p style="text-align: center">
<img border="0" src="http://thecleaner.publication.org.uk/tutorials/listview/pic2.jpg" width="432" height="312"></p>
<p><font face="Verdana">We can spruce the previous ListView example up by 
attaching icons to the items in the ListView, by changing the code in the 
Form_Load event</font></p>
<pre>Private </font><font COLOR="#0000ff">Sub</font> frmLVWTest_Load(<font COLOR="#0000ff">ByVal</font> sender <font COLOR="#0000ff">As</font> System.Object, <font COLOR="#0000ff">ByVal</font> e <font COLOR="#0000ff">As</font> System.EventArgs) <font COLOR="#0000ff">Handles </font><font COLOR="#0000ff">MyBase</font>.Load
     lvwTest.View = View.LargeIcon        <font color="#008080"> </font><font color="#009933">'Set the display type of the ListView.  In this example, we want the LargeIcon display (1)</font>
     lvwTest.LargeImageList = imgIcons     <font color="#009933">'Tell the ListView which ImageList to obtain its icons from</font>
 
     lvwTest.Items.Add("Item 1", 0)         <font color="#009933">'Add each item to the ListView. The number determines the icon to use by its index.</font>
     lvwTest.Items.Add("Item 22", 1)
     lvwTest.Items.Add("Item 333", 2)
<font COLOR="#0000ff">End</font> <font COLOR="#0000ff">Sub</font>
</pre>
<p><font face="Verdana">Here, we are instructing each ListItem to pull an icon 
from the ImageList by its index.  We have 3 icons in the ImageList, so they 
are numbered 0, 1 and 2.  Note that I've added the extra line 2nd from the 
top to tell the ListView which ImageList to look in for its icons.</font></p>
<p><font face="Verdana">Another thing to try is using 16x16 icons.  For 
this, make sure you set the ImageList, imgIcons 'Height' and 'Width' properties 
to 16. You will have to delete and re-add the icons to the ImageList when 
changing the size. Now you have to tell the ListView where to get the small 
icons, and to switch to small icon view.</font></p>
<p><font face="Verdana">Simply use this code in place of the first two lines of 
the previous code:</font></p>
<pre>    lvwTest.View = View.List		<font color="#009933">'Sets the display type to List them in a straight line downwards</font>
    lvwTest.SmallImageList = imgIcons
</pre>
<p style="line-height: 100%"><font face="Verdana"><b>OR</b></font></p>
<font SIZE="2">
<pre>  </font>  lvwTest.View = View.SmallIcons	<font color="#009933">'Tells the ListView to list items across itself</font>
    lvwTest.SmallImageList = imgIcons
</pre>
<p style="line-height: 100%; text-align: center"><font face="Verdana">When you 
now run the program, you should see the following:</font></p>
<p style="line-height: 100%; text-align: center">
<img border="0" src="http://thecleaner.publication.org.uk/tutorials/listview/pic4.jpg" width="432" height="312">  
<font face="Verdana"><b>OR</b></font>  
<img border="0" src="http://thecleaner.publication.org.uk/tutorials/listview/pic3.jpg" width="432" height="312"></p>
<hr color="#008080">
<h3> </h3>
<h3>Example 3: The ListView in style2: Details</h3>
<ul>
 <li><font face="Verdana">Add a ListView control to the form you are working on</font></li>
 <li><font face="Verdana">Call it lvwTest</font></li>
 <li><font face="Verdana">Add an ImageList to the form</font></li>
 <li><font face="Verdana">Call it 'imgIcons'</font></li>
 <li><font face="Verdana">Set the 'Width' and 'Height' properties to 16</font></li>
 <li><font face="Verdana">Add any 3 icons to the ImageList by clicking on it 
 and hitting the '<b>...</b>' button on the 'Images' property of the control. 
 Now press the 'Add' button to find an icon</font></li>
</ul>
<p><font face="Verdana">In my opinion, this is the most useful mode of the 
ListView.  It is similar to a spreadsheet layout with its multiple columns 
and lines</font></p>
<p style="text-align: center"><font face="Verdana">Picture of finished result:</font></p>
<p style="text-align: center">
<img border="0" src="http://thecleaner.publication.org.uk/tutorials/listview/pic5.jpg" width="432" height="304"></p>
<p> </p>
<p><font face="Verdana">The code here is slightly more complex, mainly because 
there is more of it.  If you examine it closely, it is the same thing 
repeated 3 times, for each of the columns or items.</font></p>
<pre><font COLOR="#0000ff">Private</font> <font COLOR="#0000ff">Sub</font> frmLVWTest_Load(<font COLOR="#0000ff">ByVal</font> sender <fontCOLOR="#0000ff">As</font> System.Object, <font COLOR="#0000ff">ByVal</font> e <font COLOR="#0000ff">As</font> System.EventArgs) <font COLOR="#0000ff">Handles</font> <font COLOR="#0000ff">MyBase</font>.Load
 lvwTest.View = View.Details		<font color="#009933">'Sets the ListView to display items with columns, as seen in the picture above</font>
 lvwTest.SmallImageList = imgIcons	<font color="#009933">'Sets the ImageList that will be used to get the small icons on the first column</font>
  
<font color="#009933"> 'The following three lines of code create the columns in the picture (A, B and C). </font>
<font color="#009933"> 'You can also change the width of the columns by changing the '100' to a different value. </font>
<font color="#009933"> 'The alignment can also be changed, to be right or centre aligned.</font>
 lvwTest.Columns.Add("Column A", 100, HorizontalAlignment.Left)
 lvwTest.Columns.Add("Column B", 100, HorizontalAlignment.Left)
 lvwTest.Columns.Add("Column C", 100, HorizontalAlignment.Left)
  
 <font COLOR="#0000ff">Dim</font> lSingleItem <font COLOR="#0000ff">As</font> ListViewItem 	<font color="#009933">'The variable will hold the ListItem information so that you can add values to the column you want to change</font>
  
 lSingleItem = lvwTest.Items.Add("Item 1", 0)		<font color="#009933">'Create a new line, and assign the ListItem into the variable so we can add sub items</font>
 lSingleItem.SubItems.Add("Subitem XXY")		<font color="#009933">'The first sub item for the first line</font>
 lSingleItem.SubItems.Add("Subitem ZZY")		<font color="#009933">'The second sub item for the first line</font>
  
 lSingleItem = lvwTest.Items.Add("Item 22", 1)		<font color="#009933">'Create another line. Same as previous new line code, but with different icon number and caption</font>
 lSingleItem.SubItems.Add("Subitem CCA")		<font color="#009933">'The first sub item for the second line</font>
 lSingleItem.SubItems.Add("Subitem DDA")
  
 lSingleItem = lvwTest.Items.Add("Item 333", 2)
 lSingleItem.SubItems.Add("Subitem TTG")
 lSingleItem.SubItems.Add("Subitem HHG")		<font color="#009933">'The second sub item for the third line</font>
<font COLOR="#0000ff">End Sub</font>
</pre>
<hr color="#008080">
<p><b><font face="Tahoma" size="4">Thanks for reading... hope it helped! :-D</font></b></p>
<p><font face="Tahoma" size="4">I realize this doesn't cover the events of the 
ListView, such as Click, DoubleClick, MouseOver etc or the properties of an item 
(lvwTest.SelectedItem.Caption) etc.  These will be addressed when the 
tutorial is updated!</font></p>
<p><b><font face="Tahoma" size="4">Don't forget to check out my
<a href="http://pscode.com/vb/scripts/ShowCode.asp?txtCodeId=169&lngWId=10">
string manipulation tutorial</a> and VOTE IF YOU THINK I DID A GOOD JOB!</font></b></p>
<p><b><font face="Tahoma" size="4">TreeView Tutorial coming as soon as I can be 
bothered!</font></b></p>
<marquee><a href="mailto:jBistoGOOD@Hotmail.com?subject=ListView Tutorial"><img src="http://www.exhedra.com/upload/AuthorPhotos/AUTHOR_PHOTO200252375693754.gif"></a></marquee>
<p>(thanks to <a href="http://www.avenuezx.com/vbnet4apps/">VBnet4Apps</a> for 
the CSS)</p>
</body>
</html>
Uploading files in PHP
Think of an application where you want your users to upload files to your machine. Uploading files in PHP is very easy. You can upload any type of file from client to server. Files are uploaded from the browser using an <input> tag, set the type parameter in the <input> tag as "File". This is supported by all popular browsers currently available on the market. Important thing is to set the ENCTYPE attribute of the form to "multipart/form-data" and set the form's action element to the file upload page. The file upload page will handle the file uploading. Here is the code:
Create a simple html form for submitting the file:
<html>
<form action="myupload.php" method=post enctype="multipart/form-data">
submit this file: <input type=file name="userfile"><br>
<input type=submit><br>
</form>
</html>
Now, create a php file named as "myupload.php":
<html>
<?
if(!empty($userfile)) {
 
//copy the file to some permanent location
copy($userfile, "/php/www/files/myfile.txt");
//destroy the file
unlink($userfile);
//display message
echo("file uploaded");
}
?>
</html>
Details:
Handling uploaded file is very easy in PHP. First, the file is saved in the temporary directory of your operating system (if you have not set the path to your own temporary directory in the php.ini file). You can copy the uploaded file from the temporary directory to the location of your choice. The file in the temporary directory is automatically destroyed at the end of the request. File name is accessible in the same way as all form data. In our code, we have set the name for the uploaded file as "userfile". You can access this variable in the upload file page as $userfile. We copy the file to a permanent location using the copy() function. Although, the uploaded file is automatically destroyed at the end of the request but its a good practice to destroy the file explicitly. You can use the unlink() function to destroy the file.
You can check if the file has been uploaded to the server using different attributes provided by the PHP. For example, if you dont want to allow your users to upload the same file again and again, you can put a restriction on the user to upload new files only. We can set limit on the size of the file to be uploaded by adding an <input> element with the NAME attribute set to MAX_FILE_SIZE and the VALUE to the upper limit. I havent demonstrated this in my sample code but here the piece of code for your reference:
<input type=hidden name=MAX_FILE_SIZE value=1024>
<imput type=file name=userfile>
Remember, the hidden <input> tag should precede the file <input> tag.
Place a check before displaying a message, use the empty() function to check if $userfile variable contains something or not, if its empty then display the message telling the user that the file hasn't been uploaded otherwise display the message that the file has been uploaded successfully.
Comentários originais (3)
Recuperado do Wayback Machine