Results 1 to 13 of 13

Thread: [RESOLVED] Command line, Me.Close() skipped

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    597

    Resolved [RESOLVED] Command line, Me.Close() skipped

    I am having quite a time figuring out what is going on with the behavior of the form shown below.

    The form, is broken down into several containers, Change Request, Change Content, and Change Status. The first two containers are for user inputs to the record.

    The textboxes in the Change Status container are all formatted for short dates. The first is self-generated and never editable, while the rest of them have been set to auto-input the current date by just clicking in the textbox.

    Name:  ChangeForm.jpg
Views: 169
Size:  39.2 KB

    Name:  ChangeForm2.jpg
Views: 163
Size:  39.8 KB

    At this point the user might click the Save button. Note that all of the available inputs have not been entered. The code below, in the Case Else branch, defines the conditions that the record will not be saved. Under this condition, if the date submitted has been filled and any of the inputs remain blank, the method will clear the Date Submit and allow the user the chance to fill in the form, or abandon the effort by clicking the Cancel button.

    What occurs is that none of the inputs are editable and if the user clicks the cancel button the form does execute Me.Close(). However, everything after the Me.Close() is executed. What one is left with is the form is still open, as is the form that the method called out.

    Should the user choose to first click the Date Submitted textbox, the date fills in and the user is now allowed to edit the form inputs and clicking the Cancel button will now execute completely (the record is not updated). Or the user can click the Save button and update the record.

    While I suspect that there is some relation between the validation method and the apparent skip of the Me.Close() command, I have no idea what that might be. Not only that, but my suspicions are usually far from what really occurs and I don't really have a grasp of what/how validation works.

    What I want is two things. The first is the user to be able to complete the form correctly and then save their edits (assuming the edits meet the criteria). It does this. Or to be able to just click the Cancel Button and close the form without saving. It does not do this.

    Code:
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            Select Case MyState
    
    
    #Region "Case Else"
                Case Else
                    If Not String.IsNullOrWhiteSpace(txtSubmit.Text) And (String.IsNullOrWhiteSpace(txtName.Text) Or
                        Not String.IsNullOrWhiteSpace(cboManager.Text) Or Not String.IsNullOrWhiteSpace(cboOwner.Text) Or
                        Not String.IsNullOrWhiteSpace(txtMade.Text) Or Not String.IsNullOrWhiteSpace(txtResult.Text)) Then
                        MessageBox.Show("All information must be filled out in the form.", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                        txtSubmit.Text = ""
                        txtSubmit.ReadOnly = False
                        'txtApprove.ReadOnly = True
                    Else
                        If Not String.IsNullOrWhiteSpace(txtEffective.Text) And chkObsolete.Checked = False Then
                            RecEffective.MakeEffective(lblChangeType.Text, lblArticleID.Text)
                            chkActive.Checked = False
                        End If
                        UpdateRecord()
                        MessageBox.Show("Record saved.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Me.Close()
                        chgList.Show()
                    End If
    #End Region
            End Select
        End Sub
    Code:
        Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click, btnCancel.Click
            Me.Close()
            Select Case MyUser
                Case "DocMaster"
                    If MySystem = "Article" Then docList.Show() Else chgList.Show()
                Case "PartMaster"
                    If MySystem = "Article" Then partList.Show() Else chgList.Show()
                Case "AssyMaster"
                    If MySystem = "Article" Then fabList.Show() Else chgList.Show()
                Case "ValMaster"
                    If MySystem = "Article" Then valList.Show() Else chgList.Show()
                Case "DevMaster"
                    If MySystem = "Article" Then devList.Show() Else chgList.Show()
            End Select
        End Sub
    Code:
        Private Sub txtSubmit_Click(sender As Object, e As EventArgs) Handles txtSubmit.Click
            If String.IsNullOrWhiteSpace(txtSubmit.Text) And txtSubmit.ReadOnly = False Then txtSubmit.Text = Today.ToShortDateString Else MsgBox("This input cannot be changed.")
        End Sub
    Code:
        Private Sub txtSubmit_Validating(sender As Object, e As CancelEventArgs) Handles txtSubmit.Validating
            If String.IsNullOrWhiteSpace(txtSubmit.Text) Then Exit Sub
            If Not IsDate(txtSubmit.Text) Or CDate(txtSubmit.Text) <> Today Then
                MsgBox("Please enter a valid current date.", vbCritical)
                txtSubmit.SelectionStart = 0
                txtSubmit.SelectionLength = Len(txtSubmit.Text)
                txtSubmit.Focus()
            End If
        End Sub

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,916

    Re: Command line, Me.Close() skipped

    What I want is two things. The first is the user to be able to complete the form correctly and then save their edits (assuming the edits meet the criteria). It does this. Or to be able to just click the Cancel Button and close the form without saving. It does not do this.
    If you want to do something when they select "Cancel" then you have to check for that.

    Code:
            If MessageBox.Show("Retry", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.Cancel Then
                Me.Close()
            End If

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,556

    Re: Command line, Me.Close() skipped

    I'm confused about what is actually happening. You said that Me.Close is executed on Cancel, but that everything after it is also executed. However, you showed the Save button, not the Cancel button. Is the Cancel button working correctly? Is this issue with the Save button?

    Ah, I see that the close button is also the Cancel button. It seems to be doing what you told it to do. Me.Close is not the same as Return, nor do you appear to want it to be.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    597

    Re: Command line, Me.Close() skipped

    I too am confused. And I am the one who put this together. I am beginning to see that my issue might well be with the txtSubmit_Click() event (see above). This issue only occurs if I skip filling in the txtSubmit textbox.

    Let's just take a look at a rough depiction of the process

    Name:  flowchart.jpg
Views: 57
Size:  15.0 KB

    btnClose and btnCancel are currently both handled in the same event method BTW.

    Actually, as I re-re-review the code and look at the process, it appears to me that I have set the process up such that there must be an input to the txtSubmit txtbox. This probably has something to do with the fact that I have the txtSubmit textbox set such that if one clicks the textbox, the Today short date is auto generated.

    Having said all of that, where would the event method suggested by Wes go?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    597

    Re: Command line, Me.Close() skipped

    I just worked through it all again and it comes down to taking out the line of code, in the btnSave_Click() Event, txtSubmit = "". I had thought that the form would be better if I cleared that textbox when the input validation failed. Obviously, I was wrong.

    Having said all of that, I still am not clear on why it all goes south if the txtSubmit textbox is cleared. I have noticed that none of the outputs are editable, and of course the cancel button does not execute the Me.Close() line of the btnClose/btnCancel event.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,916

    Re: Command line, Me.Close() skipped

    Having said all of that, where would the event method suggested by Wes go?
    That would be in the btnsave_click event. If they select Cancel then it would exit without updating anything.

    I didn't notice that the Close and Cancel button use the same Click event. Not sure why you do that, a person might want to cancel the current changes but not close the form. But maybe that's not a possibility.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    597

    Re: Command line, Me.Close() skipped

    got it! So your code then sets up for if the cancel button is clicked, instead of making the edits and refilling txtSubmit?

    Thanks. In retrospect, I really am not sure why I even thought it was a good idea to clear the txtSubmit textbox, since if one is going to cancel without saving, it doesn't matter if the txtSubmit textbox is clear or not.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,916

    Re: Command line, Me.Close() skipped

    Quote Originally Posted by gwboolean View Post
    got it! So your code then sets up for if the cancel button is clicked, instead of making the edits and refilling txtSubmit?

    Thanks. In retrospect, I really am not sure why I even thought it was a good idea to clear the txtSubmit textbox, since if one is going to cancel without saving, it doesn't matter if the txtSubmit textbox is clear or not.
    Really, it was just an example of how to use the MessageBox Cancel button to close the form. Your giving me to much credit. lol

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    597

    Re: Command line, Me.Close() skipped

    To be honest, I don't believe your change will work either. I believe the problem lies in the validation method for the textbox (see above). I haven't figured it out yet, but I think that the criteria/parameters for that method are not correct to cover the cleared textbox.

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,916

    Re: [RESOLVED] Command line, Me.Close() skipped

    Code:
    If Not IsDate(txtSubmit.Text) Or CDate(txtSubmit.Text) <> Today
    Do you realize that "Today" returns a date and time?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    597

    Re: [RESOLVED] Command line, Me.Close() skipped

    Quote Originally Posted by wes4dbt View Post
    Code:
    If Not IsDate(txtSubmit.Text) Or CDate(txtSubmit.Text) <> Today
    Do you realize that "Today" returns a date and time?

    I do. Is that causing me a problem? Personally, I would prefer to have just the date, since I remember the time element has caused me problems in the past. Is it easy for me to eliminate the time element?

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,838

    Re: [RESOLVED] Command line, Me.Close() skipped

    Quote Originally Posted by wes4dbt View Post
    Code:
    If Not IsDate(txtSubmit.Text) Or CDate(txtSubmit.Text) <> Today
    Do you realize that "Today" returns a date and time?
    It does, but the time part is zero. Today is equivalent to Now.Date. In fact, it's probably implemented that way because DateTime.Today is implemented as DateTime.Now.Date.

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,916

    Re: [RESOLVED] Command line, Me.Close() skipped

    Quote Originally Posted by jmcilhinney View Post
    It does, but the time part is zero. Today is equivalent to Now.Date. In fact, it's probably implemented that way because DateTime.Today is implemented as DateTime.Now.Date.
    Thanks, I didn't know "CDate("8/20/2022")=Today" would evaluate to True. To be honest I haven't used Cdate in years. forgot it also has a time of 12:00:00 AM.

Tags for this Thread

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