Link to home
Start Free TrialLog in
Avatar of running32
running32

asked on

Select items from listbox on form load

I need to be able to select items in the list box on form load that match with objects from my dataset.  

I wrote what I though might work but it gives me an error on ListBox1.SelectedValue = refer

The error is "Input string was not in a correct format"  refer was = to "Accident (Responsible for)"  which is in the listbox.  

thank you
Dim intakedetailcount
        SqlDataAdapter2.SelectCommand.Parameters("@paramid").Value = MHIntakeID
        SqlDataAdapter2.SelectCommand.Parameters("@type").Value = mhctype
        Try
            intakedetailcount = SqlDataAdapter2.Fill(Dsmhcselectedlist1)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '
        'highlight rows of listbox which are already selected 
        Dim j As Integer
        Dim refcount As Integer
        If Dsmhcselectedlist1.Tables(0).Rows.Count > 0 Then

            ''loop through each row
            For j = 0 To Dsmhcselectedlist1.Tables(0).Rows.Count - 1
                Dim refer As String
                ''loop through each column
                For intakedetailcount = 0 To Dsmhcselectedlist1.Tables(0).Columns.Count - 1
                    refer = Dsmhcselectedlist1.Tables(0).Rows(j).Item("strData")
                    RTrim(refer)
                    ListBox1.SetSelected(intakedetailcount, True)


                    ' ListBox1.SelectedValue = refer
                    ListBox1.SelectedValue = refer
                Next

            Next
        End If

Open in new window

Avatar of phoenixfire425
phoenixfire425
Flag of United States of America image

Change
ListBox1.SelectedValue = refer

to

ListBox1.SelectedValue = "refer"

if that is your value.
for instance.

<asp:ListItem Text="refer" Value="refer"></asp:ListItem>

Your Value would be "refer"

if

<asp:ListItem Text="refer" Value="1"></asp:ListItem>

Then your Value would be "1"
Avatar of running32
running32

ASKER

refer is a variable so it changes each time it loops.

In vb.net if I change it to "refer" it will look for the word refer which is not in the list.

Thanks
I think i read your questions wrong the first time.

you can try doing this this way.

here is an example.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ListBox1.SelectionMode = ListSelectionMode.Multiple
        Dim i As Integer
        For i = 0 To ListBox1.Items.Count - 1 Step i + 1
            ' Select the first, third and fifth items in the listbox
            If i = 0 Or i = 2 Or i = 4 Then
                ListBox1.Items(i).Selected = True
            End If
        Next
    End Sub

Open in new window

how is your Listbox populated? does it has value  "Accident (Responsible for)"?
Currently the list box is just setup as


    <asp:ListBox ID="ListBox1" runat="server">
                <asp:ListItem Text="one" Value="1"></asp:ListItem>
                <asp:ListItem Text="two" Value="2"></asp:ListItem>
                <asp:ListItem Text="three" Value="3"></asp:ListItem>
                <asp:ListItem Text="four" Value="4"></asp:ListItem>
                <asp:ListItem Text="Five" Value="5"></asp:ListItem>
    </asp:ListBox>

Open in new window

Do you need the selection to find by the name of the value??
Try replacing the below code
ListBox1.SetSelected(intakedetailcount, True)
with
ListBox1.SetSelected(intakedetailcount + 1, True)
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the delay I need to find it by name.  There needs to be multiple rows selected.

If I put a message box it will show the names in the loop but will not high light the rows.

thanks
Yes there is a value in the list box for Accident (Responsible for) but it is not selected.

Thanks
Did you try my suggestion?
Yes, thanks I tried both your suggestions.  

If I put the loop in activiated it loops and selects but then once the page is loaded it takes away the selected rows.  

thanks
  Private Sub frmMHCPopulations_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = mhcheader
        SqlDataAdapter1.SelectCommand.Parameters("@type").Value = mhctype
        Try
            listcount = SqlDataAdapter1.Fill(DsmhcPopulationlist1)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ListBox1.DisplayMember = "strData"
        ListBox1.ValueMember = "lngDataID"
        ListBox1.DataSource = DsmhcPopulationlist1.Tables("tblMHCIntakeDetail")
       


    End Sub



    Private Sub frmMHCPopulations_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        Dim intakedetailcount
        SqlDataAdapter2.SelectCommand.Parameters("@paramid").Value = MHIntakeID
        SqlDataAdapter2.SelectCommand.Parameters("@type").Value = mhctype
        Try
            intakedetailcount = SqlDataAdapter2.Fill(Dsmhcselectedlist1)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        'highlight rows of listbox which are already selected 
        Dim j As Integer
        Dim refcount As Integer
        If Dsmhcselectedlist1.Tables(0).Rows.Count > 0 Then
            ''loop through each row
            For j = 0 To Dsmhcselectedlist1.Tables(0).Rows.Count - 1
                Dim refer As String
                'loop through each column
                refer = Dsmhcselectedlist1.Tables(0).Rows(j).Item("strData")
                ListBox1.SetSelected(intakedetailcount, True)
                MsgBox(RTrim(refer))
                ListBox1.SelectedIndex = ListBox1.FindStringExact(refer)
            Next
        End If
    End Sub

Open in new window

>once the page is loaded it takes away the selected rows.  
Did not understand you. The loaded event is fired before the activated event i think.
>once the page is loaded it takes away the selected rows.  
Did not understand you. The loaded event is fired before the activated event i think.
I put it in the paint and now it is working.   Thanks
Private Sub listboxselect()
        Dim intakedetailcount
        SqlDataAdapter2.SelectCommand.Parameters("@paramid").Value = MHIntakeID
        SqlDataAdapter2.SelectCommand.Parameters("@type").Value = mhctype
        Try
            intakedetailcount = SqlDataAdapter2.Fill(Dsmhcselectedlist1)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ListBox1.SetSelected(0, False)
        'highlight rows of listbox which are already selected 
        Dim j As Integer
        Dim refcount As Integer
        If Dsmhcselectedlist1.Tables(0).Rows.Count > 0 Then
            ''loop through each row
            For j = 0 To Dsmhcselectedlist1.Tables(0).Rows.Count - 1
                Dim refer As String
                'loop through each column
                refer = Dsmhcselectedlist1.Tables(0).Rows(j).Item("strData")
                ' ListBox1.SetSelected(intakedetailcount, True)
                ListBox1.SelectedIndex = ListBox1.FindStringExact(refer)
            Next
        End If
    End Sub

 

    Private Sub frmMHCPopulations_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        listboxselect()
    End Sub

Open in new window

Thanks