Solved

Form not returning .DialogResult.OK

Posted on 2013-01-24
12
501 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

23 Experts available now in Live!

Get 1:1 Help Now