5 Replies - 1937 Views - Last Post: 27 August 2013 - 06:45 AM Rate Topic: -----

#1 ridhwanr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 13

Combo box defaulting to first value only on the first entry

Posted 26 August 2013 - 01:03 PM

All,

I am having a strange issue where I have a combo box (5 of them)on my windows VB.NET form. All of them I have collection of items in design mode where the user can select from the list. I also have the first item on the list to be "Choose from the list" now My problem is this: Some of them work some don't for the first selection. When I first click "ADD NEW" button and I fill all the other text boxes and select from the list of the combo boxes, the first value on my combo box list is defaulted to "Choose from the list" which is the first item on the list. (2 out of the 5 combo boxes default to the first Item on the list and the other 3 shows the correctly chosen item on the list) and when I save and click "ADD NEW" button again and fill the text boxes and choose an item from the list for all the combo boxes and hit save they all work (and display the selected item) and I can see the selected Item. So the first entry defaults to " Choose from List" which is my first item on the combo box list for 2 out of the 5 combo boxes and the rest changed to the user selected item.

HERE is a snapshot of my CODE: as you can see on my description of the problem I am new to VB.NET.

BTW, I am using strongly typed dataset. (basically drag and drop method)


 
 'Add new employee to DB 
    Private Sub AddNewBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewBtn.Click
        'Clear Previous data on form. i.e if the user entered text before clicking on ADD Button. 
        For Each ctrl As Control In Me.Controls
            Try
                If TypeOf (ctrl) Is CheckBox Then DirectCast(ctrl, CheckBox).Checked = False
                If TypeOf (ctrl) Is TextBox Then DirectCast(ctrl, TextBox).Clear()

                'clear combobox previous values
                If EmpTypeComboBox.Items.Count > 0 Then
                    EmpTypeComboBox.SelectedIndex = 0
                    ' EmpTypeComboBox.Text = " "

                    'Else
                    '    EmpTypeComboBox.SelectedItem = EmpTypeComboBox.SelectedValue
                End If
                If EmpJobTitleComboBox.Items.Count > 0 Then
                    EmpJobTitleComboBox.SelectedIndex = 0
                End If
                If EmpCityComboBox.Items.Count > 0 Then
                    EmpCityComboBox.SelectedIndex = 0
                    ' EmpCityComboBox.Text = " "

                End If
                If EmpStateComboBox.Items.Count > 0 Then
                    EmpStateComboBox.SelectedIndex = 0

                End If

                If EmpEducationLevelComboBox.Items.Count > 0 Then
                    EmpEducationLevelComboBox.SelectedIndex = 0
                End If

            Catch Exception As Exception
                MsgBox(Exception.Message & "Please click ADD NEW button to add new Employee. If error occurs after you click on New Button, re-start the program and perform operation again if necessary close program and re-start your computer", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Human Error")
            End Try
        Next
        'Add new Employee to database
        SaveBtn.Enabled = True
        Try
            Me.EmployeeTblBindingSource.AddNew()
            EmpTypeComboBox.Focus()
        Catch Exception As Exception
            MsgBox(Exception.Message & "Database connections lost. Please re-start your program ")
        End Try
        HelpLabel.Text = "All fields are required to be completed EXCEPT Social Security field and Manager information" & vbNewLine & "The Social Security Field can be used to enter employee driving license number if desired for identification."

        'set employee hired date to todays date
        EmpDateOfHireDateTimePicker.Value = DateTime.Now

    End Sub



    Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click

        'Validations for text boxes --Check for (.) and (-) and null enteries 

        If EmpTypeComboBox.SelectedIndex = 0 Then
            MsgBox("Please choose Employee type from dropdown list", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Human Error")
            EmpTypeComboBox.Focus()
            Exit Sub
        End If

        If  EmpJobTitleComboBox.SelectedIndex = 0 Then
            MsgBox("Please choose the Job title of the Employee you want to register from the dropdown list. If Employee job title is not show in the dropdown list, choose Other.", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Human Error")
            EmpJobTitleComboBox.Focus()
            Exit Sub
        End If

  Try
            ''Save the Employee entered to database
            Me.Validate()
            Me.EmployeeTblBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DugsiDBDataSet)
            MsgBox("Employee registration Recorded", MsgBoxStyle.OkOnly, "Update/Save Successfull")
            Me.DugsiDBDataSet.Dispose()
            Me.TableAdapterManager.Dispose()

            Me.Refresh()
            'Me.Close()
        Catch Exception As Exception
            MessageBox.Show(Exception.Message & "You have either not entered the required fields of the Employee you want to register or the database connection has been lost. If you are sure you've entered all of the require fields of the Employee you want to register and you seeing this error, please close the program and re-start the program, If the problem still exist, reboot your computer and if you are still getting the error please contact your system admin.")
        End Try




Thank you very much for your help iin advance

Is This A Good Question/Topic? 0
  • +

Replies To: Combo box defaulting to first value only on the first entry

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2879
  • View blog
  • Posts: 9,552
  • Joined: 12-December 12

Re: Combo box defaulting to first value only on the first entry

Posted 26 August 2013 - 01:55 PM

I've read this a couple of times but am still confused. What exactly is the problem? Is it with the Add or Save button..?

If there is a problem with 2 out of 5 comboboxes then, logically, examine the code and properties for them to discover how they differ.

Anyway, do you mean to check Items.Count, which is the full list of items? I'm just checking my understanding of your code.

BTW Telling someone to re-boot there computer, and suggesting that it is a "Human Error" is.. not cricket.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2879
  • View blog
  • Posts: 9,552
  • Joined: 12-December 12

Re: Combo box defaulting to first value only on the first entry

Posted 26 August 2013 - 02:01 PM

BTWW I trust that you intend to reduce the number, and length, of your error messages.

User Input Validation in Windows Forms :MSDN
Was This Post Helpful? 0
  • +
  • -

#4 ridhwanr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 13

Re: Combo box defaulting to first value only on the first entry

Posted 26 August 2013 - 02:37 PM

andrewsw Thanks for the prompt reply. First I learned something from you today and I really appreciate that which is the Error messages.
Secondly, Here is my issue: 2 out of my 5 combo boxes are defaulting to the first item on the combo box List after "Save" button is clicked...and this is just for the first entry. after the first entry and the user clicks "Add New" button again the combo boxes works as it suppose to work. This is strange as itself that I checked all combo boxes and they all have same properties. I am using
  'clear combobox previous values -  after "Add New Button is clicked

               If EmpTypeComboBox.Items.Count > 0 Then

                   EmpTypeComboBox.SelectedIndex = 0


to default the first item on the combo box list which is " Choose from the list" after every time the "Add New" button is clicked - this is to remind the user to select from the list as well as validation. What else am I doing wrong? Please help. Many thanks in advance

This post has been edited by andrewsw: 26 August 2013 - 03:13 PM
Reason for edit:: Remove quote and line numbers

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2879
  • View blog
  • Posts: 9,552
  • Joined: 12-December 12

Re: Combo box defaulting to first value only on the first entry

Posted 26 August 2013 - 03:23 PM

Still confused. Are you saying that it is only on first showing your form that the comboboxes have the wrong item selected? In which case you could use the Form's Load event to set their SelectedIndex = 0.

(I'm assuming that the comboboxes are not data-bound and therefore might be displaying the values of the first record in a table.)

There are settings that can make the comboboxes remember their last selected item between sessions. If this is the case then you could find and remove these settings.

Otherwise, there is a debugging tutorial linked in my signature that would enable you to step through your code.

This post has been edited by andrewsw: 26 August 2013 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 ridhwanr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-August 13

Re: Combo box defaulting to first value only on the first entry

Posted 27 August 2013 - 06:45 AM

andrewsw,
First, Thanks for your time. I am not sure why I can't communicate what my problem is, I will try again:

I have 5 Combo boxes, which I set the collection list.. (Design view). lets take 2 combo boxes as an example: the first combo box is called EMPTYPE and it has the following list:
1.Choose from the list
2.Fulltime
3.Part time
4.seasonal

The second combo box is called EMPTITLE: and it has the following list:
1.Choose from the list
2.Teacher
3.Assistant teacher
4.Tutor
5. seasonal
6.mentor
and more list....

Now if you look into my code above, Every time the user clicks "ADD NEW" Button I am resetting the combo box values to the first item on the list which is: [Select from the list]. .selectIndex=0

Remember all of my combo boxes properties are identical. I am using drag and drop dataset method on my form.

Now, When the form loads and the user clicks "ADD NEW" button all combo boxes defaults to the first item on the list which is "Choose from the List" --So far so good. and fill out the text boxes (names, addresses etc) and select the first combo box (for this example) to "Full time" and the second combo box to "Teacher" and click save.
Next when the user clicks "ADD NEW" Button again (same session) and fill out the text boxes and again choose "Part time" for the first combo box in this example and "Tutor" for the second combo box and click "SAVE".

Now when I close the window and go to the 2nd form called "EMPLOYEE OVER VIEW" which I am using again drag and drop method dataset - now for the first ENTRY all the textboxes are filled correctly but for the first combo box which should have" Fulltime" data has "choose from the list" (basically defaulted to the first item on the list) the second combo box which I choose "Teacher" has the correct data entry (Teacher).

For the second entry (same session) which I choose "Part time" for the first combo box and "Tutor" for the second combo box - both are showing the correct values (Part time, Tutor)

Now my question is, what is making the first entry, first combo box to default to the first item on the list and why does the second combo box has the correct data entry which is "teacher"?
Sometimes when I select the second entry and go back to the first entry --the first entry gets the second entry data. crazy huh!

for my "Employee Overview" form, I have a list box that enables me to choose the first name (set by value member) of the employee I want to view his/her records. I also tried this in gridview and same result.

Sorry for the long explanation -- just hope that I explained my problem this time very clearly!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1