Getting Value from selectedIndexChanged event????

webguy55

Member
Joined
Feb 14, 2010
Messages
12
Programming Experience
Beginner
Here I am having an issue. I have combo box selectedIndexChanged events going on but I cant figure out how to get the value out of them so that when i hit the btn_ProjSal that the new salaries show up in the 3 labels. Here is my code. Doing it straight from the selectedIndexChanged event this works..but to get the button to pull the new values from each of those events is my issue.

VB.NET:
Public Class Form1

    Dim _emp101Sal As Decimal = 10000.0
    Dim _emp102Sal As Decimal = 11500.0
    Dim _emp103Sal As Decimal = 12300.0
    Dim _emp101Raise As Decimal
    Dim _emp102Raise As Decimal
    Dim _emp103Raise As Decimal


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cb_101Perc.Items.Add(1)
        cb_101Perc.Items.Add(2)
        cb_101Perc.Items.Add(3)
        cb_101Perc.Items.Add(4)
        cb_101Perc.Items.Add(5)
        cb_101Perc.Items.Add(6)
        cb_101Perc.Items.Add(7)
        cb_101Perc.Items.Add(8)
        cb_101Perc.Items.Add(9)
        cb_101Perc.Items.Add(10)

        cb_102Perc.Items.Add(1)
        cb_102Perc.Items.Add(2)
        cb_102Perc.Items.Add(3)
        cb_102Perc.Items.Add(4)
        cb_102Perc.Items.Add(5)
        cb_102Perc.Items.Add(6)
        cb_102Perc.Items.Add(7)
        cb_102Perc.Items.Add(8)
        cb_102Perc.Items.Add(9)
        cb_102Perc.Items.Add(10)

        cb_103Perc.Items.Add(1)
        cb_103Perc.Items.Add(2)
        cb_103Perc.Items.Add(3)
        cb_103Perc.Items.Add(4)
        cb_103Perc.Items.Add(5)
        cb_103Perc.Items.Add(6)
        cb_103Perc.Items.Add(7)
        cb_103Perc.Items.Add(8)
        cb_103Perc.Items.Add(9)
        cb_103Perc.Items.Add(10)

        Call ShowCurrentSalary()

    End Sub

    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click

        Me.Close()

    End Sub


    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click

        lbl_101Proj.Text = ""
        lbl_102Proj.Text = ""
        lbl_103Proj.Text = ""
        cb_101Perc.Text = ""
        cb_102Perc.Text = ""
        cb_103Perc.Text = ""

    End Sub

    Private Function ShowProjected(ByVal salary, ByVal raise)

        Dim newSalary As Decimal

        newSalary = salary * (1 + (raise * 0.01))
        Return newSalary

    End Function

    Private Function GrantRaise(ByRef salary, ByVal raise)

        Dim newSalary As Decimal

        newSalary = salary * (1 + (raise * 0.01))
        Return newSalary

    End Function

    Private Sub ShowCurrentSalary()

        lbl_101Sal.Text = FormatCurrency(_emp101Sal)
        lbl_102Sal.Text = FormatCurrency(_emp102Sal)
        lbl_103Sal.Text = FormatCurrency(_emp103Sal)

    End Sub


    Public Sub cb_101Perc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_101Perc.SelectedIndexChanged

        Dim intSelectedIndex As Integer

        intSelectedIndex = cb_101Perc.SelectedIndex
        Dim empSalary As Decimal
        Dim empRaise As Decimal
        Dim total As Decimal

        empSalary = _emp101Sal
        empRaise = intSelectedIndex + 1

        total = ShowProjected(empSalary, empRaise)

        'lbl_101Proj.Text = total.ToString("C2")

    End Sub

    Public Sub cb_102Perc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_102Perc.SelectedIndexChanged

        Dim intSelectedIndex As Integer

        intSelectedIndex = cb_101Perc.SelectedIndex
        Dim empSalary As Decimal
        Dim empRaise As Decimal
        Dim total As Decimal

        empSalary = _emp102Sal
        empRaise = intSelectedIndex + 1

        total = ShowProjected(empSalary, empRaise)

        'lbl_102Proj.Text = total.ToString("C2")

    End Sub

    Public Sub cb_103Perc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_103Perc.SelectedIndexChanged

        Dim intSelectedIndex As Integer

        intSelectedIndex = cb_101Perc.SelectedIndex
        Dim empSalary As Decimal
        Dim empRaise As Decimal
        Dim total As Decimal

        empSalary = _emp103Sal
        empRaise = intSelectedIndex + 1

        total = ShowProjected(empSalary, empRaise)

        'lbl_102Proj.Text = total.ToString("C2")

    End Sub

    Private Sub btn_ProjSal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ProjSal.Click

        lbl_101Proj.Text = total1.ToString("C2")
        lbl_102Proj.Text = total2.ToString("C2")
        lbl_103Proj.Text = total3.ToString("C2")


    End Sub


End Class
 
You don't get anything from the SelectedIndexChanged event. The event is raised and you handle it. That's it. It's simply a notification to allow you to execute code whenever the SelectedIndex property changes.

Now, if you want to get information from a ComboBox, you'll normally get either its SelectedIndex, SelectedItem or SelectedValue property. That goes no matter where the code is. Whether you code is in the ComboBox's SelectedIndexChanged event handler, the Click event handler of a Button or somewhere else entirely, those are the properties you should generally be using.
 
Back
Top