简体   繁体   中英

VB.net change ComboBox selected value and display

I am using this code in vb.net:

Function GetBillingMonthsGrouped() As List(Of BillingMonthsGrouped)
        Dim conn = New MySqlConnection()
        Dim conn2 = New MySqlConnection()
        Dim myCommand, myCommand2 As New MySqlCommand
        Dim reader, reader2 As MySqlDataReader
        Dim SQL As String

        conn.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "
        conn2.ConnectionString = "server=" + global_variables.web_server_ip + "; user id=" + global_variables.web_server_username + "; password=" + global_variables.web_server_password + "; database=" + global_variables.web_server_database + "; "

        Dim billingMonthsGrouped = New List(Of BillingMonthsGrouped)

        'customers
        conn.Open()
        SQL = "select MONTH(timestamp) from billing group by MONTH(timestamp) order by MONTH(timestamp) ASC "
        myCommand.Connection = conn
        myCommand.CommandText = SQL
        reader = myCommand.ExecuteReader
        While reader.Read()
            billingMonthsGrouped.Add(New BillingMonthsGrouped(reader.GetString(0), MonthName(reader.GetString(0))))
        End While
        conn.Close()

        Return billingMonthsGrouped
    End Function
    Public Class BillingMonthsGrouped
        Public Sub New(ByVal id As String, ByVal name As String)
            mID = id
            mName = name
        End Sub

        Private mID As String
        Public Property ID() As String
            Get
                Return mID
            End Get
            Set(ByVal value As String)
                mID = value
            End Set
        End Property

        Private mName As String
        Public Property Name() As String
            Get
                Return mName
            End Get
            Set(ByVal value As String)
                mName = value
            End Set
        End Property
    End Class

and then this part:

ComboBox3.DataSource = GetBillingMonthsGrouped()
ComboBox3.DisplayMember = "Name"
ComboBox3.ValueMember = "ID"
ComboBox3.SelectedIndex = 0

What code would i need to use to change the selected value of ComboBox3 to be the current month

I tried using:

ComboBox3.SelectedValue = DateTime.Now.ToString("MM")

but that doesn't change the selected value. I need to have the value as the month number and the display as the month name

Hope that the comboBox already having the months, you need to set current month as selected. then in this situation you can use the following code to select current month as selected month

  ComboBox1.SelectedIndex = Now.Month

If you want to display one month only means you can use the code:

  ComboBox1.SelectedText = DateTime.Today.ToString("MMMM") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM