#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0

    Combo Box problem - how to populate data


    HI to all,

    I have a combo box. my problem is that, i have 4 textbox below

    here are the data from frmPilotShiftingSchedules.frm
    cboPilot
    txtScheduleNo
    txtMonth
    txtTimeFrom
    TxtTimeTo


    My problem is this:

    how can i automatically load data in textbox,for example, if you choose a name or a value in combo box cboPilot , the data will also appear to textbox? Thanks for the help..

    here is my code
    Private Sub LoadComboBoxes_DB()
    Dim dt As DataTable
    dt = CollectData("SELECT Pilot_ID,Pilot_Name + ' ' + Emp_No AS Pilot_Name FROM vw_Pilot ")
    If dt.Rows.Count = 0 Then Exit Sub
    Me.cboPilot.DataSource = Nothing
    Me.cboPilot.DataSource = dt
    Me.cboPilot.ValueMember = "Pilot_ID"
    Me.cboPilot.DisplayMember = "Pilot_Name"
    Me.cboPilot.SelectedIndex = -1
    End Sub

    here is the 2nd code

    Private Sub cboPilot_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPilot.SelectedValueChanged

    Dim dt As DataTable
    'If bLoad = True Then Exit Sub
    'dt = CollectData("select * from vw_Pilot_Scheduling where Pilot_ID = '" & Me.cboPilot.SelectedValue & "'")

    dt = CollectData("SELECT Pilot_ID,Pilot_Name + ' ' + Emp_No AS Pilot_Name FROM vw_Pilot_Shifting ORDER BY Pilot_Name ")
    'dt = CollectData("SELECT * from vw_Pilot_Shifting")

    If dt.Rows.Count > 0 Then
    Me.txtScheduleNo.Text = dt.Rows(0)("Schedule_No")
    Me.txtMonth.Text = dt.Rows(0)("Month_No")
    Me.txtTimeFrom.Text = dt.Rows(0)("Day_Time_From")
    Me.txtTimeTo.Text = dt.Rows(0)("Day_Time_To")
    End If
    End Sub
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    With a Combo Box, the Click event places the item selected from the list in the text box portion and collapses the list. Therefore I use the dblClick event to react to the contents. I also use the KeyPress event to call the Click event if an [Enter] (Chr$(13)) is detected. The normal process would then be to click a member of the list and then hit the [Enter] key. The user can also use the arrow keys to navigate up and down the list, and then just enter when the correct one is found. If the list is long, and you want to give the user the opportunity to narrow down the list with key strokes, you should load the Combo Box alphabetically sorted and make sure the first item in the list is invalid (I use No Match with a space in front of it).

    I trust this is what you meant.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by couttsj
    With a Combo Box, the Click event places the item selected from the list in the text box portion and collapses the list. Therefore I use the dblClick event to react to the contents. I also use the KeyPress event to call the Click event if an [Enter] (Chr$(13)) is detected. The normal process would then be to click a member of the list and then hit the [Enter] key. The user can also use the arrow keys to navigate up and down the list, and then just enter when the correct one is found. If the list is long, and you want to give the user the opportunity to narrow down the list with key strokes, you should load the Combo Box alphabetically sorted and make sure the first item in the list is invalid (I use No Match with a space in front of it).

    I trust this is what you meant.

    J.A. Coutts

    Okey sir, Opps., i did not understand fully. For your info, im just a newbie in vb2010., and i've not practiced programming for a very long period of time.
    can u give me a light example?? i am very sorry for disturbing you.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Originally Posted by xpasaway
    Okey sir, Opps., i did not understand fully. For your info, im just a newbie in vb2010., and i've not practiced programming for a very long period of time.
    can u give me a light example?? i am very sorry for disturbing you.
    You are using VB.Net and my experience is with VB6, so there may be some slight differences in application. But the basic operation of the Combo Box should be the same. The list contents of the Combo Box or the List Box is a single string with each element separated by a Null character (Chr$(0)). The Combo Box consists of a Text Box and a List Box. When you click on an element in the list of a Combo Box, that element is copied to the Text Box and the ListIndex property is updated to point to that element. Both of these are now available for use, the list is collapsed, and the focus is on the Text Box portion. If you intercept the [Enter] keystroke (Chr$(13)) in the KeyPress event, you can process this information further.

    I have included an example from a working program below that recovers customer information from a database. The list portion of the Combo Box is loaded from a 2 dimensional array that relates the Customer Name to the Customer Number. This is necessitated because there can be identical names, but the numbers are unique. The array is loaded by a database query sorted by name, but the first element of the list is always loaded with an invalid name that will always be at the top of the list:
    No Match
    ABBOTT/MILENA MRS
    ABC AUTO IMPORTS
    ABERT/PIOTR MR
    ABOLADE/LILLIAN MS
    ABRAM/ALICJA MRS
    ABRAM/HELENA MRS
    The list will attempt to find whatever is typed into the Text Box. If an invalid name (or partial name) is entered, the list will always go to the top, and selecting the first item (ListIndex = 0) will produce an error message, because it is invalid. I am using a Combo Box configured as a Dropdown Combo, because I want to allow new customers to be added here, but if you do not want to allow any entry, you should configure it as a Dropdown List. You will also notice that I am issuing a system call to automatically show the Dropdown list whenever the Combo Box receives the focus (this will be different in .net).

    J.A. Coutts
    Code:
    Private Sub Customer_Click()
        Call ClearGrid
        LSelect.Visible = True
        If Customer.ListIndex < 0 Then Exit Sub
        CustNum.Text = CustListData(1, Customer.ListIndex)
    End Sub
    
    Private Sub Customer_DblClick()
        Dim N%
        Dim Result As Long
        Dim vKeys As Variant
        Dim vExceptions As Variant
        Screen.MousePointer = 11
        Customer.Clear
        vKeys = "SELECT CustName, CustNo From Customer ORDER BY CustName;"
        Result = GetLocalData(vKeys, CustListData, vExceptions)
        For N% = 0 To UBound(CustListData, 2)
            Customer.AddItem CustListData(0, N%)
        Next N%
        ListFlg = False
        Screen.MousePointer = 0
    End Sub
    
    Private Sub Customer_DropDown()
        If ListFlg Then
            Call Customer_DblClick
            ListFlg = False
        End If
    End Sub
    
    Private Sub Customer_GotFocus()
        Dim Tmp%
        Tmp% = SendMessage(Customer.hWnd, CB_SHOWDROPDOWN, 1, ByVal 0&)
    End Sub
    
    Private Sub Customer_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then
            If Customer.ListIndex < 0 Then
                MsgBox "No Selection Made!"
            Else
                RecNo = CustNum.Text
                Call GetRecord(RecNo)
                RecGrid.SetFocus
                LSelect.Visible = False
            End If
        End If
    End Sub
    Edit: The space in front of No Match got removed in transmission.
    Last edited by couttsj; September 18th, 2012 at 12:39 PM.

IMN logo majestic logo threadwatch logo seochat tools logo