?
Solved

Form not returning .DialogResult.OK

Posted on 2013-01-24
12
Medium Priority
?
509 Views
Last Modified: 2013-01-26
Hi,

This statement doesn't seem to be set to .OK to set the variable:

 If frmPickStore.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
         intSourceStore = CInt(frmPickStore.txtStore.Text)
 End If

the frmPickStore looks like this...

Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Security
Imports PRIDE
Imports PRIDE.Data
Imports PRIDE.Entities

Public Class frmPickStore
    Inherits System.Windows.Forms.Form

    Private Sub frmPickStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dsStores As New TList(Of PrsStores)
        dsStores = DataRepository.PrsStoresProvider.Find("InterStore = 'Y'")
        dsStores.Sort("StoreName")

        clbStores.DataSource = dsStores
        clbStores.ValueMember = "StoreID"
        clbStores.DisplayMember = "StoreName"

    End Sub

    Private Sub clbStores_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles clbStores.DoubleClick
      txtStore.Text = clbStores.SelectedValue.ToString
      Me.DialogResult = DialogResult.OK
    End Sub
End Class
0
Comment
Question by:bigtwig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38815966
Try using an actual instance of the form instead of the default instance:
Dim pick As New frmPickStore
If pick.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
    intSourceStore = CInt(pick.txtStore.Text)
End If

Open in new window


Notice "pick" being used in lines #2 and #3.
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 1300 total points
ID: 38815974
I can offer an alternative.

Create a property in frmPickStore

 ReadOnly Property Cancelled() As Boolean
        Get
            Return _Cancelled
        End Get
    End Property

form level variable:
dim _Cancelled as boolean

Within your existing clbStores_DoubleClick, replace me.dialogresult... with _Cancelled = True

Back on your calling form, change it to this:
 If frmPickStore.Cancelled Then
         intSourceStore = CInt(frmPickStore.txtStore.Text)
 End If
0
 
LVL 40
ID: 38815980
How do you open the form?

In order for this mechanism to work, the form should be displayed with ShowDialog, not with Show.

Also, have you checked with a breakpoint to make sure that the line is called? Does the form hides itself when you set the value of DialogResult. I am not sure what a clb is for you, but in older versions of the framework, there was a bug where the DoubleClick was never called on a ComboBox.
0
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
LVL 18

Assisted Solution

by:UnifiedIS
UnifiedIS earned 1300 total points
ID: 38815981
Sorry should be
If NOT frmPickStore.Cancelled then
 intSourceStore = CInt(frmPickStore.txtStore.Text)
 End If
0
 
LVL 2

Author Comment

by:bigtwig
ID: 38816067
@JamesBurger: Form gets called when another field loses focus:
frmMain.TxtNote_LostFocus()
I'm using ShowDialog and not Show


@UnifiedIS:  If I take out the .ShowDialog(), then the form won't open, correct?
0
 
LVL 2

Author Comment

by:bigtwig
ID: 38816076
@Idle_Mind: pick is also coming back as not being OK. It does the .ShowDialog and when it returns it doesn't go to the intSourceStore=.... line.
0
 
LVL 2

Author Comment

by:bigtwig
ID: 38816102
The IF statement is within the frmMain.txtNote.LostFocus() subroutine.
In old versions of the code, the .ShowDialog opened the frmPickStore form, I selected a line on the form with a double-click and intSourceStore was set to the value I selected.

Now, the .ShowDialog opens the frmPickStore form, I select a line on the form with a double-click, the two lines in the _DoubleClick event are executed, but control is returned to the beginning of the _LostFocus subroutine and not continuing with the IF statement.
0
 
LVL 18

Assisted Solution

by:UnifiedIS
UnifiedIS earned 1300 total points
ID: 38816125
You will still need to call frmPickStore.ShowDialog.  

 frmPickStore.ShowDialog
'then your check of the cancelled property
 If frmPickStore.Cancelled Then
         intSourceStore = CInt(frmPickStore.txtStore.Text)
 End If
'then dispose the object
frmPickStore.Dispose
0
 
LVL 18

Assisted Solution

by:UnifiedIS
UnifiedIS earned 1300 total points
ID: 38816127
Shoot, did it again

 frmPickStore.ShowDialog
'then your check of the cancelled property
 If NOT frmPickStore.Cancelled Then
         intSourceStore = CInt(frmPickStore.txtStore.Text)
 End If
'then dispose the object
frmPickStore.Dispose
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 38816171
I have encountered that strange behaviour a few times. Since we have no control on the way events are handled, it is often impossible to understand the problem. Usually, in these situations, simply rearranging the code in a different way can correc the problem.

For instance, many pieces of code can be written two ways, such as the following:

     rep = MyForm.ShowDialog()
     If rep = DialogResult.OK Then...

of

     If MyForm.ShowDialog() Then...

Sometimes one will work as you intend and correct the bad callback into the event, while the other one will give you a problem such as yours.

The problem of the event that seems to be called twice can also be corrected with a static variable, something like the following (sometimes the reverse):

     Static flag As Boolean
     Static yourDialog As New YourForm

     If flag Then
          'Second pass through the event, you are coming back from the dialog
          flag = False  'Reset for next time
          If yourDialog.DialogResult = DialogResult.OK Then...
          yourDialog = Nothing 'Unless you want to keep the instance available
    Else
          'First pass, show the dialog
          flag = True  'Set for callback
          YourForm.ShowDialog
     End If
0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 300 total points
ID: 38821688
How do you close dialog? Probably it should be
Private Sub clbStores_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles clbStores.DoubleClick
      txtStore.Text = clbStores.SelectedValue.ToString
      Me.DialogResult = DialogResult.OK
      Me.Close()
End Sub
Check out Form_Closing event for dialog- may be it resets DialogResult?
0
 
LVL 2

Author Closing Comment

by:bigtwig
ID: 38822184
Thanks for the help! I did a little mix and match from everyone.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question