How do I access ComboBox.SelectedText from another thread?

P

Paulers

Hello all,

I have been pulling my hair out trying to figure out how to access the
selected text in a combobox from another thread. I can get it working
with a textbox but it does not with a combobox. Could someone be so
kind as to show me an example of how this is accomplished? Many thanks
in advance.
 
T

Tom Shelton

Paulers said:
Hello all,

I have been pulling my hair out trying to figure out how to access the
selected text in a combobox from another thread. I can get it working
with a textbox but it does not with a combobox. Could someone be so
kind as to show me an example of how this is accomplished? Many thanks
in advance.

So, what are you doing with the textbox? Are you invoking a method to
return the selected text, or are you trying to access directly? Can
you be a little more specific about what doesn't work? A short code
snip-it would be helpful.
 
C

Cor Ligthert[MVP]

Tom,

I am also very curious about that code, how to set a combobox with data that
is assynchroon processed and still accoording the users interface actions.

I realy hope to see that code.

I assume that you asks this more polite then I mostly do as it is about
this, however have that same feeling and that we then probably come with the
same answers.

:)

Cor
 
P

Paulers

So, what are you doing with the textbox?  Are you invoking a method to
return the selected text, or are you trying to access directly?  Can
you be a little more specific about what doesn't work?  A short code
snip-it would be helpful.

Thanks for the replys
in my form I am declaring:

Public Delegate Sub DelegateUIUpdate(ByVal dgv As DataGridView,
ByVal ds As DataSet)

In the Button Click I have

Dim workerThread As Thread = New Thread(AddressOf
CallsByScriptWorker)
workerThread.Start()

then I have two subs, one to do the work and the other to update the
UI thread.

