?
Solved

Form not returning .DialogResult.OK

Posted on 2013-01-24
12
Medium Priority
?
513 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
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

809 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