1

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?

djv
  • 15,168
  • 7
  • 48
  • 72
DVELPR
  • 184
  • 1
  • 2
  • 13
  • 2
    Never use `Throw ex` in a `Catch` block. If you want to rethrow an exception then just use `Throw`. That said, if all you're going to do is rethrow the exception, what do you have a `Catch` block for at all? You don't need one if you have a `Finally` block so only have one if you have a purpose for it. – jmcilhinney Feb 26 '20 at 15:29
  • without throw ex i could not catch the error while Await Task.Run(sub....) function, i post the full code sir – DVELPR Feb 26 '20 at 15:33
  • 2
    Of course you could. The only reason you need to rethrow the exception is that you caught it in the first place. As I already said, don't catch it at all, then you won't need to rethrow it. Get rid of the `Catch` block altogether. If you really must include a useless `Catch` block, at least rethrow properly and just use `Throw` rather than `Throw ex`. – jmcilhinney Feb 26 '20 at 16:06
  • 1
    Adding to what @jmcilhinney said, `Try ... Catch ex ... Throw ex` and `Try ... Catch... Throw` are *not* the same. `Throw ex` will reset the stack trace, so when this new exception is caught, the stack trace will only go as far back as this `Throw ex` point. Using `Throw` will preserve the stack trace. However, you shouldn't ever use `Catch .. Throw` unless you had handled an explicit exception type prior such as `Try ... Catch ex As SpecificExceptionType ... Catch ... Throw`. See [this answer](https://stackoverflow.com/a/730255/832052) for more. – djv Feb 26 '20 at 16:16
  • 1
    ahhh !! got it thanks i changed throw ex to throw ..appreciate – DVELPR Feb 26 '20 at 16:30
  • 1
    Except you don't need catch at all if you are just going to throw it without do anything else. Neither `Throw` nor `Throw ex` is a good idea here. – djv Feb 26 '20 at 17:02

2 Answers2

3

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}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

@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
DVELPR
  • 184
  • 1
  • 2
  • 13
  • 1
    It's a little strange to have a `If _lsv.InvokeRequired Then` without following it with an `Else` where the method is not invoked on the UI. From the looks of it, that condition will always be true, so you can probably remove it and just invoke always. And still you have `Catch ex As Exception ... Throw`. It serves zero purpose and should be removed. – djv Feb 26 '20 at 17:06
  • thanks for great help and pointing me lot of unusual codes. i just updated with your guidelines .. if error happened how to do Listview ResumeLayout – DVELPR Feb 26 '20 at 17:41
  • 1
    Ok, now I understand. You can use `Try ... Finally` without `Catch`. Even if an exception happens inside the `Try`, it will run the code inside `Finally` before being caught anywhere. – djv Feb 26 '20 at 19:11
  • thanQ so much for your great help. i am using finally block for release the object from memory in every function is that correct way ? – DVELPR Feb 27 '20 at 02:15
  • @djv, after removed catch block in [_lamLsv = sub() try ... finally] then i could not get stacktrace get only Message. but using catch block[_lamLsv = sub() try ...Catch ... finally] i get stacktrace ...bit confused ... – DVELPR Feb 27 '20 at 04:31