Solved

Combobox shows blank values.

Posted on 2014-04-25
6
424 Views
Last Modified: 2014-04-25
I have a windows form which contains a a combo box named cboCurrentPermission.  I have  created Private Sub LoadUserTab(Optional ByRef FlowLayoutClear As Boolean = False) to load all data for the form.  I call the same Sub for Form_Load and tabpanel_Enter.

When the form first loads I get the results in the picture next to 1.  When I click on the database tab and then back on the User tab is get the results in picture 3.
Combobox example
 It calls the same code so I cannot figure out why one shows text and the other show blank values.  

I have included the code for the whole form.

Public Class fmCSSMS_Settings
    Private WithEvents buton As New Button
    Dim i As Integer
    Dim UserAccountsDoneLoading As Boolean
    Dim conn As New OleDb.OleDbConnection
    Dim da As OleDb.OleDbDataAdapter
    Dim dt As DataTable = New DataTable
    Dim UserID As Integer

    'This event handles all the click events from the dynamic buttons in the FlowLayoutControl1
    Private Sub buton_Click(sender As Object, e As EventArgs)
        btnUpdate.Text = "Update"
        Dim btn As Button = CType(sender, Button)
        If btn.Name = "btnAddUser" Then
            cbDisable.Visible = False
            btnUpdate.Text = "Add User"
            clearForm()
        Else
            lblName.Text = dt.Rows(CInt(btn.Name)).ItemArray(1).ToString & " " & dt.Rows(CInt(btn.Name)).ItemArray(3).ToString _
                & ", " & dt.Rows(CInt(btn.Name)).ItemArray(2).ToString
            cboRank.Text = dt.Rows(CInt(btn.Name)).ItemArray(1).ToString
            tbFirstName.Text = dt.Rows(CInt(btn.Name)).ItemArray(2).ToString
            tbLastName.Text = dt.Rows(CInt(btn.Name)).ItemArray(3).ToString
            tbOffice.Text = dt.Rows(CInt(btn.Name)).ItemArray(5).ToString
            tbPhone.Text = dt.Rows(CInt(btn.Name)).ItemArray(6).ToString
            tbEmail.Text = dt.Rows(CInt(btn.Name)).ItemArray(7).ToString
            tbNote.Text = dt.Rows(CInt(btn.Name)).ItemArray(9).ToString
            btnUpdate.Visible = True
            cbDisable.Visible = True
            UserID = dt.Rows(CInt(btn.Name)).ItemArray(0).ToString
            lblModifyby.Text = dt.Rows(CInt(btn.Name)).ItemArray(10).ToString
            lblModifyDate.Text = dt.Rows(CInt(btn.Name)).ItemArray(11).ToString
        End If
        LoadUserTab(True)
    End Sub

    Private Sub fmCSSMS_Settings_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        UserAccountsDoneLoading = False
    End Sub

    Private Sub tpUsers_Enter(sender As Object, e As EventArgs) Handles tpUsers.Enter
        btnUpdate.Visible = False
        cbDisable.Visible = False
        LoadUserTab(True)
        
    End Sub



    Private Sub tpUsers_Leave(sender As Object, e As EventArgs) Handles tpUsers.Leave
        UserAccountsDoneLoading = False
    End Sub

    Private Sub fmCSSMS_Settings_Load(sender As Object, e As EventArgs) Handles Me.Load
        clearForm()
        LoadUserTab(True)

    End Sub

    'Clear all User Information Fields
    Private Sub clearForm()
        lblName.Text = ""
        cboRank.Text = ""
        tbFirstName.Text = ""
        tbLastName.Text = ""
        tbOffice.Text = ""
        tbPhone.Text = ""
        tbEmail.Text = ""
        tbNote.Text = ""
        btnUpdate.Visible = False
        cbDisable.Visible = False
        lblPSWError.Visible = False
        lblModifyby.Text = ""
        lblModifyDate.Text = ""
        cboCurrentPermission.Items.Clear()
    End Sub

    'encrypt and save new User password to database.  Encryption key is set in encryption.class
    Private Sub btnPWDChange_Click(sender As Object, e As EventArgs) Handles btnPWDChange.Click
        If tbNewPWD.Text = tbConfirmPWD.Text Then
            conn.ConnectionString = My.Settings.connPro & My.Settings.connStr
            conn.Open()
            Dim encryptedPassword As String = EncryptData.Encrypt(tbConfirmPWD.Text)
            Dim SQL As String = "UPDATE Users" &
                " SET UserPassword = '" & encryptedPassword &
                "' WHERE ID = " & UserID
            Using cmd = New OleDbCommand(SQL, conn)
                cmd.ExecuteNonQuery()
            End Using
        Else
            lblPSWError.Visible = True
        End If

    End Sub
    'Confirm the passwords entered match.  
    'TODO: This will also be where we will set the password requirements if there are any.
    Private Sub tbConfirmPWD_KeyUp(sender As Object, e As KeyEventArgs) Handles tbConfirmPWD.KeyUp
        If tbNewPWD.Text = tbConfirmPWD.Text Then
            lblPSWError.Visible = False
        Else
            lblPSWError.Visible = True
        End If
    End Sub

    'This sub loads the FlowLayout of all users in the database and stores them to the DT.  This DT is stored until 
    'the form is closed out.
    Private Sub LoadUserTab(Optional ByRef FlowLayoutClear As Boolean = False)
        clearForm()
        If UserAccountsDoneLoading <> True Then
            If FlowLayoutClear = True Then FlowLayoutPanel1.Controls.Clear()
            dt = Nothing
            Try
                conn.ConnectionString = My.Settings.connPro & My.Settings.connStr
                conn.Open()
                Dim oSQL As String = "SELECT * FROM Users WHERE Disabled = FALSE"
                'Dim ds As DataSet = New DataSet
                Dim UserCount As Integer
                da = New OleDb.OleDbDataAdapter(oSQL, conn)
                dt = New DataTable
                da.Fill(dt)
                UserCount = dt.Rows.Count
                buton = New Button
                buton.BackgroundImage = My.Resources.User_Add
                buton.Width = 65
                buton.Height = 65
                buton.TextAlign = ContentAlignment.BottomLeft
                buton.Text = ""
                buton.Name = "btnAddUser"
                AddHandler buton.Click, AddressOf buton_Click
                FlowLayoutPanel1.Controls.Add(buton)
                For i = 0 To UserCount - 1
                    buton = New Button
                    buton.BackgroundImage = My.Resources.User_64
                    buton.Width = 65
                    buton.Height = 65
                    buton.TextAlign = ContentAlignment.BottomLeft
                    buton.Text = dt.Rows(i).ItemArray(1).ToString & " " & dt.Rows(i).ItemArray(3).ToString
                    buton.Name = i.ToString ' dt.Rows(i).ItemArray(0).ToString
                    AddHandler buton.Click, AddressOf buton_Click
                    FlowLayoutPanel1.Controls.Add(buton)
                Next

                da = Nothing

                'Load Permissions Combobox
                Dim p As Integer
                Dim SQL As String = "SELECT LevelName " &
                    "FROM UserRights"

                'Dim pDt As DataTable = New DataTable
                Dim ds As DataSet = New DataSet
                da = New OleDb.OleDbDataAdapter(SQL, conn)
                da.Fill(ds)
                p = ds.Tables(0).Rows.Count

                For j = 0 To p - 1
                    cboCurrentPermission.Items.Add(ds.Tables(0).Rows(j).ItemArray(0).ToString)
                Next
                ds = Nothing

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
            End Try
            UserAccountsDoneLoading = True
        End If

        conn.Close()
        conn.Dispose()

    End Sub