Sub CallsByScriptWorker()
Dim sb As StringBuilder = New StringBuilder
sb.Append("SELECT rcd.DateTime, rcd.DialedNumberString, ")
sb.Append("rcd.TargetLabel as TargetVDN, ")
sb.Append("r.EnterpriseName as Route, ")
sb.Append("ms.EnterpriseName as Script, ")
sb.Append("rcd.FinalObjectID as Node, ")
sb.Append("rc.EnterpriseName, ")
sb.Append("rcd.ANI, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5000 THEN
ECCValue ELSE NULL END) AS CallType, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5001 THEN
ECCValue ELSE NULL END) AS ExternalID, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5002 THEN
ECCValue ELSE NULL END) AS ClientCode, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5003 THEN
ECCValue ELSE NULL END) AS FirstName, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5004 THEN
ECCValue ELSE NULL END) AS LastName, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5005 THEN
ECCValue ELSE NULL END) AS DOB, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5006 THEN
ECCValue ELSE NULL END) AS RefillComplete, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5010 THEN
ECCValue ELSE NULL END) AS RXArray ")
sb.Append("from Route_Call_Detail rcd ")
sb.Append("LEFT OUTER JOIN Route_Call_Variable rcv on
rcd.RecoveryKey = rcv.RCDRecoveryKey ")
sb.Append("LEFT OUTER JOIN Script s on s.ScriptID =
rcd.ScriptID ")
sb.Append("LEFT OUTER JOIN Master_Script ms on
ms.MasterScriptID = s.MasterScriptID ")
sb.Append("LEFT OUTER JOIN Routing_Client rc on
rcd.RoutingClientID = rc.RoutingClientID ")
sb.Append("LEFT OUTER JOIN Route r on r.RouteID = rcd.RouteID
")
sb.Append("where rcd.DateTime between '" &
Me.BeginDatePicker.Text & " " & Me.BeginTimePicker.Text & "' and '" &
Me.EndDatePicker.Text & " " & Me.EndTimePicker.Text & "' ")
sb.Append("and ms.EnterpriseName ='" &
Me.CallsByScripCombo.SelectedText & "' ")
sb.Append(" GROUP BY rcd.DateTime, rcd.DialedNumberString,
rcd.TargetLabel, r.EnterpriseName, ms.EnterpriseName,
rcd.FinalObjectID, rc.EnterpriseName, rcd.ANI ")
sb.Append("Order by rcd.DateTime ")
Dim Conn As SqlConnection = New SqlConnection(myConnString)
Dim DNDS As DataSet = New DataSet
Dim Adp As SqlDataAdapter = New
SqlDataAdapter(sb.ToString.ToString, Conn)
Adp.Fill(DNDS, "temp1")
Dim delUIUpdate As New DelegateUIUpdate(AddressOf UIUpdate)
Invoke(delUIUpdate, Me.CallsDataGridView, DNDS)
Conn.Close()
End Sub


Sub UIUpdate(ByVal dgv As DataGridView, ByVal ds As DataSet)
dgv.DataSource = ds.Tables(0).DefaultView
Me.CallsQueryButton.Enabled = True
End Sub
 
P

Paulers

Thanks for the replys
in my form I am declaring:

    Public Delegate Sub DelegateUIUpdate(ByVal dgv As DataGridView,
ByVal ds As DataSet)

In the Button Click I have

            Dim workerThread As Thread = New Thread(AddressOf
CallsByScriptWorker)
            workerThread.Start()

then I have two subs, one to do the work and the other to update the
UI thread.

Sub CallsByScriptWorker()
        Dim sb As StringBuilder = New StringBuilder
        sb.Append("SELECT  rcd.DateTime, rcd.DialedNumberString,")
        sb.Append("rcd.TargetLabel as TargetVDN, ")
        sb.Append("r.EnterpriseName as Route, ")
        sb.Append("ms.EnterpriseName as Script, ")
        sb.Append("rcd.FinalObjectID as Node, ")
        sb.Append("rc.EnterpriseName, ")
        sb.Append("rcd.ANI, ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5000 THEN
ECCValue ELSE NULL END) AS CallType,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5001 THEN
ECCValue ELSE NULL END) AS ExternalID,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5002 THEN
ECCValue ELSE NULL END) AS ClientCode,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5003 THEN
ECCValue ELSE NULL END) AS FirstName,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5004 THEN
ECCValue ELSE NULL END) AS LastName,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5005 THEN
ECCValue ELSE NULL END) AS DOB,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5006 THEN
ECCValue ELSE NULL END) AS RefillComplete,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5010 THEN
ECCValue ELSE NULL END) AS RXArray  ")
        sb.Append("from Route_Call_Detail rcd ")
        sb.Append("LEFT OUTER JOIN Route_Call_Variable rcv on
rcd.RecoveryKey = rcv.RCDRecoveryKey ")
        sb.Append("LEFT OUTER JOIN Script s on s.ScriptID =
rcd.ScriptID  ")
        sb.Append("LEFT OUTER JOIN Master_Script ms on
ms.MasterScriptID = s.MasterScriptID  ")
        sb.Append("LEFT OUTER JOIN Routing_Client rc on
rcd.RoutingClientID = rc.RoutingClientID  ")
        sb.Append("LEFT OUTER JOIN Route r on r.RouteID = rcd.RouteID
")
        sb.Append("where rcd.DateTime between '" &
Me.BeginDatePicker.Text & " " & Me.BeginTimePicker.Text & "' and '" &
Me.EndDatePicker.Text & " " & Me.EndTimePicker.Text & "' ")
        sb.Append("and ms.EnterpriseName ='" &
Me.CallsByScripCombo.SelectedText & "' ")
        sb.Append(" GROUP BY rcd.DateTime, rcd.DialedNumberString,
rcd.TargetLabel, r.EnterpriseName, ms.EnterpriseName,
rcd.FinalObjectID, rc.EnterpriseName, rcd.ANI ")
        sb.Append("Order by rcd.DateTime ")
        Dim Conn As SqlConnection = New SqlConnection(myConnString)
        Dim DNDS As DataSet = New DataSet
        Dim Adp As SqlDataAdapter = New
SqlDataAdapter(sb.ToString.ToString, Conn)
        Adp.Fill(DNDS, "temp1")
        Dim delUIUpdate As New DelegateUIUpdate(AddressOf UIUpdate)
        Invoke(delUIUpdate, Me.CallsDataGridView, DNDS)
        Conn.Close()
    End Sub

    Sub UIUpdate(ByVal dgv As DataGridView, ByVal ds As DataSet)
        dgv.DataSource = ds.Tables(0).DefaultView
        Me.CallsQueryButton.Enabled = True
    End Sub- Hide quoted text -

- Show quoted text -

Oops I forgot to mention that the issue in in the sql query when I
call Me.CallsByScripCombo.SelectedText. I works fine when I access the
values of the other controls like the datetimechooser and textbox.

thanks!
 
C

Cor Ligthert[MVP]

Paulers,

And your goal with this is?

Beside making it complex and slowing the process down.

You can give your dataset to your main thread where your UI is in, however
as that can only after that it is filled. The process of updating you can do
in another thread, however you have to check if that has be done well before
you can go on with the rest of your processing.

Therefore you have to synchronise the asynchronis processing. A little bit
crazy in my idea and slurping processing time.

Just keeping it in one thread will synchronise this process in one time.

Just my idea.

Cor

"Paulers" <[email protected]> schreef in bericht
So, what are you doing with the textbox? Are you invoking a method to
return the selected text, or are you trying to access directly? Can
you be a little more specific about what doesn't work? A short code
snip-it would be helpful.

Thanks for the replys
in my form I am declaring:

Public Delegate Sub DelegateUIUpdate(ByVal dgv As DataGridView,
ByVal ds As DataSet)

In the Button Click I have

Dim workerThread As Thread = New Thread(AddressOf
CallsByScriptWorker)
workerThread.Start()

then I have two subs, one to do the work and the other to update the
UI thread.

Sub CallsByScriptWorker()
Dim sb As StringBuilder = New StringBuilder
sb.Append("SELECT rcd.DateTime, rcd.DialedNumberString, ")
sb.Append("rcd.TargetLabel as TargetVDN, ")
sb.Append("r.EnterpriseName as Route, ")
sb.Append("ms.EnterpriseName as Script, ")
sb.Append("rcd.FinalObjectID as Node, ")
sb.Append("rc.EnterpriseName, ")
sb.Append("rcd.ANI, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5000 THEN
ECCValue ELSE NULL END) AS CallType, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5001 THEN
ECCValue ELSE NULL END) AS ExternalID, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5002 THEN
ECCValue ELSE NULL END) AS ClientCode, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5003 THEN
ECCValue ELSE NULL END) AS FirstName, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5004 THEN
ECCValue ELSE NULL END) AS LastName, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5005 THEN
ECCValue ELSE NULL END) AS DOB, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5006 THEN
ECCValue ELSE NULL END) AS RefillComplete, ")
sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5010 THEN
ECCValue ELSE NULL END) AS RXArray ")
sb.Append("from Route_Call_Detail rcd ")
sb.Append("LEFT OUTER JOIN Route_Call_Variable rcv on
rcd.RecoveryKey = rcv.RCDRecoveryKey ")
sb.Append("LEFT OUTER JOIN Script s on s.ScriptID =
rcd.ScriptID ")
sb.Append("LEFT OUTER JOIN Master_Script ms on
ms.MasterScriptID = s.MasterScriptID ")
sb.Append("LEFT OUTER JOIN Routing_Client rc on
rcd.RoutingClientID = rc.RoutingClientID ")
sb.Append("LEFT OUTER JOIN Route r on r.RouteID = rcd.RouteID
")
sb.Append("where rcd.DateTime between '" &
Me.BeginDatePicker.Text & " " & Me.BeginTimePicker.Text & "' and '" &
Me.EndDatePicker.Text & " " & Me.EndTimePicker.Text & "' ")
sb.Append("and ms.EnterpriseName ='" &
Me.CallsByScripCombo.SelectedText & "' ")
sb.Append(" GROUP BY rcd.DateTime, rcd.DialedNumberString,
rcd.TargetLabel, r.EnterpriseName, ms.EnterpriseName,
rcd.FinalObjectID, rc.EnterpriseName, rcd.ANI ")
sb.Append("Order by rcd.DateTime ")
Dim Conn As SqlConnection = New SqlConnection(myConnString)
Dim DNDS As DataSet = New DataSet
Dim Adp As SqlDataAdapter = New
SqlDataAdapter(sb.ToString.ToString, Conn)
Adp.Fill(DNDS, "temp1")
Dim delUIUpdate As New DelegateUIUpdate(AddressOf UIUpdate)
Invoke(delUIUpdate, Me.CallsDataGridView, DNDS)
Conn.Close()
End Sub


Sub UIUpdate(ByVal dgv As DataGridView, ByVal ds As DataSet)
dgv.DataSource = ds.Tables(0).DefaultView
Me.CallsQueryButton.Enabled = True
End Sub
 
P

Paulers

Paulers,

And your goal with this is?

Beside making it complex and slowing the process down.

You can give your dataset to your main thread where your UI is in, however
as that can only after that it is filled. The process of updating you can do
in another thread, however you have to check if that has be done well before
you can go on with the rest of your processing.

Therefore you have to synchronise the asynchronis processing. A little bit
crazy in my idea and slurping processing time.

Just keeping it in one thread will synchronise this process in one time.

Just my idea.

Cor

"Paulers" <[email protected]> schreef in bericht

Thanks for the replys
in my form I am declaring:

    Public Delegate Sub DelegateUIUpdate(ByVal dgv As DataGridView,
ByVal ds As DataSet)

In the Button Click I have

            Dim workerThread As Thread = New Thread(AddressOf
CallsByScriptWorker)
            workerThread.Start()

then I have two subs, one to do the work and the other to update the
UI thread.

Sub CallsByScriptWorker()
        Dim sb As StringBuilder = New StringBuilder
        sb.Append("SELECT  rcd.DateTime, rcd.DialedNumberString,")
        sb.Append("rcd.TargetLabel as TargetVDN, ")
        sb.Append("r.EnterpriseName as Route, ")
        sb.Append("ms.EnterpriseName as Script, ")
        sb.Append("rcd.FinalObjectID as Node, ")
        sb.Append("rc.EnterpriseName, ")
        sb.Append("rcd.ANI, ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5000 THEN
ECCValue ELSE NULL END) AS CallType,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5001 THEN
ECCValue ELSE NULL END) AS ExternalID,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5002 THEN
ECCValue ELSE NULL END) AS ClientCode,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5003 THEN
ECCValue ELSE NULL END) AS FirstName,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5004 THEN
ECCValue ELSE NULL END) AS LastName,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5005 THEN
ECCValue ELSE NULL END) AS DOB,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5006 THEN
ECCValue ELSE NULL END) AS RefillComplete,  ")
        sb.Append("MAX(CASE WHEN ExpandedCallVariableID = 5010 THEN
ECCValue ELSE NULL END) AS RXArray  ")
        sb.Append("from Route_Call_Detail rcd ")
        sb.Append("LEFT OUTER JOIN Route_Call_Variable rcv on
rcd.RecoveryKey = rcv.RCDRecoveryKey ")
        sb.Append("LEFT OUTER JOIN Script s on s.ScriptID =
rcd.ScriptID  ")
        sb.Append("LEFT OUTER JOIN Master_Script ms on
ms.MasterScriptID = s.MasterScriptID  ")
        sb.Append("LEFT OUTER JOIN Routing_Client rc on
rcd.RoutingClientID = rc.RoutingClientID  ")
        sb.Append("LEFT OUTER JOIN Route r on r.RouteID = rcd.RouteID
")
        sb.Append("where rcd.DateTime between '" &
Me.BeginDatePicker.Text & " " & Me.BeginTimePicker.Text & "' and '" &
Me.EndDatePicker.Text & " " & Me.EndTimePicker.Text & "' ")
        sb.Append("and ms.EnterpriseName ='" &
Me.CallsByScripCombo.SelectedText & "' ")
        sb.Append(" GROUP BY rcd.DateTime, rcd.DialedNumberString,
rcd.TargetLabel, r.EnterpriseName, ms.EnterpriseName,
rcd.FinalObjectID, rc.EnterpriseName, rcd.ANI ")
        sb.Append("Order by rcd.DateTime ")
        Dim Conn As SqlConnection = New SqlConnection(myConnString)
        Dim DNDS As DataSet = New DataSet
        Dim Adp As SqlDataAdapter = New
SqlDataAdapter(sb.ToString.ToString, Conn)
        Adp.Fill(DNDS, "temp1")
        Dim delUIUpdate As New DelegateUIUpdate(AddressOf UIUpdate)
        Invoke(delUIUpdate, Me.CallsDataGridView, DNDS)
        Conn.Close()
    End Sub

    Sub UIUpdate(ByVal dgv As DataGridView, ByVal ds As DataSet)
        dgv.DataSource = ds.Tables(0).DefaultView
        Me.CallsQueryButton.Enabled = True
    End Sub- Hide quoted text -

- Show quoted text -
Cor,

Thanks, I appreciate your input. My intention was to not lock up the
user interface while the SQL query was taking place. At first I was
using the single threas but I saw a thread demo on
learnvisualstudio.net and decided to play with it. Is there another
way that this can be accomplished?

Thank you.
 
C

Cor Ligthert[MVP]

Paulers,

The problem is that in this kind of processing you can only keep one thing
open.
That is the close box. All other things have to wait until the processing is
done (most of the time is this not even recognisable by this kind of
database processing). It goes that fast that as soon as the user has reached
the point he want to select, then the processing is already done.

What is in your case what the user can do in between of those processes?

(I know that the sample is often used, however in my idea more to show it
then that it has any practical benefit).

It is something else of course when you are loading a very large solution,
however in many cases you have as well to wait until it is complete ready,
you cannot stop that then only with the taskmanager. A sample of that is
Visual Studio. The price that is paid in processing time in normal
situations is probably to high.

In other words don't expect that multi threading is a solution for
everything it is in the oposite, there are very few situations were you can
use it. (To be honest in data processing I don't know one).

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top