Solved

Form not returning .DialogResult.OK

Posted on 2013-01-24
12
502 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 85

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 325 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
 
LVL 18

Assisted Solution

by:UnifiedIS
UnifiedIS earned 325 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 325 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 325 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 100 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 27

Assisted Solution

by:Ark
Ark earned 75 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 43
Need help on C# Linq query on object 6 53
How do ASP.NET and MVC work together? 4 32
ASP.NET 5 Templates 2 68
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now