End Class

Open in new window


It did not matter if I used a datable or dataset.

Thanks in advance.
cboIssue.png
0
Comment
Question by:Chrispy2811
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40022529
please try to use the _Activate event instead of the _Load event.
however, I agree that's not too clear to me why it does not show the items ...
0
 
LVL 2

Author Comment

by:Chrispy2811
ID: 40022551
I tried adding it to the _Activate even no dice.

- the drop down font does not change when going from the initial form load and then on tab panel_Enter.  However I gave the suggestion a try and removed it and added a new one back and received the same results.
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40022561
line 109: If UserAccountsDoneLoading <> True Then


might be the culprit

on line 4 change to
Dim UserAccountsDoneLoading As Boolean = false
0
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

 
LVL 2

Author Comment

by:Chrispy2811
ID: 40022589
No dice either, same results.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40022622
please put a breakpoint on this line :
cboCurrentPermission.Items.Clear()

and the line(s)
For j = 0 To p - 1
                    cboCurrentPermission.Items.Add(ds.Tables(0).Rows(j).ItemArray(0).ToString)
                Next

run in debug mode and see where it stops, and which one is called last...
0
 
LVL 2

Author Comment

by:Chrispy2811
ID: 40022680
Well I am an idiot.  It was loading the dropdown and then when I would click on the drop down it would call the tabpanel_Enter event. Which the first thing it did was call the ClearForm event.  

Thanks.

I changed the code.  There was no real need to have the Items.Clear() in there.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

22 Experts available now in Live!

Get 1:1 Help Now