locked
Refreshing the form after cancellation RRS feed

  • Question

  • In my windows form i have added a button to enter new record , and when that selected , the button text changes as cancel and the focus will move to first entry field . But if i click cancel without entering any data , the focus is not getting removed .and when i press next record then the focus is removed & i can press add new record button again. Kindly help me how to solve this .below is my code

        Private Sub AddnewButton_Click(sender As Object, e As EventArgs) Handles AddnewButton.Click
        'MsgBox("you preseed add new button")
        Try
            With AddnewButton
                If .Text = "Add New Record" Then
                    MSdiesBindingSource.AddNew()
                    .Text = "Cancel"
                Else
                    RefreshData()
                    .Text = "Add New Record"
                End If
    
            End With
            With Size_in_mgTextBox
                If (.CanSelect) Then
                    .Text = String.Empty
                    .Select()
                End If
            End With
    
            IsAddNewRecordInProgress = Convert.ToBoolean(AddnewButton.Text = "Cancel")
    
    
        Catch ex As Exception
            MsgBox("An error occured: " & ex.Message.ToString().ToString(),
                  MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Add new Record Failed")
        End Try
    End Sub
        Private Sub RefreshData()
        Try
            'Me.MSdiesBindingSource.Filter = Nothing
            MSdiesTableAdapter.Fill(Me.TrialdatabaseDataSet.MSdies)
            MSdiesBindingSource.RemoveFilter()
        Catch ex As Exception
            MsgBox("Refresh Data Error!")
        End Try
    
    
    End Sub

    Basically what i want is when i press [add new record] button , the form goes to edit mode , but when i hit the same button (which is now showing as cancel) it should reset (complete refresh) and allow me to come out of edit mode(exiting the focus from Entry field)


    Monday, July 20, 2020 11:27 AM

Answers

  • Thank you so much for your suggestion , is there any video similar to this task?can you please share me the link...

    Greetings,

    I don't have an exact match for what you are asking but do have something that should get you in the right direction.

    There are two projects, one a forms project and the other a class project. As the code is for learning, done for an article for Microsoft TechNet I kept things wide open. Pretty much everything done is in MainForm and for add/edit a CustomerForm is used.

    When adding or editing a new row is added if the user pressed the save button, for edits the same while if the user cancels an edit I use RejectChanges method on the DataRow.

    Key points are found in EditCurrentCustomer and BindingNavigatorAddNewItem_Click coupled with the code in customer form.

    Note the project is on basics of master-details but same logic applies for a single DataGridView to edit/add form. You don't need a DataGridView but this sample works with a DataGridView.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 20, 2020 12:55 PM

All replies

  • Hello,

    I would recommend using a modal form that when displayed is either in add or edit mode. You could have two buttons, one to save with DialogResult set to Ok and a cancel button with DialogResult set to cancel. In add mode if the Ok button is clicked back in the calling form add a new record, if they press cancel button invoke CancelEdit method of the BindingSource.

    Also, there is never a good reason to "refresh" anything unless you know something changed in the table outside of the application, I've never in 20+ years have done a "refresh".

    Now the above to you may be more work then you want to do, that is your choice as you want something simpler yet simple is not always better. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 20, 2020 11:53 AM
  • Thank you so much for your suggestion , is there any video similar to this task?can you please share me the link...
    Monday, July 20, 2020 12:08 PM
  • Thank you so much for your suggestion , is there any video similar to this task?can you please share me the link...

    Greetings,

    I don't have an exact match for what you are asking but do have something that should get you in the right direction.

    There are two projects, one a forms project and the other a class project. As the code is for learning, done for an article for Microsoft TechNet I kept things wide open. Pretty much everything done is in MainForm and for add/edit a CustomerForm is used.

    When adding or editing a new row is added if the user pressed the save button, for edits the same while if the user cancels an edit I use RejectChanges method on the DataRow.

    Key points are found in EditCurrentCustomer and BindingNavigatorAddNewItem_Click coupled with the code in customer form.

    Note the project is on basics of master-details but same logic applies for a single DataGridView to edit/add form. You don't need a DataGridView but this sample works with a DataGridView.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 20, 2020 12:55 PM
  • Private Sub RefreshData()
        Try
          TrialdatabaseDataSet.MSdies.Clear 
          MSdiesTableAdapter.Fill(TrialdatabaseDataSet.MSdies)
        Catch ex As Exception
            MsgBox("Refresh Data Error!")
        End Try
    End Sub


    Success
    Cor

    Monday, July 20, 2020 3:11 PM