Annoying Windows Forms Problem

S

Steve A.

Here is the scenario:

I have a project that uses two forms. Form1 uses a SQL
Connection, several DataAdapters for various tables,
DataSet, and a DataView that I filter by user-selected
field names and associated criteria. Form2 has a couple
of read-only text fields to show the user the search
field and criteria they entered plus a DataGrid that is
populated from the DataView. There is a Public Class
defined to hold shared variables.

Code:
Public Class SharedVariables
Public Shared SelectedKey As String ' Make it shared
End Class

Code:
' Define an instance of the search form
Dim Form2 As New frmSearchResults

' Set the txtSearchingFor textbox and field name
to data from the calling form
Form2.txtSearchingFor.Text = txtSearch.Text
..
..
..
If cboSearch.SelectedIndex = 5 Then
myField = "Phone"
Form2.txtFieldName.Text = "Phone"
End If

dvDonors.Table = dsDataSet.Donors
dvDonors.RowFilter = myField & " LIKE" & "'" &
txtSearch.Text & "*" & "'"
Form2.grdGrid.DataSource = dvDonors

' Display the form.
Form2.Show()
txtSelectedKey.Text = SharedVariables.SelectedKey

*** txtSelectedKey.Text after the first execution is Null.

All of this works fine up to this point - I can show the
second form with the filtered data.

Here is the malfunctioning code:

Private Sub grdGrid_DoubleClick(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
grdGrid.DoubleClick
SharedVariables.SelectedKey = grdGrid.Item
(grdGrid.CurrentCell)
End Sub

The shared variable is not updated with the datagrid item
the first time through. (it's null) If you execute the
search again (after closing Form2) you get the datagrid
item from the first execution. Execute it again (3rd
time) you get the item from the 2nd execution and so on.

What is causing this behaviour?
 
F

Fergus Cooney

Hi Steve,

Form2.Show()
txtSelectedKey.Text = SharedVariables.SelectedKey

Show goes of to Form2 and Shows it, leaves it there to do its thing and
comes back <immediately>. Then it sets txtSelected to the existing value of
SharedVariables.SelectedKey (because Form2 hasn't done anything yet, except
appear).

Now, if it were Form2.ShowDialog instead, the caller would wait for Form2
to finish and SharedVegetables would be cooked nicely.

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Steve A. said:
I have a project that uses two forms. Form1 uses a SQL
Connection, several DataAdapters for various tables,
DataSet, and a DataView that I filter by user-selected
field names and associated criteria. Form2 has a couple
of read-only text fields to show the user the search
field and criteria they entered plus a DataGrid that is
populated from the DataView. There is a Public Class
defined to hold shared variables.

Code:
Public Class SharedVariables
Public Shared SelectedKey As String ' Make it shared
End Class

Code:
' Define an instance of the search form
Dim Form2 As New frmSearchResults

' Set the txtSearchingFor textbox and field name
to data from the calling form
Form2.txtSearchingFor.Text = txtSearch.Text
.
.
.
If cboSearch.SelectedIndex = 5 Then
myField = "Phone"
Form2.txtFieldName.Text = "Phone"
End If

dvDonors.Table = dsDataSet.Donors
dvDonors.RowFilter = myField & " LIKE" & "'" &
txtSearch.Text & "*" & "'"

Why not use 'Form2.txtSearch'?
Form2.grdGrid.DataSource = dvDonors

' Display the form.
Form2.Show()
txtSelectedKey.Text = SharedVariables.SelectedKey

Why not use 'Form2.txtSelectedKey'?
*** txtSelectedKey.Text after the first execution is Null.

All of this works fine up to this point - I can show the
second form with the filtered data.

Here is the malfunctioning code:

Private Sub grdGrid_DoubleClick(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
grdGrid.DoubleClick
SharedVariables.SelectedKey = grdGrid.Item
(grdGrid.CurrentCell)
End Sub

Where do you use this code? On your 'Form2' instance?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Dilbert's words of wisdom #18: Never argue with an idiot. They drag you down
to their level then beat you with experience.
 
S

Steve A.

-----Original Message-----


Why not use 'Form2.txtSearch'?
SharedVariables.SelectedKey

Why not use 'Form2.txtSelectedKey'?

txtSelectedKey is defined on Form1 as a TextBox - It's
there temporarily so I could see if Form2 was setting the
shared variable SelectedKey properly.
Where do you use this code? On your 'Form2' instance?

Yes. The grdGrid.DoubleClick event is on Form2.

Thank you for your reply :)
 
S

Steve A.

-----Original Message-----
Hi Steve,

Form2.Show()
txtSelectedKey.Text = SharedVariables.SelectedKey

Show goes of to Form2 and Shows it, leaves it there to do its thing and
comes back <immediately>. Then it sets txtSelected to the existing value of
SharedVariables.SelectedKey (because Form2 hasn't done anything yet, except
appear).

Now, if it were Form2.ShowDialog instead, the caller would wait for Form2
to finish and SharedVegetables would be cooked nicely.

That worked perfectly. Thank You! The idea is to have
the user select the record they want and to repopulate
Form1 so they can edit it. Sorry to be such a doofuss
(your fix was a simple one) but, VB.Net is such a rich
language it will take me a while to assimilate it all.
Thanks again.
 

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