简体   繁体   中英

VB.net Add new listview item in listview with Checked True?

How can I set Listviewitem is Checked True/False with AddRange function?

Try
    _lsv.SuspendLayout()
    _lsv.Items.AddRange((From itm In _desrz.Select(           
                             Function(x) New ListViewItem(New String() {
                                 x.FolderName,
                                 x.FolderPath}
                             ))).ToArray)
Catch ex As Exception
    Throw ex
Finally
    _lsv.ResumeLayout() : _lsv.Update() : _lsv.Refresh()
End Try

_desrz is a Folder object with 3 properties: FolderPath , FolderName , and FolderChecked . I can add FolderPath and FolderName to the listview, but I could not set the check property value is true/false?

You can set a property on a newly constructed object using an object initialiser:

New SomeType With {.SomeProperty = someValue, .SomeOtherProperty = someOtherValue}

or, with constructor arguments:

New SomeType(someArgument, someOtherArgument) With {.SomeProperty = someValue, .SomeOtherProperty = someOtherValue}

In your case, your type is ListViewItem , your constructor is that String array and your property is Checked :

New ListViewItem({x.FolderName, x.FolderPath}) With {.Checked = True}

@jmchinney answer, and this is working code below ,

          Public Shared Async Function LoadFoldersList(_lsv As ListView, _JsonFoldersListPath As String) As Task(Of Boolean)
        Return Await Task.Run(Async Function()
                                  Dim _rslt As Boolean = False
                                  Dim _itmS As ListViewItemCollection = Nothing
                                  Dim _desrz As List(Of TFolder) = Nothing
                                  Dim _lamLsv As Action = Nothing
                                  Try
                                      If My.Computer.FileSystem.FileExists(_JsonFoldersListPath) Then
                                          Dim _cntnts = My.Computer.FileSystem.ReadAllText(_JsonFoldersListPath)
                                          If Not IsNothing(_cntnts) AndAlso Not String.IsNullOrEmpty(_cntnts) Then
                                              _desrz = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of TFolder))(Await _cntnts.ToDeCryptWOP)
                                              If Not IsNothing(_desrz) Then
                                                  _lamLsv = Sub()
                                                                Try
                                                                    _lsv.SuspendLayout()
                                                                    _lsv.Items.AddRange((From itm In _desrz.Select(
                                                                                                 Function(x) New ListViewItem(New String() {
                                                                                                 x.FolderName,
                                                                                                 x.FolderPath}
                                                                                                 ) With {.Checked = x.Checked})).ToArray)
                                                                Finally
                                                                    _lsv.ResumeLayout() : _lsv.Update() : _lsv.Refresh()
                                                                End Try
                                                            End Sub
                                                  If _lsv.InvokeRequired Then
                                                      _lsv.Invoke(Sub() _lamLsv())
                                                  Else
                                                      _lamLsv()
                                                  End If
                                              End If
                                          End If
                                      End If
                                      _rslt = True
                                  Catch ex As Exception
                                      L.Log.WritelogAsync(ex)
                                  Finally
                                      _lamLsv = Nothing : _desrz = Nothing : _itmS = Nothing 'is that correct for prevent memroy leaking
                                  End Try
                                  Return _rslt
                              End Function)
    End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM