Results 1 to 11 of 11

Thread: [RESOLVED] .ShowDialog() causing annoyances

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [RESOLVED] .ShowDialog() causing annoyances

    I'm making a program and it requires a Form to show that requires an action before the user can go back to the main Form. Of course to do this I am using .ShowDialog(), but unfortunately I have found out that ShowDialog() has some annoying querks.

    Firstly, any time I pressed a button it would exit the entire application. After loads of googling I finally found out that you have to use

    Code:
    Me.DialogResult = None
    At the end of each button click in order to keep the Form from closing.

    But secondly, anything that I do to the Form is saved! So for instance I have a listbox that I populate with some code during the Form's Load Event. Well after I close it, if I bring the Form back up the items in the ListBox quadruples! I don't know why it's times 4 but I know that if I clear the items on each load it stops this. So whenever I use .ShowDialog() stuff is leftover on the form even after I .Close() it.

    While I can find impractical ways around it, such as the Me.DialogResult and resetting all the Forms properties on load, I would like to know if there is anything I can do to prevent those two problems, any work around or other options.

    Also I have tried just plain Show and everything works perfectly, it's just that then the user can switch to the main form with the second window still open, which is why I was using .ShowDialog in the first place.

    code
    At first I had this
    Code:
    QuickLoadForm.ShowDialog()
    Then I looked at the MSDN Library and changed it to this
    Code:
            Dim instance As Form = QuickLoadForm
            Dim returnValue As DialogResult
            returnValue = instance.ShowDialog()
    I still have the problem however and I'm guessing that Modal forms are supposed to behave like that. But saving all the forms settings is really annoying and doesn't make any sense to me so if someone knows how to turn that off I'd appreciate it.

    EDIT: Well I should of finished reading the MSDN Article

    Code:
    Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application.
    I'm tried it out and putting Me.Dispose() on the Form Close event fixed "save form properties" problem


    However I still would like to know if there is an easy way to set the DialogResult so that you don't have to use:

    Code:
    Me.DialogResult = None
    at the end of every button click. I tried using it on the Form Load but it still exits on any click.
    Last edited by Vectris; May 26th, 2009 at 09:27 PM.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: .ShowDialog() causing annoyances

    You don't have to use Me.DialogResult=None after each button is clicked, I have shown many Dialogs via ShowDialog() and have never had this problem, the likely thing I can see (if created in the designer), is that the property "DialogResult" of your button (not the form) is set to "Cancel" in which anytime you click the button, it will close the form.

    If that is not the case, simply set the DialogResult value of your form you are showing to none, and only change it right as your OK button is clicked.

    If the problem persists I'd probably have to see more of your code to get a better understanding of what is going on.

    I don't actually use the designer anymore, I do everything in code in my Form1_Load, from creating buttons, to other forms, VS doesn't lag as much, and it's much simpler =)

    Kind Regards

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: .ShowDialog() causing annoyances

    There's absolutely no need to set the DialogResult property of the form to None because it is None to start with and it will always be None until you set it to some other value. If you're finding that you have to set it to None then it can only be because you're setting it to some other value beforehand. If you aren't doing it explicitly in code then it's presumably because you have set the DialogResult property of the Buttons. I'm guessing that you had one Button with it's DialogResult property set and then you copied that Button rather than adding new ones.

    Setting the DialogResult property of a Button means that clicking that Button will set the form's DialogResult property to that value. Note that, if you select a Button as the form's CancelButton, its DialogResult property will be set to Cancel by default. Selecting a Button as the form's AcceptButton does not affect the Button's DialogResult property.

    Unless you specifically want to use the same instance over and over, this is how you should show a dialogue:
    vb.net Code:
    1. Using dialogue As New SomeForm
    2.     If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
    3.         'Process the affirmative response.
    4.     End If
    5. End Using
    If the user's response can be values other than OK and Cancel then you might use a Select Case instead of an If.

    In the dialogue itself, if there's a Cancel button the it should be selected as the form's CancelButton and its DialogResult wil be set to Cancel by default. If there's an OK button and you will definitely close the form when it's clicked then you should set its DialogResult property to OK. If the form may or may not close, e.g. you need to validate data, then leave the OK button's DialogResult as None and then explicitly set the form's DialogResult property in the Click event handler if you want the form to close.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: .ShowDialog() causing annoyances

    Change the "DialogResult" of the button worked perfectly! Thanks Icyculyr.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  5. #5

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: .ShowDialog() causing annoyances

    Quote Originally Posted by jmcilhinney View Post
    If you're finding that you have to set it to None then it can only be because you're setting it to some other value beforehand. If you aren't doing it explicitly in code then it's presumably because you have set the DialogResult property of the Buttons. I'm guessing that you had one Button with it's DialogResult property set and then you copied that Button rather than adding new ones.

    Setting the DialogResult property of a Button means that clicking that Button will set the form's DialogResult property to that value. Note that, if you select a Button as the form's CancelButton, its DialogResult property will be set to Cancel by default. Selecting a Button as the form's AcceptButton does not affect the Button's DialogResult property.
    That's exactly what happened. I have a ccancelButton (2 cs to avoid conflic) and I did actually set it as the Form's CancelButton which changed the Dialog Result. Then I copied it to make another button and that was causing my problem.

    Thank You both.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: .ShowDialog() causing annoyances

    Quote Originally Posted by Icyculyr View Post
    I don't actually use the designer anymore, I do everything in code in my Form1_Load, from creating buttons, to other forms, VS doesn't lag as much, and it's much simpler
    Sounds a bit Amish to me. If you like to do that then more power to you but there's no way it's simpler. If I want OK and Cancel Buttons on my form I just double-click in the Toolbox twice, drag the Buttons until the snap lines position them automatically, edit the Text of each, select both and set the Anchor together and then select them as the form's AcceptButton and CancelButton. There's no way that it's simpler to write the code to do that by hand. For a start, you have to do some calculations to determine the Location of each Button when, in the designer, the snap lines will do it for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: .ShowDialog() causing annoyances

    Haha, in small apps it's easier to use the designer, however in large apps, where lots of content changes often, you'd have 100+ controls on the designer, I prefer code =)

    Kind Regards

  8. #8
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: .ShowDialog() causing annoyances

    Quote Originally Posted by Icyculyr View Post
    Haha, in small apps it's easier to use the designer, however in large apps, where lots of content changes often, you'd have 100+ controls on the designer, I prefer code =)

    Kind Regards
    I suppose if no-one else is going to debug it, then more power to you.

    100+ controls on a form sounds like a design issue rather than a 'large app'. I've found that once you get to around 50 odd controls, things are starting to get cumbersome. Usually, I'd break things down into functionality and make a single control for that function.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: .ShowDialog() causing annoyances

    that's what we've done.... grouped together functionality into user controls.... so the form itself typically only has a dozen controls, tops....
    Can make things annoying when you are trying to track down where certain functionality exists, but after a while, you learn how it's all put together.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: .ShowDialog() causing annoyances

    I see, but if your designing a large scale app, where many different components are required, that cannot be used for other things, stuff gets messy.. I don't know if there's a way to clean it up but I've never been able to.

    Kind Regards

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: .ShowDialog() causing annoyances

    Quote Originally Posted by Icyculyr View Post
    I see, but if your designing a large scale app, where many different components are required, that cannot be used for other things, stuff gets messy.. I don't know if there's a way to clean it up but I've never been able to.

    Kind Regards
    Large-sclae apps should generally have more forms, not more controls on the same number of forms. I think you probably need to spend more time on the design. It's very unlikely that you couldn't break that logic up into multiple forms or UserControls that could be created and destroyed as needed. The only thing I can think of offhand that would require so many controls would be something like a sound mixer, but who'd build that in VB.NET anyway?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width