dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Close messagebox

  1. #1

    Thread Starter
    Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    34

    Resolved [RESOLVED] Close messagebox

    I have a message box that pops up when an item is selected.
    If I click No the message box closes.
    If I click Yes it carries out the instruction but closes and opens again if I click yes again it closes.
    Code below:
    Code:
        Private Sub LstMat1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LstMat1.SelectedIndexChanged
            ' remove name from LstMat1
            Dim delete As String
            If LstMat1.Items.Count > 0 Then
                delete = MessageBox.Show("Delete" & vbCrLf & LstMat1.SelectedItem & "?",
                    "Important Note",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1)
                If delete = vbYes Then
                    LstMat1.Items.Remove(LstMat1.SelectedItem)
                End If
            End If
        End Sub
    Learning is a never ending subject.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,847

    Re: Close messagebox

    It's because of where your code is located. The SelectedIndexChanged event is triggered a second time when you "Remove" the current item.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,349

    Re: Close messagebox

    If you click Yes, the SelectedItem is removed... but as your code is inside the SelectedIndexChanged event, the event will fire again (because the SelectedIndex has Changed).

    I think you should alter your If statement to also check the value of SelectedIndex (or SelectedItem).

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,065

    Re: Close messagebox

    You could use a flag to stop this behavior.

    Code:
        Private Sub LstMat1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LstMat1.SelectedIndexChanged
            ' remove name from LstMat1
            Static recur As Boolean = False
            If recur Then Exit Sub
            Dim delete As String
            If LstMat1.Items.Count > 0 Then
                delete = MessageBox.Show("Delete" & vbCrLf & LstMat1.SelectedItem & "?",
                    "Important Note",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1)
                If delete = vbYes Then
                    recur = True
                    LstMat1.Items.Remove(LstMat1.SelectedItem)
                    recur = False
                End If
            End If
        End Sub
    Static
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    34

    Re: Close messagebox

    Thanks everybody for the help and advice.
    I like your solution dbasnett of using a Boolean flag and will no doubt use it in future projects.
    At the moment I just placed the code in the click event and it works fine.

    Si,
    You wrote you would check the value of the selected value, I don't understand why.
    I show the selected value in the message box in case the user has clicked the wrong name but I dont think that's why your suggesting it.
    Learning is a never ending subject.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,349

    Re: Close messagebox

    The second time you come into the routine, the value(s) should be different.

    I suspect (but haven't checked) that the second time it runs the value of SelectedIndex will be -1 (which means no item selected).


    However, the flag solution is a bit clearer, so I would recommend that instead.

  7. #7

    Thread Starter
    Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    34

    Re: Close messagebox

    Once again thanks but why not leave it in the list box click event? or is the flag in the index changed event better?
    or does it not make any real difference?
    Learning is a never ending subject.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,349

    Re: Close messagebox

    Using the Click event is fine, and is a bit better because it means the code is slightly simpler (because you don't need a flag).

  9. #9

    Thread Starter
    Member Mallard8's Avatar
    Join Date
    Feb 2018
    Location
    Wales
    Posts
    34

    Re: Close messagebox

    Thank you, can't leave a rep I need to spread the love around!
    Learning is a never ending subject.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width