locked
how to use ComboBox valuemembre in fonction ?? RRS feed

  • Question

  • hello everyone

    i make combobox link with a database , it's show list of item which one has a value this value used in fonction to calcul for exmple

    list item       value membre

    Cr                0,04
    H2O             0,13
    H                 0,0001
    Br                0,034

    J = 400 * 45 / (0.01 * " here i want to put the value " )

    my problem that i want to show the list item for the user and when he selected item automatically he used the value membre

    i used

    J = 400 * 45 / (0.01 * combobox1.valuemembre ) didnt work

    _________________________________________________
    J = 400 * 45 / (0.01 * combobox1.selectvalue )     didnt work

    _________________________________________________
    dim N as double 

    N = combobox1.valuemembre

    J = 400 * 45 / (0.01 * N )                                    didnt work


    Friday, August 21, 2020 6:17 PM

Answers

  • Hi

    Try this stand alone example and see if it helps any.

    ' Form1 with Label1, Label2 and ComboBox1

    Option Strict On Option Explicit On Public Class Form1 Dim dt As New DataTable("Freddy") Dim BS As New BindingSource Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With dt .Columns.Add("Item", GetType(String)) .Columns.Add("Value", GetType(Double)) .Rows.Add("Cr", 0.04) .Rows.Add("H2O", 0.13) .Rows.Add("H", 0.0001) .Rows.Add("Br", 0.034) End With BS.DataSource = dt With ComboBox1 .DataSource = BS .DisplayMember = "Item" .ValueMember = "Value" End With End Sub ' this function not used in this example Function GetDisplayed() As String Dim item As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView) Return item.Row.Item("Item").ToString End Function ' this function is used Function GetVal() As Double Dim item As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView) Dim v As Double = 0D Double.TryParse(item.Row.Item("Value").ToString, v) Return v End Function Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim j As Double = 400 * 45 / (0.01 * GetVal()) ' to 4 significant places Label2.Text = j.ToString("0.0000") End Sub End Class



    Regards Les, Livingston, Scotland



    • Marked as answer by Houssem TABET Friday, August 21, 2020 8:36 PM
    • Edited by leshay Friday, August 21, 2020 10:13 PM removed redundant Sub
    Friday, August 21, 2020 7:37 PM

All replies

  • Hi

    Try this stand alone example and see if it helps any.

    ' Form1 with Label1, Label2 and ComboBox1

    Option Strict On Option Explicit On Public Class Form1 Dim dt As New DataTable("Freddy") Dim BS As New BindingSource Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With dt .Columns.Add("Item", GetType(String)) .Columns.Add("Value", GetType(Double)) .Rows.Add("Cr", 0.04) .Rows.Add("H2O", 0.13) .Rows.Add("H", 0.0001) .Rows.Add("Br", 0.034) End With BS.DataSource = dt With ComboBox1 .DataSource = BS .DisplayMember = "Item" .ValueMember = "Value" End With End Sub ' this function not used in this example Function GetDisplayed() As String Dim item As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView) Return item.Row.Item("Item").ToString End Function ' this function is used Function GetVal() As Double Dim item As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView) Dim v As Double = 0D Double.TryParse(item.Row.Item("Value").ToString, v) Return v End Function Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim j As Double = 400 * 45 / (0.01 * GetVal()) ' to 4 significant places Label2.Text = j.ToString("0.0000") End Sub End Class



    Regards Les, Livingston, Scotland



    • Marked as answer by Houssem TABET Friday, August 21, 2020 8:36 PM
    • Edited by leshay Friday, August 21, 2020 10:13 PM removed redundant Sub
    Friday, August 21, 2020 7:37 PM
  • Here is an example which populates a list from data is a database, zero reasons for a DataSet or DataTable.

    Public Class ComboBoxItem
        Public Property Text() As String
        Public Property Value() As Decimal
    
        Public Overrides Function ToString() As String
            Return Text
        End Function
    End Class
    

    Read data

    Imports System.Data.SqlClient
    
    Public Class DataOperationsSqlServer
        Private Shared ConnectionString As String =
                           "Data Source=.\SQLEXPRESS;" &
                           "Initial Catalog=ForumExample;Integrated Security=True"
    
        Public Shared Function ComboBoxListItems() As List(Of ComboBoxItem)
            Dim list As New List(Of ComboBoxItem)
    
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
                    cmd.CommandText = "SELECT DisplayMember, ValueMember FROM dbo.DemoTable;"
                    cn.Open()
                    Dim reader = cmd.ExecuteReader()
    
                    While reader.Read()
                        list.Add(New ComboBoxItem() With {
                                    .Text = reader.GetString(0),
                                    .Value = reader.GetDecimal(1)})
                    End While
                End Using
            End Using
    
            Return list
    
        End Function
    
    End Class

    Form Load populate ComboBox, get the decimal value in SelectedIndexChanged event of the ComboBox. No need to set DisplayMember as the ToString in ComboBoxItem is used automatically.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
    
            ComboBox1.DataSource = DataOperationsSqlServer.ComboBoxListItems()
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
            Handles ComboBox1.SelectedIndexChanged
    
            '
            ' Here is your value
            '
            Dim currentValue = CType(ComboBox1.SelectedItem, ComboBoxItem).Value
        End Sub
    End Class
    

    Here I altered the code to show the value

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
        Handles ComboBox1.SelectedIndexChanged
    
        '
        ' Here is your value
        '
        Dim currentValue = CType(ComboBox1.SelectedItem, ComboBoxItem).Value
        TextBox1.Text = currentValue.ToString()
    End Sub

    Raw data

    In closing, if this is another type of database simply change the data provider.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, August 21, 2020 8:12 PM
  • thank you a lots guys its so helpful ^_^
    Friday, August 21, 2020 8:38 PM