Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[74] ListView Control

Last post 11-14-2007 10:00 PM by SeanH. 49 replies.
Page 4 of 4 (50 items) < Previous 1 2 3 4
Sort Posts: Previous Next
  • 07-05-2007 7:14 PM In reply to

    • Maxjonz
    • Not Ranked
    • Joined on 07-05-2007
    • United Kingdom
    • New Member
    • Points 10

    Re: [74] ListView Control

    I found that the following code didn't work for me

    ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item

    I had to use

    ListView1.ListItems.Remove(ListView1.SelectedItem.Index) 

    Also  - it wasn't obvious to me to start with  ( I'm a newbie to listviews )

    but to  'get at' the data in the views one had to use

    ListView1.ListItems.Item(x)    where  x is a valid number in the  range 1 to ListView1.ListItems.Count  (NOT   0 to .Count - 1 as in normal Visual Basic)

     Cheers

    Maxjonz

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 08-08-2007 10:20 AM In reply to

    • Halina
    • Not Ranked
    • Joined on 08-08-2007
    • United Kingdom
    • New Member
    • Points 10

    Re: [74] ListView Control

    Hi..

    I using ListView in C#.

    I tried to add text into certain colum in ListView.

    For example: I have 3 columns, Column1, Column2, Column3.

    I want to insert "TEST1"  and "TEST2" in Column2

    How can I going to do in C#?

    Thank you

    Halina

    • Post Points: 10
  • 08-10-2007 4:34 AM In reply to

    • anatha1
    • Top 500 Contributor
    • Joined on 08-06-2007
    • Cambodia
    • Addicted Member
    • Points 575

    Re: [74] ListView Control

    Hey everyone here are some example of Database that use ListView:

    if u need in C# pls copy this code convert to C# your self.

    1. Use Microsoft Access Database
    • Module1 code

    Imports

    System.Data.OleDb

    Imports

    System.IO

    Module

    Module1

    Dim cnn As New OleDb.OleDbConnection

    Public LocPos As Integer

    Public TotalPos As Integer

    Sub DBConnection(ByVal PTH As String)

    Try

    cnn =

    New OleDbConnection("Provider=microsoft.jet.oledb.4.0; data source='" & PTH & "'")

    cnn.Open()

    Catch ex As Exception

    MsgBox(ex.Message, MsgBoxStyle.Information,

    "")

    End

    End Try

    End Sub

    Public Enum CustomDel

    DellAll = 1

    DelCustom = 2

    End Enum

    Public Enum CtrlDorE

    CtrlDiabled = 1

    CtrlEnabled = 2

    End Enum

    Public Enum Optionshow

    SHowAllFields = 1

    SHowSomeField = 2

    End Enum

    Sub DeleteData(ByVal tbl As String, ByVal FieldCon As String, ByVal ValueCon As String, ByVal Deloption As CustomDel)

    Try

    Dim cm As New OleDb.OleDbCommand

    Dim del As Integer

    Dim SQLa As String : Dim SQLc As String

    SQLa =

    "delete * from " & tbl

    SQLc =

    "delete from " & tbl & " where " & FieldCon & "='" & ValueCon & "'"

    Select Case Deloption

    Case 1 : del = CustomDel.DellAll : cm.CommandText = SQLa

    Case 2 : del = CustomDel.DelCustom : cm.CommandText = SQLc

    End Select

    cm.Connection = cnn

    cm.ExecuteNonQuery()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    Sub TransferData2txt(ByVal frm As Form, ByVal tbl As String, ByVal Pos As Integer, ByVal ParamArray Txt() As String)

    Dim cm As New OleDb.OleDbCommand

    Dim ds As New DataSet

    Dim adp As New OleDbDataAdapter

    Dim ct As Control : Dim i As Integer

    On Error GoTo err

    cm.CommandText =

    "select * from " & tbl

    cm.Connection = cnn

    adp =

    New OleDbDataAdapter("select * from " & tbl, cnn)

    ds =

    New DataSet(tbl)

    adp.Fill(ds, tbl)

    TotalPos = ds.Tables(tbl).Rows.Count - 1

    adp.Dispose()

    Dim dr As OleDbDataReader = cm.ExecuteReader

    If ds Is Nothing Then Return

    With ds.Tables(tbl).Rows(Pos)

    For Each ct In frm.Controls

    If TypeOf ct Is TextBox Then

    For i = 0 To UBound(Txt)

    If LCase(Txt(i)) = LCase(ct.Name) Then

    ct.Text = .Item(i).ToString

    i = i + 1 :

    Exit For

    End If

    Next

    End If

    Next

    End With

    dr.Close()

    err:

    Exit Sub

    End Sub

    Function IDcreator(ByVal tbl As String, ByVal IDStyle As String, ByVal Connector As String, ByVal FormatNumber As String, ByVal Field As String) As String

    Dim cm As New OleDb.OleDbCommand()

    Dim da As New OleDb.OleDbDataAdapter()

    Dim ds As New DataSet()

    Dim Tem As String

    Try

    ds =

    New DataSet(tbl)

    da =

    New OleDb.OleDbDataAdapter("select * from " & tbl, cnn)

    da.Fill(ds, tbl)

    Dim RecordCount = ds.Tables(tbl).Rows.Count() 'count all records in one table

    Tem = IDStyle & Connector & Format(RecordCount + 1, FormatNumber)

    Dim i = 1

    Do

    i = i + 1

    cm.CommandText =

    "select * from " & tbl & " where " & Field & " = '" & Tem & "'"

    cm.Connection = cnn

    Dim rst As OleDb.OleDbDataReader = cm.ExecuteReader

    If rst.HasRows Then

    Tem = IDStyle & Connector & Format(RecordCount + i, FormatNumber)

    rst.Close()

    Else : IDcreator = Tem : Exit Function

    End If

    Loop

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Function

    Sub showDataTolst(ByVal tbl As String, ByVal lst As ListView, ByVal showOption As Optionshow, ByVal ParamArray SelectField() As String)

    Dim cm As New OleDb.OleDbCommand()

    Dim da As New OleDb.OleDbDataAdapter()

    Dim ds As New DataSet()

    Dim Opt As Integer, TemField As String

    Dim a As Integer, Sql As String, i%, ii%

    Dim ColH As ColumnHeader

    lst.View = View.Details

    lst.Clear()

    Select Case showOption

    Case 1 : Opt = Optionshow.SHowAllFields

    Sql =

    "select * from " & tbl

    Case 2 : Opt = Optionshow.SHowSomeField

    For a = 0 To UBound(SelectField)

    TemField = TemField & SelectField(a) &

    ","

    Next

    TemField = Strings.Left(TemField, Len(TemField) - 1)

    Sql =

    "select " & TemField & " from " & tbl

    End Select

    cm.CommandText = Sql

    cm.Connection = cnn

    da =

    New OleDb.OleDbDataAdapter(Sql, cnn)

    ds =

    New DataSet(tbl)

    da.Fill(ds, tbl)

    For i = 0 To ds.Tables(tbl).Columns.Count - 1

    Dim fieldName = ds.Tables(tbl).Columns(i).ColumnName ' find caption of field name

    ColH =

    New ColumnHeader()

    ColH.Text = fieldName

    lst.Columns.Add(ColH)

    Next

    For Each ColH In lst.Columns

    ColH.Width = 90

    Next

    Dim dr As OleDbDataReader = cm.ExecuteReader

    While dr.Read

    Dim lstitem As ListViewItem

    For i = 0 To dr.FieldCount - 1

    lstitem =

    New ListViewItem(dr.Item(i).ToString)

    For ii = 1 To dr.FieldCount - 1

    lstitem.SubItems.Add(dr.Item(ii).ToString)

    Next

    lst.Items.Add(lstitem)

    Exit For

    Next

    End While

    dr.Close()

    End Sub

    Sub AddNewRecord(ByVal tbl As String, ByVal ParamArray Data() As String)

    Dim cm As New OleDb.OleDbCommand()

    Dim da As New OleDb.OleDbDataAdapter()

    Dim ds As New DataSet()

    Dim Temp As String

    Dim FTemp As String

    Try

    ds =

    New DataSet(tbl)

    da =

    New OleDb.OleDbDataAdapter("select * from " & tbl, cnn)

    da.Fill(ds, tbl)

    Dim i As Integer

    For i = 0 To UBound(Data)

    Dim fieldName = ds.Tables(tbl).Columns(i).ColumnName ' find caption of field name

    Temp = Temp & fieldName &

    ","

    FTemp = FTemp &

    "'" & Data(i) & "'" & ","

    Next

    Temp = Strings.Left(Temp, Len(Temp) - 1)

    FTemp = Strings.Left(FTemp, Len(FTemp) - 1)

    Dim sql As String

    sql =

    "Insert into " & tbl & "(" & Temp & ")" & " Values(" & FTemp & ")"

    cm.CommandText = sql

    cm.Connection = cnn

    cm.ExecuteNonQuery()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    Sub UpDateDataToTable(ByVal tbl As String, ByVal FieldCon As String, ByVal ValueCon As String, ByVal ParamArray Data() As String)

    Try

    Dim Temp As String

    Dim cm As New OleDb.OleDbCommand

    Dim da As New OleDb.OleDbDataAdapter()

    Dim ds As New DataSet()

    ds =

    New DataSet(tbl)

    da =

    New OleDb.OleDbDataAdapter("select * from " & tbl, cnn)

    da.Fill(ds, tbl)

    Dim i As Integer

    For i = 0 To UBound(Data)

    Dim fieldName = ds.Tables(tbl).Columns(i).ColumnName ' find caption of field name

    Temp = Temp & fieldName &

    "='" & Data(i) & "',"

    Next

    Temp = Strings.Left(Temp, Len(Temp) - 1)

    cm.CommandText =

    "update " & tbl & " set " & Temp & " where " & FieldCon & "='" & ValueCon & "'"

    cm.Connection = cnn

    cm.ExecuteNonQuery()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    End

    Module
    1. Use SqlServer

    connect direct to SqlSever (MS SQL Server Studio 2005)

    Imports System.Data.SqlClient
    Dim Cnn As New SqlConnection
    Dim CnnStr$
    CnnStr = "Data Source=PC2\SQLEXPRESS;Initial Catalog=KhmerDictionary;Integrated Security=True;Pooling=False;"
    Cnn = New SqlConnection(CnnStr)
    Cnn.Open()

    Connect Indirect to Sql Server Database File

    Imports System.Data.SqlClient
    Dim Cnn As New SqlConnection
    Dim CnnStr$
    CnnStr="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\NimolProject\Dictionary Testing\Original DBDictionary\KhmerDictionary.mdf";Integrated Security=True;Connect Timeout=30;User Instance=True"
    Cnn = New SqlConnection(CnnStr)
    Cnn.Open()















    • Modul2 for Sqlserver connection

    Imports

    System.Data.SqlClient

    Module

    Module2

    Dim cnn As New SqlClient.SqlConnection

    Public LocPos As Integer

    Public TotalPos As Integer

    Public Enum CustomDel

    DellAll = 1

    DelCustom = 2

    End Enum

    Public Enum Optionshow

    SHowAllFields = 1

    SHowSomeField = 2

    End Enum

    Sub DeleteData(ByVal tbl As String, ByVal FieldCon As String, ByVal ValueCon As String, ByVal Deloption As CustomDel)

    Try

    Dim cm As New SqlClient.SqlCommand

    Dim del As Integer

    Dim SQLa As String : Dim SQLc As String

    SQLa =

    "delete * from " & tbl

    SQLc =

    "delete from " & tbl & " where " & FieldCon & "='" & ValueCon & "'"

    Select Case Deloption

    Case 1 : del = CustomDel.DellAll : cm.CommandText = SQLa

    Case 2 : del = CustomDel.DelCustom : cm.CommandText = SQLc

    End Select

    cm.Connection = cnn

    cm.ExecuteNonQuery()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    Sub TransferData2txt(ByVal frm As Form, ByVal tbl As String, ByVal Pos As Integer, ByVal ParamArray Txt() As String)

    Dim cm As New SqlClient.SqlCommand

    Dim ds As New DataSet

    Dim adp As New SqlClient.SqlDataAdapter

    Dim ct As Control : Dim i As Integer

    On Error GoTo err

    cm.CommandText =

    "select * from " & tbl

    cm.Connection = cnn

    adp =

    New SqlClient.SqlDataAdapter("select * from " & tbl, cnn)

    ds =

    New DataSet(tbl)

    adp.Fill(ds, tbl)

    TotalPos = ds.Tables(tbl).Rows.Count - 1

    adp.Dispose()

    Dim dr As SqlClient.SqlDataReader = cm.ExecuteReader

    If ds Is Nothing Then Return

    With ds.Tables(tbl).Rows(Pos)

    For Each ct In frm.Controls

    If TypeOf ct Is TextBox Then

    For i = 0 To UBound(Txt)

    If LCase(Txt(i)) = LCase(ct.Name) Then

    ct.Text = .Item(i).ToString

    i = i + 1 :

    Exit For

    End If

    Next

    End If

    Next

    End With

    dr.Close()

    err:

    Exit Sub

    End Sub

    Function IDcreator(ByVal tbl As String, ByVal IDStyle As String, ByVal Connector As String, ByVal FormatNumber As String, ByVal Field As String) As String

    Dim cm As New SqlClient.SqlCommand

    Dim da As New SqlClient.SqlDataAdapter

    Dim ds As New DataSet()

    Dim Tem As String

    Try

    ds =

    New DataSet(tbl)

    da =

    New SqlClient.SqlDataAdapter("select * from " & tbl, cnn)

    da.Fill(ds, tbl)

    Dim RecordCount = ds.Tables(tbl).Rows.Count() 'count all records in one table

    Tem = IDStyle & Connector & Format(RecordCount + 1, FormatNumber)

    Dim i = 1

    Do

    i = i + 1

    cm.CommandText =

    "select * from " & tbl & " where " & Field & " = '" & Tem & "'"

    cm.Connection = cnn

    Dim rst As SqlClient.SqlDataReader = cm.ExecuteReader

    If rst.HasRows Then

    Tem = IDStyle & Connector & Format(RecordCount + i, FormatNumber)

    rst.Close()

    Else : IDcreator = Tem : Exit Function

    End If

    Loop

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Function

    Sub showDataTolst(ByVal tbl As String, ByVal lst As ListView, ByVal showOption As Optionshow, ByVal ParamArray SelectField() As String)

    Dim cm As New SqlClient.SqlCommand

    Dim da As New SqlClient.SqlDataAdapter

    Dim ds As New DataSet()

    Dim Opt As Integer, TemField As String

    Dim a As Integer, Sql As String, i%, ii%

    Dim ColH As ColumnHeader

    lst.View = View.Details

    lst.Clear()

    Select Case showOption

    Case 1 : Opt = Optionshow.SHowAllFields

    Sql =

    "select * from " & tbl

    Case 2 : Opt = Optionshow.SHowSomeField

    For a = 0 To UBound(SelectField)

    TemField = TemField & SelectField(a) &

    ","

    Next

    TemField = Strings.Left(TemField, Len(TemField) - 1)

    Sql =

    "select " & TemField & " from " & tbl

    End Select

    cm.CommandText = Sql

    cm.Connection = cnn

    da =

    New SqlClient.SqlDataAdapter(Sql, cnn)

    ds =

    New DataSet(tbl)

    da.Fill(ds, tbl)

    For i = 0 To ds.Tables(tbl).Columns.Count - 1

    Dim fieldName = ds.Tables(tbl).Columns(i).ColumnName ' find caption of field name

    ColH =

    New ColumnHeader()

    ColH.Text = fieldName

    lst.Columns.Add(ColH)

    Next

    For Each ColH In lst.Columns

    ColH.Width = 90

    Next

    Dim dr As SqlClient.SqlDataReader = cm.ExecuteReader

    While dr.Read

    Dim lstitem As ListViewItem

    For i = 0 To dr.FieldCount - 1

    lstitem =

    New ListViewItem(dr.Item(i).ToString)

    For ii = 1 To dr.FieldCount - 1

    lstitem.SubItems.Add(dr.Item(ii).ToString)

    Next

    lst.Items.Add(lstitem)

    Exit For

    Next

    End While

    dr.Close()

    End Sub

    Sub AddNewRecord(ByVal tbl As String, ByVal ParamArray Data() As String)

    Dim cm As New SqlClient.SqlCommand

    Dim da As New SqlClient.SqlDataAdapter

    Dim ds As New DataSet()

    Dim Temp As String

    Dim FTemp As String

    Try

    ds =

    New DataSet(tbl)

    da =

    New SqlClient.SqlDataAdapter("select * from " & tbl, cnn)

    da.Fill(ds, tbl)

    Dim i As Integer

    For i = 0 To UBound(Data)

    Dim fieldName = ds.Tables(tbl).Columns(i).ColumnName ' find caption of field name

    Temp = Temp & fieldName &

    ","

    FTemp = FTemp &

    "'" & Data(i) & "'" & ","

    Next

    Temp = Strings.Left(Temp, Len(Temp) - 1)

    FTemp = Strings.Left(FTemp, Len(FTemp) - 1)

    Dim sql As String

    sql =

    "Insert into " & tbl & "(" & Temp & ")" & " Values(" & FTemp & ")"

    cm.CommandText = sql

    cm.Connection = cnn

    cm.ExecuteNonQuery()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    Sub UpDateDataToTable(ByVal tbl As String, ByVal FieldCon As String, ByVal ValueCon As String, ByVal ParamArray Data() As String)

    Try

    Dim Temp As String

    Dim cm As New SqlClient.SqlCommand

    Dim da As New SqlClient.SqlDataAdapter

    Dim ds As New DataSet()

    ds =

    New DataSet(tbl)

    da =

    New SqlClient.SqlDataAdapter("select * from " & tbl, cnn)

    da.Fill(ds, tbl)

    Dim i As Integer

    For i = 0 To UBound(Data)

    Dim fieldName = ds.Tables(tbl).Columns(i).ColumnName ' find caption of field name

    Temp = Temp & fieldName &

    "='" & Data(i) & "',"

    Next

    Temp = Strings.Left(Temp, Len(Temp) - 1)

    cm.CommandText =

    "update " & tbl & " set " & Temp & " where " & FieldCon & "='" & ValueCon & "'"

    cm.Connection = cnn

    cm.ExecuteNonQuery()

    Catch ex As Exception

    MsgBox(ex.Message)

    End Try

    End Sub

    End

    Module

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    • Form1 Code need 4 textbox,one Listview,2 Error provider,8 command Button, 1 Label name lblrecord
    • Captiontext :New , Update, delete, Save, |<, <<, >>, >|

    Public Class Form1

    Private WithEvents button1 As New Button

    Private mouseX, mouseY As Integer

    Private myMouseDown As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim Path As String

    Path = System.IO.Directory.GetCurrentDirectory &

    "\TestDb.mdb"

    DBConnection(Path)

    TransferData2txt(

    Me, "ProductInfo", 0, "TextBox1", "TextBox2", "TextBox3", "TextBox4")

    showDataTolst(

    "ProductInfo", Me.Lst, Optionshow.SHowAllFields)

    'Lst.Bounds = New Rectangle(New Point(100, 10), New Size(300, 200))

    Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

    End Sub

    Private Sub CmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdFirst.Click

    Lst.Items(LocPos).Selected =

    False

    LocPos = 0 :

    Me.lblrecord.Text = "1 / " & TotalPos + 1

    TransferData2txt(

    Me, "ProductInfo", LocPos, "TextBox1", "TextBox2", "TextBox3", "TextBox4")

    Lst.Items(LocPos).Selected =

    True

    End Sub

    Private Sub CmdLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdLast.Click

    Lst.Items(LocPos).Selected =

    False

    LocPos = TotalPos :

    Me.lblrecord.Text = TotalPos + 1 & " / " & TotalPos + 1

    TransferData2txt(

    Me, "ProductInfo", LocPos, "TextBox1", "TextBox2", "TextBox3", "TextBox4")

    Lst.Items(LocPos).Selected =

    True

    End Sub

    Private Sub CmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdNext.Click

    Lst.Items(LocPos).Selected =

    False

    If LocPos >= TotalPos Then

    lblrecord.Text = TotalPos + 1 &

    " OF " & TotalPos + 1

    Else

    LocPos = LocPos + 1

    lblrecord.Text = LocPos + 1 &

    " / " & TotalPos + 1

    TransferData2txt(

    Me, "ProductInfo", LocPos, "TextBox1", "TextBox2", "TextBox3", "TextBox4")

    Lst.Items(LocPos).Selected =

    True

    End If

    End Sub

    Private Sub CmdPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdPre.Click

    Lst.Items(LocPos).Selected =

    False

    If LocPos = 0 Then

    lblrecord.Text = 1 &

    " OF " & TotalPos + 1

    Else

    LocPos = LocPos - 1

    lblrecord.Text = LocPos + 1 &

    " / " & TotalPos + 1

    TransferData2txt(

    Me, "ProductInfo", LocPos, "TextBox1", "TextBox2", "TextBox3", "TextBox4")

    Lst.Items(LocPos).Selected =

    True

    End If

    End Sub

     

    Private Sub CmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdNew.Click

    TextBox1.Text = IDcreator(

    "ProductInfo", "Pro", "-", "0000", "ProID")

    TextBox2.Clear() : TextBox3.Clear() : TextBox4.Clear()

    End Sub

    Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click

    If TextBox3.Text = "" Or IsNumeric(TextBox3.Text) = False Then

    ErrorProvider1.SetError(TextBox3,

    "Invalid Data ! Please Check Your Data")

    ElseIf TextBox4.Text = "" Or IsNumeric(TextBox4.Text) = False Then

    ErrorProvider2.SetError(TextBox4,

    "Invalid Data ! Please Check Your Data")

    Else

    AddNewRecord(

    "Productinfo", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)

    showDataTolst(

    "ProductInfo", Me.Lst, Optionshow.SHowAllFields)

    End If

    End Sub

    Private Sub CmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDelete.Click

    DeleteData(

    "ProductInfo", "Proid", TextBox1.Text, CustomDel.DelCustom)

    showDataTolst(

    "ProductInfo", Me.Lst, Optionshow.SHowAllFields)

    End Sub

    Private Sub CmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdUpdate.Click

    UpDateDataToTable(

    "ProductInfo", "Proid", TextBox1.Text, TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)

    showDataTolst(

    "ProductInfo", Me.Lst, Optionshow.SHowAllFields)

    End Sub

     

    Private Sub Lst_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lst.SelectedIndexChanged

    Dim i%

    i = Lst.FocusedItem.Index

    'MsgBox(Lst.Items(i).ToString)

    TransferData2txt(

    Me, "ProductInfo", i, "TextBox1", "TextBox2", "TextBox3", "TextBox4")

    End Sub

     

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Me.Close()

    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

    myMouseDown =

    True

    mouseX = Cursor.Position.X -

    Me.Location.X

    mouseY = Cursor.Position.Y -

    Me.Location.Y

    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

    Static LastCursor As Point

    Dim NowCursor As Point = New Point(Cursor.Position.X, Cursor.Position.Y)

    If Point.op_Inequality(NowCursor, LastCursor) Then

    If myMouseDown Then

    Me.Location = New System.Drawing.Point(Cursor.Position.X - mouseX, Cursor.Position.Y - mouseY)

    End If

    LastCursor = Cursor.Position

    End If

    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

    myMouseDown =

    False

    End Sub

    End Class

    i hope you cam customize this code with your own project  thanks

    if this is not enough pls give some idea ...na

    from Anatha1

    • Post Points: 5
  • 11-14-2007 9:44 PM In reply to

    • SeanH
    • Not Ranked
    • Joined on 11-14-2007
    • Canada
    • New Member
    • Points 35

    Re: [74] ListView Control

    Maxjonz:

    I found that the following code didn't work for me

    ListView1.ListItems.Remove(ListView1.SelectedItem) '// removes the selected item

    I had to use

    ListView1.ListItems.Remove(ListView1.SelectedItem.Index) 

    Also  - it wasn't obvious to me to start with  ( I'm a newbie to listviews )

    but to  'get at' the data in the views one had to use

    ListView1.ListItems.Item(x)    where  x is a valid number in the  range 1 to ListView1.ListItems.Count  (NOT   0 to .Count - 1 as in normal Visual Basic)

     Cheers

    Maxjonz

     

     

    ListView1.SelectedItems(0).Remove()

    Or to remove all selected items:

    For Each I as ListViewItem In ListView1.SelectedItems

        I.Remove() 

    Next 

     

    • Post Points: 5
  • 11-14-2007 10:00 PM In reply to

    • SeanH
    • Not Ranked
    • Joined on 11-14-2007
    • Canada
    • New Member
    • Points 35

    Re: [74] ListView Control

    How to sort a ListView control? Here is the code I have written to handle just that. This code also includes a context menu of items I found I needed on almost every ListView I used (I use them alot).

    Can either create a file with this in it and add it to any project you need it in, or you can create a Class Library and add this code, compile it and add a reference to any project you wish to use this code in.

    How to use the ListViewSorter:

    In your ListView's ColumnClick event, place this line of code:

        ... 

        SortListViewColumn(sender, e)

         ...

     

    How to use the context menu:

    In your form's Load event, add the following code.

        ...

        Dim objListViewMenu As New clsListViewMenu(True)

        ListView1.ContextMenuStrip = objListViewMenu.ListViewMenu

         ...
     

    Or, if you have a Context Menu you need for the list view, you can merge them with this line of code:

        ... 

        ListView1.ContextMenuStrip = objListViewMenu.Merge(YOURCONTEXTMENU)

         ...

    There are various properties associated with the ListViewMenu object, all should be pretty self-evident.

    Enjoy...

     

     

    Imports System
    Imports System.Windows.Forms
    Imports System.ComponentModel
    Imports System.Drawing

    Namespace ListViewManipulation

      Public Module ListViewSorter

        Public Enum SortOrders
          Ascending = 0
          Descending
        End Enum

        Private Sub ErrorMessage(ByVal Text As String)
          MsgBox(Text, MsgBoxStyle.Exclamation, "YOUR TITLE HERE")
        End Sub

        Public Sub AutoSizeColumns(ByVal ListView As ListView, Optional ByVal EvenSpacing As Boolean = False)

          Dim intColumns As Integer = ListView.Columns.Count
          Dim intClientWidth As Integer = ListView.ClientRectangle.Width
          Dim intWidths(intColumns) As Integer
          Dim intWidth As Integer = 0

          With ListView
            .BeginUpdate()

            Try
              If EvenSpacing Then
                intWidth = CInt((intClientWidth) / intColumns)

                For Each objColumn As ColumnHeader In .Columns
                  objColumn.Width = intWidth
                Next

                If (intWidth * (intColumns)) > intClientWidth Then
                  Dim C As ColumnHeader = .Columns(intColumns - 1)
                  C.Width -= (intWidth * intColumns) - intClientWidth
                End If
              Else
                .AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
                For Each C As ColumnHeader In .Columns
                  intWidths(C.Index) = C.Width
                Next

                .AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
                For Each C As ColumnHeader In .Columns
                  If C.Width < intWidths(C.Index) Then C.Width = intWidths(C.Index)
                Next
              End If
            Catch ex As Exception
              ErrorMessage(ex.Message)
            End Try

            .EndUpdate()
          End With

        End Sub

        Private Function SmallerOf(ByVal value1 As Integer, ByVal value2 As Integer) As Integer

          Dim intReturn As Integer = 0

          If value1 <= value2 Then
            intReturn = value1
          Else
            intReturn = value2
          End If

          Return intReturn

        End Function

        Public Sub SortListViewColumn(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs)

          Try
            If sender Is Nothing Then Throw New NullReferenceException("[sender] is null.")
            If e Is Nothing Then Throw New NullReferenceException("[ColumnClickEventArgs] is null.")
            If sender.GetType IsNot GetType(ListView) Then Throw New Exception("[sender] must be a ListView control.")

            Static objHistory As New Dictionary(Of Integer, clsClickHistory)

            Dim objLast As clsClickHistory = Nothing
            Dim intHash As Integer = sender.GetHashCode

            If Not objHistory.ContainsKey(intHash) Then
              objHistory.Add(intHash, New clsClickHistory(e.Column, SortOrders.Ascending))
            End If

            objLast = objHistory(intHash)

            With objLast
              If .Column = e.Column Then
                .Order = .FlipOrder
              Else
                .Order = SortOrders.Descending
              End If

              .Column = e.Column
            End With

            With DirectCast(sender, ListView)
              .BeginUpdate()
              .ListViewItemSorter = New clsListViewSorter(objLast.Column, objLast.Order)
              .ListViewItemSorter = Nothing
              .EndUpdate()
            End With
          Catch ex As Exception
            ErrorMessage(ex.Message)
            With DirectCast(sender, ListView)
              .ListViewItemSorter = Nothing
              .EndUpdate()
            End With
          End Try

        End Sub

        Private Class clsListViewSorter
          Implements IComparer

          Private intColumn As Integer = 0
          Private intOrder As SortOrders = SortOrders.Ascending

          Public Sub New()

          End Sub

          Public Sub New(ByVal Column As Integer, ByVal SortOrder As SortOrders)
            Me.Column = Column
            Me.Order = SortOrder
          End Sub

          Public Property Order() As SortOrders
            Get
              Return intOrder
            End Get
            Set(ByVal value As SortOrders)
              If Not System.Enum.IsDefined(GetType(SortOrders), value) Then Throw New Exception("Value is not defined in Enum " & GetType(SortOrder).Name)
              intOrder = value
            End Set
          End Property

          Public Property Column() As Integer
            Get
              Return intColumn
            End Get
            Set(ByVal value As Integer)
              intColumn = value
            End Set
          End Property

          Private Function IsNumeric(ByVal Chars() As Char) As Boolean

            Dim bolPointSeen As Boolean = False

            For Each c As Char In Chars
              If Not Char.IsNumber(c) Then
                If c = "."c Then
                  If bolPointSeen Then Return False
                  bolPointSeen = True
                Else
                  Return False
                End If
              End If
            Next

            Return True

          End Function

          Public Function Compare(ByVal Item1 As Object, ByVal Item2 As Object) As Integer _
           Implements IComparer.Compare

            Dim chr1() As Char = DirectCast(Item1, ListViewItem).SubItems(intColumn).Text.ToCharArray
            Dim chr2() As Char = DirectCast(Item2, ListViewItem).SubItems(intColumn).Text.ToCharArray

            If chr1 = chr2 Then Return 0

            If IsNumeric(chr1) Then
              If IsNumeric(chr2) Then
                If intOrder = SortOrder.Ascending Then
                  Return Val(chr1).CompareTo(Val(chr2))
                Else
                  Return Val(chr2).CompareTo(Val(chr1))
                End If
              End If
            End If

            For i As Integer = 0 To SmallerOf(chr1.GetUpperBound(0), chr2.GetUpperBound(0))
              If chr1(i) <> chr2(i) Then
                If intOrder = SortOrder.Ascending Then
                  Return String.Compare(chr1(i), chr2(i))
                Else
                  Return String.Compare(chr2(i), chr1(i))
                End If
              End If
            Next

          End Function
        End Class

        Private Class clsClickHistory
          Dim intColumn As Integer = 0
          Dim intOrder As SortOrders = SortOrders.Ascending

          Public Sub New()

          End Sub

          Public Sub New(ByVal Column As Integer, ByVal Order As SortOrders)
            Me.Column = Column
            Me.Order = Order
          End Sub

          Public Property Column() As Integer
            Get
              Return intColumn
            End Get
            Set(ByVal value As Integer)
              intColumn = value
            End Set
          End Property

          Public Property Order() As SortOrders
            Get
              Return intOrder
            End Get
            Set(ByVal value As SortOrders)
              If Not System.Enum.IsDefined(GetType(SortOrders), value) Then Throw New Exception("Value is not defined in Enum " & GetType(SortOrders).Name)
              intOrder = value
            End Set
          End Property

          Public Function FlipOrder() As SortOrders
            Return DirectCast(Math.Abs(CInt(intOrder) - 1), SortOrders)
          End Function
        End Class

      End Module

      Public Class ListViewMenuCancelEventArgs
        Inherits System.ComponentModel.CancelEventArgs

        Dim objLV As ListView = Nothing
        Dim objCE As CancelEventArgs = Nothing

        Public Sub New(ByVal ListView As ListView, ByVal e As CancelEventArgs)
          Me.ListView = ListView
          Me.CancelEventArgs = e
        End Sub

        Public Property ListView() As ListView
          Get
            Return objLV
          End Get
          Set(ByVal value As ListView)
            objLV = value
          End Set
        End Property

        Public Property CancelEventArgs() As CancelEventArgs
          Get
            Return objCE
          End Get
          Set(ByVal value As CancelEventArgs)
            objCE = value
          End Set
        End Property

      End Class

      Public Class clsListViewMenu

        Public Event ContextMenuOpening(ByVal sender As Object, ByVal e As ListViewMenuCancelEventArgs)

        Dim WithEvents mnuViewStyle As New ContextMenuStrip
        Dim WithEvents mnuViewView As ToolStripMenuItem = Nothing
        Dim WithEvents mnuViewSort As ToolStripMenuItem = Nothing
        Dim WithEvents mnuViewAutosize As ToolStripMenuItem = Nothing
        Dim WithEvents objLargeIcons As