By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Annoying Windows Forms Problem

P: n/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?
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
* "Steve A." <se*******@hotmail.com> scripsit:
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.
Nov 20 '05 #3

P: n/a
-----Original Message-----
* "Steve A." <se*******@hotmail.com> scripsit:
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 is defined on Form1 as a TextBox - It's
there temporarily so I could see if Form2 was setting the
shared variable SelectedKey properly.
*** 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?


Yes. The grdGrid.DoubleClick event is on Form2.

Thank you for your reply :)

--
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 downto their level then beat you with experience.
.

Nov 20 '05 #4

P: n/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 andcomes back <immediately>. Then it sets txtSelected to the existing value ofSharedVariables.SelectedKey (because Form2 hasn't done anything yet, exceptappear).

Now, if it were Form2.ShowDialog instead, the caller would wait for Form2to 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.
Regards,
Fergus
.

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.