Results 1 to 7 of 7

Thread: For Each CheckedListBox in BGW

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    For Each CheckedListBox in BGW

    Hi guys I am trying to implement this bit of code in a BGW have it in a Sub now but need it in BGW

    Sub Code Code:
    1. If Not String.IsNullOrEmpty(CStr(Dialog1.ComboBox3.SelectedItem)) Then
    2. Dim tName As String = Dialog1.ComboBox3.SelectedItem.ToString
    3.             For Each item As String In Dialog1.CheckedListBox1.CheckedItems()
    4.                 For i = table.Rows.Count - 1 To 0 Step -1
    5.                     If (table.Rows(i)(tName)).Equals(item) Then
    6.                         table.Rows.RemoveAt(i)
    7.                     End If
    8.                 Next
    9.             Next
    10.         End If

    I have a button that shows a dialogbox that contains various combo, text and checklist boxesx based on the selectedindex of the comboBox a checklist box is populated with all the unique fields in that column


    What would be the most "proper" way to do this? This is what i have so far:

    Sub Code:
    1. Private Sub DelRecs()
    2.         If Not String.IsNullOrEmpty(CStr(Dialog1.ComboBox3.SelectedItem)) Then
    3.             Dim tName As String = CStr(Dialog1.ComboBox3.SelectedItem)
    4.             For Each item As String In Dialog1.CheckedListBox1.CheckedItems()
    5.                 list.Add(item)
    6.             Next
    7.             BackgroundWorker5.RunWorkerAsync()
    8.         End If
    9.     End Sub

    bgw Code:
    1. Private Sub BackgroundWorker5_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker5.DoWork
    2.         Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
    3.         For Each item As String In list
    4.             For i = table.Rows.Count - 1 To 0 Step -1
    5.                 If (table.Rows(i)(tName)).Equals(item) Then
    6.                     table.Rows.RemoveAt(i)
    7.                 End If
    8.             Next
    9.         Next
    10.     End Sub

    declarations Code:
    1. Dim item As String
    2.     Dim list As New List(Of String)
    3.     Dim tName As String

    I am really confused as I have something very similiar in another BGW and it works fine

    But something tells me I am not doing this properly

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: For Each CheckedListBox in BGW

    I'm a bit of a stickler for clarity in terminology so I feel the need to point out that you do not do anything "in a BackgroundWorker", just as you don't do anything "in a Timer". You do something in the DoWork event handler of the BackgroundWorker.

    Anyway, the whole point of the BackgroundWorker is to do work in the background. Getting anything from or passing anything to a control is NOT background work. Controls are UI elements and the UI is the very definition of the foreground.

    If your DoWork event handler needs data from the UI then you are supposed to pass it in, which you do by getting it BEFORE calling RunWorkerAsync and then passing it as an argument WHEN calling RunWorkerAsync. You then get that data back from the e.Argument property in the event handler. You can follow the CodeBank link in my signature and check out my thread on Using The BackgroundWorker for an example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: For Each CheckedListBox in BGW

    Thanks J
    I have been reading you code bank and learning. I came across another post while googling around you had somewhere about handling large files in batches
    I dont see that here though perhaps you remember where it was posted?

    I seem to be having a problem importing a rather large txt file into a DataTable and I am wondering if the batch method you posted might do the trick?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: For Each CheckedListBox in BGW

    I didn't read your code too carefully the first time around but, now that I have, I have several issues with it. While it's obviously possible, accessing those controls from outside the dialogue is poor form.

    First, you should only be enabling the OK button on your dialogue if the user selects an item in the ComboBox. That way, you simply test the result of ShowDialog and that will tell you whether you should continue. If it's OK then you know an item was selected and you can retrieve it.

    Second, the ComboBox should be Private and the item selected should be exposed via a property. The items checked in the CheckedListBox should also be exposed via a property, so you shouldn't even have to know that there is a CheckedListBox from outside the dialogue.

    So, your dialogue would look something like this:
    vb.net Code:
    1. Public ReadOnly Property ColumnName() As String
    2.     Get
    3.         Return CStr(ComboBox1.SelectedItem)
    4.     End Get
    5. End Property
    6.  
    7. Public ReadOnly Property SelectedValues() As String()
    8.     Get
    9.         Return CheckedListBox.CheckedItems.Cast(Of String)().ToArray()
    10.     End Get
    11. End Property
    12.  
    13. Private Sub ComboBox1_SelectionChangeCommitted(...) Handles ComboBox1.SelectionChangeCommitted
    14.     okButton.Enabled = True
    15. End Sub
    You can then use the dialogue as it should be:
    vb.net Code:
    1. Using dialogue As New SomeDialogue
    2.     If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
    3.         BackgroundWorker1.RunWorkerAsync(New WorkerData With {.ColumnName = dialogue.ColumnName, .SelectedValues = dialogue.SelectedValues})
    4.     End If
    5. End Using
    In the DoWork event handler you would do something like this:
    vb.net Code:
    1. Dim data = DirectCast(e.Argument, WorkerData)
    2.  
    3. For Each value In data.SelectedValues
    4.     For Each row In table.Select(String.Format("{0} = '{1}'", data.ColumnName, value))
    5.         table.Rows.Remove(row)
    6.     Next
    7. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: For Each CheckedListBox in BGW

    Quote Originally Posted by billboy View Post
    Thanks J
    I have been reading you code bank and learning. I came across another post while googling around you had somewhere about handling large files in batches
    I dont see that here though perhaps you remember where it was posted?

    I seem to be having a problem importing a rather large txt file into a DataTable and I am wondering if the batch method you posted might do the trick?
    This doesn't seem to have anything to do with the topic of this thread. Regardless, I don't know which post you mean. The first thing that cam to mind was this:

    www.vbforums.com/showthread.php?t=672445

    but that's one of your threads anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: For Each CheckedListBox in BGW

    Thanks that wasnt the thread, but it is the same project I am working on, nvertheless i wil lstart on a new thread for that if I can find your post regarding that topic.

    I dont know where and how I define/declare "WorkerData"
    I get a squirrley line indicating its not been defined

    Thanks

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: For Each CheckedListBox in BGW

    Look at how WorkerData is used. At one point it is preceded by the New keyword and at another it is used withe DirectCast. It should be obvious from both of those that it is a type, i.e. a class or structure. It's obviously not a type from the Framework because it doesn't appear in the documentation, so it's obviously a type that you define yourself. You define it specifically for this sole purpose, because you need and object that has specific properties for this specific purpose.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width