Solved

ComboBox value not saved when tabbing into the field

Posted on 2011-02-21
8
372 Views
Last Modified: 2012-05-11
I have a Windows VB.NET form that is bound to a table in my database.  The form has text fields and combo box fields.  I am having a very interesting problem and can't figure out how to fix it.  When the form is displayed, if I use the tab key to enter the combo box field; then use the down arrow key to change the value in the combo box; then tab to the next field; then click on the Save button, the value in the combo box reverts back to the original value.  When I change it, it changes on the screen but as soon as I click on the Save button it reverts back to the orginal value.  In my Save Function I call EndEdit() on the BindingSource and that is when the value reverts back to the original value.

However, if I click the combo box with my mouse; then use the down arrow key to change the value of the combo box; then click on the Save button, the new value is kept and saved.  Calling EndEdit on the Binding Source doesn't change the value.

I can't figure out what is going on.  I'm including the two functions that I think come into play with this.

Any help is greatly appreciated.
========= This function is triggered whenever the combobox value is changed ====================
    Private Sub EmployeeStatusComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeStatusComboBox.SelectedIndexChanged
        ' This function is called even when the window is initially loading, therfore
        ' skip processing a status change since it was handled in the Load Event
        Dim NewStatus As String

        If Not InitialLoadFlag Then

            NewStatus = StrConv(EmployeeStatusComboBox.Text, VbStrConv.Uppercase)
            ' Check if Employement Status has changed
            If NewStatus <> OriginalEmployeeStatus Then
                Select Case NewStatus
                    Case "ACTIVE"
                        'set the items in the list accordingly
                        With PlanStatusComboBox.Items
                            .Clear() 'clear the current options
                            .Add("FULL TIME")
                            .Add("PART TIME")
                        End With

                        'select full time
                        EmployeeDrow.EmployeeStatus = "ACTIVE"
                        EmployeeDrow.PlanStatus = "FULL TIME"
                        If Not PlanStatusComboBox.Enabled Then
                            PlanStatusComboBox.Enabled = True
                        End If
                        PlanStatusComboBox.Text = "FULL TIME"
                        PlanStatusComboBox.Refresh()

                    Case "TERMINATED"
                        'set the items in the list accordingly
                        With PlanStatusComboBox.Items()
                            .Clear() 'clear the current options
                            .Add("NON-VESTED")
                            .Add("VESTED") 'left for some reason
                        End With
                        EmployeeDrow.EmployeeStatus = "TERMINATED"
                        EmployeeDrow.PlanStatus = "NON-VESTED"
                        If Not PlanStatusComboBox.Enabled Then
                            PlanStatusComboBox.Enabled = True
                        End If
                        PlanStatusComboBox.Text = "NON-VESTED"
                        PlanStatusComboBox.Refresh()
                    Case "NON-BARGAINING"
                        'set the items in the list accordingly
                        With PlanStatusComboBox.Items()
                            .Clear() 'clear the current options
                            .Add("NON-VESTED")
                            .Add("VESTED")
                        End With
                        EmployeeDrow.EmployeeStatus = "NON-BARGAINING"
                        EmployeeDrow.PlanStatus = "NON-VESTED"
                        If Not PlanStatusComboBox.Enabled Then
                            PlanStatusComboBox.Enabled = True
                        End If
                        PlanStatusComboBox.Text = "NON-VESTED"
                        PlanStatusComboBox.Refresh()
                End Select
            End If
            EmployeeStatusComboBox.Refresh()
        Else
            InitialLoadFlag = False
        End If
    End Sub

