Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select data in a combox

Status
Not open for further replies.

jpower69

Programmer
Feb 5, 2008
54
US
I am trying to figure out how to select data in a combobox whose dropdowastyle is dropdownlist.
when my form first loads, it populates the combox, named cmbox1 with the following statements;
lsql = "Select currency from [sec_mast].[dbo].[sec_fxrt] group by currency "
'
' populate first combox
'

Try
cmd = New SqlCommand(lsql, conn)
adapter.SelectCommand = cmd
adapter.Fill(ds)
adapter.Dispose()
cmd.Dispose()

cmbox1.DataSource = ds.Tables(0)
cmbox1.ValueMember = "currency"
cmbox1.DisplayMember = "currency"

Catch ex As Exception
MessageBox.Show("cannot open connection")

End Try

this works no problem. My problem is that I ca not figure out the correct steps to select a different currency in the cmbx1.
I have tried the Click method, but that executes once i click on the dropdown.
I have tried the selectedindexchange/selectedvalue change but I get my message when the form loads of No records

Can anyone suggest what I should try...ir shoiuld I try something else here

Thanks in advance for any information
 
Did you SelectedIndex:

Code:
Public Class Form1

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

        With cmbox1
            .Items.Clear()
            For i As Integer = 1 To 25
                .Items.Add("My Item " & i)
            Next
            .SelectedIndex = 0
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
[blue]
        cmbox1.SelectedIndex = 5
[/blue]
    End Sub
End Class

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
no, not yet
i'm trying trying that now when you relpied

i'll let you know

thanks for getting back
 
out of curiosity, why would a public sub for a combox be executed twice

the first time it loads the data into the dropdown..then it starts all over again and displays an message "no records" since no records where selected the
second time..
Private Sub cmbox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbox1.SelectedIndexChanged

'
' default settings
'
txtcurrency.Text = "EUR" <<< default settings for the first time trhu
txtcurrency.Text = "EUR"
txtcur1.Text = "EUR"
txtcur1.Text = "EUR"
txtbase.Text = "USD"
txtbase1.Text = "USD"
txtamount1.Text = "0.000000"
txtresult.Text = "0.000000"

If wcurr = "" Then
wcurr = "EUR"
txtcurrency.Text = wcurr
txtcur1.Text = wcurr
Else
wcurr = cmbox1.SelectedValue.ToString
txtcurrency.Text = wcurr
txtcur1.Text = wcurr
End If

'
' set up SQl string
'
lsql = "Select effect_dat, fxrate, currency from [sec_mast].[dbo].[sec_fxrt] where currency = '" & wcurr & "' order by effect_dat desc"

' conn = New SqlConnection(strconnect)

cmd = New SqlCommand(lsql, conn)

myreader = cmd.ExecuteReader

If myreader.HasRows Then
ListView1.Items.Clear()

Do While myreader.Read()
wcnt = wcnt + 1

If wcnt = 1 Then
txtcurrency.Text = wcurr
txtcur1.Text = wcurr
txtcurr_rate.Text = myreader("fxrate")
txteffect_dt.Text = myreader("effect_dat")
End If


Dim item As New ListViewItem
item.Text = myreader("effect_dat")
item.SubItems.Add(myreader("fxrate"))
item.SubItems.Add(myreader("currency"))
ListView1.Items.Add(item)
Loop
Else
MsgBox("no records ")<<<< displays this the second time around
End If

myreader.Close()


End Sub




 
You may Google "selectedindexchanged fires twice"

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top