• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

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

0
running32
Asked:
running32
  • 6
  • 4
  • 4
  • +2
1 Solution
 
phoenixfire425Commented:
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"
0
 
running32Author Commented:
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
0
 
phoenixfire425Commented:
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

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
guru_samiCommented:
how is your Listbox populated? does it has value  "Accident (Responsible for)"?
0
 
phoenixfire425Commented:
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

0
 
phoenixfire425Commented:
Do you need the selection to find by the name of the value??
0
 
Gagan_JauraCommented:
Try replacing the below code
ListBox1.SetSelected(intakedetailcount, True)
with
ListBox1.SetSelected(intakedetailcount + 1, True)
0
 
CodeCruiserCommented:
Have you tried

ListBox1.SelectedItem = refer

?

or

ListBox1.SelectedIndex = Listbox1.FindStringExact(refer)

?
0
 
running32Author Commented:
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
0
 
running32Author Commented:
Yes there is a value in the list box for Accident (Responsible for) but it is not selected.

Thanks
0
 
CodeCruiserCommented:
Did you try my suggestion?
0
 
running32Author Commented:
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

0
 
CodeCruiserCommented:
>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.
0
 
CodeCruiserCommented:
>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.
0
 
running32Author Commented:
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

0
 
running32Author Commented:
Thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now