|
-
Feb 19th, 2012, 12:14 AM
#1
Thread Starter
Frenzied Member
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:
If Not String.IsNullOrEmpty(CStr(Dialog1.ComboBox3.SelectedItem)) Then
Dim tName As String = Dialog1.ComboBox3.SelectedItem.ToString
For Each item As String In Dialog1.CheckedListBox1.CheckedItems()
For i = table.Rows.Count - 1 To 0 Step -1
If (table.Rows(i)(tName)).Equals(item) Then
table.Rows.RemoveAt(i)
End If
Next
Next
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:
Private Sub DelRecs()
If Not String.IsNullOrEmpty(CStr(Dialog1.ComboBox3.SelectedItem)) Then
Dim tName As String = CStr(Dialog1.ComboBox3.SelectedItem)
For Each item As String In Dialog1.CheckedListBox1.CheckedItems()
list.Add(item)
Next
BackgroundWorker5.RunWorkerAsync()
End If
End Sub
bgw Code:
Private Sub BackgroundWorker5_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker5.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
For Each item As String In list
For i = table.Rows.Count - 1 To 0 Step -1
If (table.Rows(i)(tName)).Equals(item) Then
table.Rows.RemoveAt(i)
End If
Next
Next
End Sub
declarations Code:
Dim item As String
Dim list As New List(Of String)
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
-
Feb 19th, 2012, 02:06 AM
#2
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.
-
Feb 19th, 2012, 03:00 AM
#3
Thread Starter
Frenzied Member
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?
-
Feb 19th, 2012, 05:58 AM
#4
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:
Public ReadOnly Property ColumnName() As String Get Return CStr(ComboBox1.SelectedItem) End Get End Property Public ReadOnly Property SelectedValues() As String() Get Return CheckedListBox.CheckedItems.Cast(Of String)().ToArray() End Get End Property Private Sub ComboBox1_SelectionChangeCommitted(...) Handles ComboBox1.SelectionChangeCommitted okButton.Enabled = True End Sub
You can then use the dialogue as it should be:
vb.net Code:
Using dialogue As New SomeDialogue If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then BackgroundWorker1.RunWorkerAsync(New WorkerData With {.ColumnName = dialogue.ColumnName, .SelectedValues = dialogue.SelectedValues}) End If End Using
In the DoWork event handler you would do something like this:
vb.net Code:
Dim data = DirectCast(e.Argument, WorkerData) For Each value In data.SelectedValues For Each row In table.Select(String.Format("{0} = '{1}'", data.ColumnName, value)) table.Rows.Remove(row) Next Next
-
Feb 19th, 2012, 06:02 AM
#5
Re: For Each CheckedListBox in BGW
 Originally Posted by billboy
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.
-
Feb 19th, 2012, 11:43 AM
#6
Thread Starter
Frenzied Member
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
-
Feb 19th, 2012, 05:41 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|