Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > .NET Framework 1.x
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 2nd, 2007, 02:00 PM
Authorized User
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default change only the first item in array's back color i

hi i'm using vb.net.

i have a listbox and its a multiple selected listbox. and i have a array that give the values need to be selected in listbox. So while page load i need to display items to listbox and select the items depends upon the array value. so when the page load depends upon the array it will select multiple items in listbox. and here is the code i used to display mutiple selected items in listbox when listbox is loaded. and array won't be same all the time. depends upon the on textbox value the array keeps on changing.

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

If File.Exists("C:\Program\data.mdb") Then
            Dim pArray As New ArrayList

            Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                               "Data Source=" & "C:\Program\data.mdb" & " "

            Dim myConnection As New OleDbConnection(strConn)

            Dim schemaTable As DataTable
            'This will get all the table names from Database to DataTable
            schemaTable = myConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})

            Dim Table1 As DataTable = New DataTable("Orders")
            Dim Row As DataRow

            Dim Description As DataColumn = New DataColumn("Description")

            Dim Group As DataColumn = New DataColumn("Group")
            Group.DataType = System.Type.GetType("System.Int32")

            Dim i As Integer
            For i = 0 To schemaTable.Rows.Count - 1
                Dim NotInArray As Boolean = True

                'Taking Each Table Name from DataTable
                Dim pTableName As String = schemaTable.Rows(i)!TABLE_NAME.ToString

                'Checking the table names Starts with "MG" or "MM"
                If Microsoft.VisualBasic.Left(pTableName, 2) = "MG" Or Microsoft.VisualBasic.Left(pTableName, 2) = "MM" Then
                    Dim strSQL As String = "Select Description, Group, pl1 from " & pTableName & " where [Group] <> 0"
                    Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
                    Dim myReader As OleDbDataReader = myCommand.ExecuteReader
                    While myReader.Read
                        'If NotInArray =True, then write to DataTable
                        NotInArray = True
                        Dim j As Integer
                        If pArray.Count > 0 Then
                            For j = 0 To pArray.Count - 1
                                'Checking whether Description already exist in Array(already added to DataTable)
                                'No duplication of Description in DataTable File
                                If pArray(j).ToString = myReader(0) Then
                                    'If Description is already Added in DataTable file then don't write to DataTable
                                    NotInArray = False
                                    Exit For
                                End If
                        End If

                        'Write to DataTable, this Description is not Added before
                        If NotInArray = True Then
                            Row = Table1.NewRow()
                            Row("Description") = Trim(myReader(0)) & "   [" & myReader(1) & "]" & "  [" & myReader(2) & "]"
                        End If
                    End While
                End If

            Dim objDataView As New DataView(Table1)
            objDataView.Sort = "Description ASC"
            ListBox1.DataSource = objDataView
            ListBox1.DisplayMember = "Description"
            ListBox1.ValueMember = "Group"
            ListBox1.SelectedValue = ""


            MessageBox.Show("No file exist")
        End If
    End Sub

Function SelectMultipleValuesListBox(ByVal sTable As DataTable)

        Dim objDataView As New DataView(sTable)
        objDataView.Sort = "Description ASC"

        Dim dvRow As DataRowView
        Dim i As Integer = 0
        Dim g As Integer = 0

        i = 0
        For Each dvRow In objDataView
            i = i + 1
            If aryListBox.Count - 1 < g Then
                Exit For
            End If
            If dvRow.Item(0).split("[")(1).split("]")(0) = aryListBox(g).ToString Then
                ListBox1.SelectedIndex = i - 1
                g = g + 1
                GoTo CheckNextValue
            End If

    End Function
now according to the array i can select multiple items in listbox. for that i used the code
ListBox1.SelectedIndex = i - 1
is there anyway i can change only the first item in array's back color in listbox.
Eg: array {100,200,300}. so in my listbox item 100,200,300 will be selected when page load. i need to change only the back color of item 100 or fore color of item 100.

and i try this code...
ListBox1.SelectedIndex = i - 1
ListBox1.Items(i - 1).BackColor = System.Drawing.Color.Red
ListBox1.Items(i - 1).ForeColor = System.Drawing.Color.White
but error occurs for this code...
An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll

Additional information: Public member 'ForeColor' on type 'DataRowView' not found.
if you have any idea, please let me know. i'm not getting any help while searching. so if you have any idea please help me and if you can provide an example then it will be great help for me.

thanks in advance.
Old November 5th, 2007, 10:43 PM
Authorized User
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts

Heah... i found this code to solve this problem... hope this will help anyone...

Private Sub DrawItemHandler(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem


        Dim textFont As Font
        textFont = New Font(e.Font.FontFamily, e.Font.Size * 1)

       e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index).ToString(), textFont, New SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y)

    End Sub 'DrawItemHandler

    Private Sub MeasureItemHandler(ByVal sender As Object, ByVal e As MeasureItemEventArgs)
        e.ItemHeight = 22
    End Sub 'MeasureItemHandler

Similar Threads
Thread Thread Starter Forum Replies Last Post
form back color; saidan C# 2005 12 October 29th, 2007 08:16 AM
menu item with out post back pabidi General .NET 0 June 23rd, 2007 02:17 AM
back color of cell of datagrid rajessnit@gmail.com VB.NET 1 November 18th, 2006 03:28 AM
How to change the back color of toolbat allenfei BOOK: Professional C#, 2nd and 3rd Editions 3 August 15th, 2005 02:37 AM
listbox, different color for each item? MichaelTJ ADO.NET 1 November 13th, 2003 10:37 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.