============  This is the Save function =========================
    Private Function SaveEmployeeData() As Boolean
        Dim success As Integer
        Dim ec As New EmployeeClass
        Dim rmBirthDate As Boolean = False
        Dim rmTermDate As Boolean = False
        Dim rmDBDate As Boolean = False
        Dim rmBFRDate As Boolean = False
        Dim rmAVDate As Boolean = False

        ' Validate Employee Data
        If Not ValidateEmployeeRecord() Then
            Return False
        End If

        ' Save information
        Try
            Me.EmployeesBindingSource.EndEdit()
            If EmployeeDrow.RowState = DataRowState.Modified Or EmployeeDrow.RowState = DataRowState.Added Then
                ' Convert Text fields to uppercase
                EmployeeDrow.Address = RemoveCrLf(UCase(EmployeeDrow.Address))
                EmployeeDrow.City = UCase(EmployeeDrow.City)
                EmployeeDrow.EmployeeStatus = UCase(EmployeeDrow.EmployeeStatus)
                EmployeeDrow.EmployeeType = UCase(EmployeeDrow.EmployeeType)
                EmployeeDrow.FirstName = UCase(EmployeeDrow.FirstName)
                EmployeeDrow.LastName = UCase(EmployeeDrow.LastName)
                EmployeeDrow.MaritalStatus = UCase(EmployeeDrow.MaritalStatus)
                EmployeeDrow.Notes = UCase(EmployeeDrow.Notes)
                EmployeeDrow.Sex = UCase(EmployeeDrow.Sex)
                EmployeeDrow.State = UCase(EmployeeDrow.State)
                ' Remove DB Amount if termination date entered for first time
                If OriginalTermDate = "" Then
                    Try
                        If IsDate(EmployeeDrow.TerminationDate) Then
                            EmployeeDrow.LifeInsuranceFaceAmount = 0
                            LifeInsuranceFaceAmountTextBox.Text = ""
                        End If
                    Catch ex As Exception

                    End Try
                End If

                ' Check if dates need to be removed
                If BirthDateMaskedTextBox.Text = "" Then
                    rmBirthDate = True
                End If
                If TerminationDateTextBox.Text = "" Then
                    rmTermDate = True
                End If
                If EffectiveDateforLifeInsuranceTextBox.Text = "" Then
                    rmDBDate = True
                End If
                If BenefitFormsReceviedTextBox.Text = "" Then
                    rmBFRDate = True
                End If
                If AccountValuationTextBox.Text = "" Then
                    rmAVDate = True
                End If

                success = EmployeesTableAdapter.Update(PfmsDataSet1.Employees)
                If success = 0 Then
                    MsgBox("Employee Information Not Saved.")
                Else
                    ' Check if dates need to be removed
                    If rmBirthDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "DOB") Then
                            MsgBox("Could not remove Employee Birth Date.")
                        End If
                    End If
                    If rmTermDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "DOT") Then
                            MsgBox("Could not remove Employee Termination Date.")
                        End If
                    End If
                    If rmDBDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "DBD") Then
                            MsgBox("Could not remove Employee Death Benefit Date.")
                        End If
                    End If
                    If rmBFRDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "BFRD") Then
                            MsgBox("Could not remove Employee Benefit Forms Received Date.")
                        End If
                    End If
                    If rmAVDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "AVD") Then
                            MsgBox("Could not remove Employee Account Valuation Date.")
                        End If
                    End If

                    OriginalName = EmployeeDrow.FirstName + " " + EmployeeDrow.LastName
                    OriginalSSN = EmployeeDrow.SocialSecurityNumber
                    Try
                        OriginalTermDate = EmployeeDrow.TerminationDate
                    Catch ex As Exception
                        OriginalTermDate = ""
                    End Try
                    ' Store original employment status in case it changes
                    OriginalEmployeeStatus = StrConv(EmployeeDrow.EmployeeStatus, VbStrConv.Uppercase)
                    OriginalPlanStatus = StrConv(EmployeeDrow.PlanStatus, VbStrConv.Uppercase)

                    MsgBox("Employee Information Saved.")
                    Return True
                End If
            Else
                Return True
            End If
        Catch ex As Exception
            ' Check if duplicate SSN error
            If ex.Message.Contains("duplicate") Then
                MsgBox("SSN already exists.")
            Else
                If MsgBox("Error saving employee. Click No to Continue.  Click Yes to Display Error Details.", MsgBoxStyle.YesNo, "Error") = MsgBoxResult.Yes Then
                    MsgBox("Error: " + ex.ToString())
                End If
            End If
        End Try
        Return False
    End Function

Open in new window

0
Comment
Question by:dyarosh
8 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
Comment Utility
For the AutoPostback property of the control set it equal to true. That should do it.
0
 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
Change this line of code "If Not InitialLoadFlag Then" to one I posted below.
If Not InitialLoadFlag AndAlso Not EmployeeStatusComboBox.SelectedIndex < 0  Then

Open in new window

0
 

Author Comment

by:dyarosh
Comment Utility
rawinnlnx9:
I could not find the AutoPostBack property.  See attached screen shot.

PagodNaUtak:
I tried your code and it did not work. The value still reverts back to the original value if I use Tab. Screen Shot of Employment Status field
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 20

Expert Comment

by:ElrondCT
Comment Utility
Is this a web form (ASP.NET), or a Windows form? If it's a Windows form, that would explain why you don't have AutoPostBack--that's a web method.

Which version of Visual Studio/Visual Basic are you using? 2008 or 2010?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
>I have a Windows VB.NET form that is bound to a table in my database.
The question begins with above sentence!

The reason this is happening is that changing the value using arrows does not turn on the edit mode of the combobox hence the new selection is not registered. You can use the .Text property of the combobox to read the new selection, find the id of the selected text and use this id when saving. Also try calling the BeginEdit in GotFocus event of the combobox.
0
 

Author Closing Comment

by:dyarosh
Comment Utility
My apologies for taking so long to respond.  Thank you for your help.
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Glad to help :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

11 Experts available now in Live!

Get 1:1 Help Now