Solved

Form not returning .DialogResult.OK

Posted on 2013-01-24
12
508 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 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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
 
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 28

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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