Checkboxlist Help please ; ;

Senoska

Member
Joined
Jun 27, 2006
Messages
15
Programming Experience
Beginner
Okay this is what I'm working on. This program keeps track of Linkshell[Guild] mates in the game Final Fantasy XI. Points are given for attendance. I want to add bulk points to many members via a check box, but for some reason it only adds points to the person I have selected. Code a picture below:


llp5tb.jpg


VB.NET:
Imports System.IO
Imports System.Xml
Imports System.Data
Imports System.Windows.Forms

Public Class Home
    Inherits System.Windows.Forms.Form
    Dim personEditing As Person
    Dim idEditing As Integer = -1
    Dim eventEditing As Person2
    Dim Warnings As Double = 1
    Dim var1 As Double
    Dim var2 As Double
    Dim var3 As Double
    Dim var4 As Double
    Dim Math1 As Double
    Dim Math2 As Double
    Dim Math3 As Double
    Dim math4 As Double


    Sub SaveList2()
        If lstInfo.Items.Count > 0 Then
            Dim users(chkbox1.Items.Count - 1) As Person
            chkbox1.Items.CopyTo(users, 0)
            Dim objSW As System.IO.StreamWriter
            objSW = New System.IO.StreamWriter("Members.xml", False)
            Dim x As New System.Xml.Serialization.XmlSerializer(users.GetType)
            x.Serialize(objSW, users)
            x = Nothing
            objSW.Close()
            objSW.Dispose()
        End If
    End Sub

    Sub ReadList2()
        If System.IO.File.Exists("Members.xml") Then
            chkbox1.Items.Clear()

            Dim users() As Person

            Dim sInput As String = System.IO.File.ReadAllText("Members.xml")
            Dim clsStream As New System.IO.MemoryStream()
            clsStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(sInput), 0, sInput.Length)
            clsStream.Position = 0
            Dim objSR As New System.Xml.XmlTextReader(clsStream)
            Dim x As New System.Xml.Serialization.XmlSerializer(GetType(Person()))
            users = x.Deserialize(objSR)
            x = Nothing
            objSR.Close()

            If users.Length > 0 Then
                For intA As Integer = 0 To users.Length - 1
                    chkbox1.Items.Add(users(intA))
                Next intA
            End If
        End If
    End Sub

    Private Sub chkbox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox1.SelectedIndexChanged
        If chkbox1.SelectedIndex > -1 Then
            Dim box As CheckedItemCollection
            box = chkbox1.CheckedItems
            idEditing = chkbox1.SelectedIndex
            personEditing = CType(chkbox1.SelectedItem, Person)
        End If
    End Sub


    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        var2 = personEditing.Points
        var3 = TextBox3.Text
        var2 = var2 + var3
        personEditing.Points = var2
        Call SaveList()
        Call ReadList()
    End Sub
End Class
The Person class:
VB.NET:
<Serializable()> _
Public Class Person
    Public Name As String
    Public MainJob As String
    Public SubJob As String
    Public Secondary As String
    Public Warning As String
    Public COP As String
    Public Zilart As String
    Public ToAU As String
    Public Fishing As String
    Public Woodworking As String
    Public Smithing As String
    Public Goldsmithing As String
    Public Clothcraft As String
    Public Leathercraft As String
    Public Bonecraft As String
    Public Alchemy As String
    Public Cooking As String
    Public Points As String
    Public Email As String
    Public Math1 As String
    Public Comments As String
    Public IsALeader As String
    Public Crafting As String
    Public Trial As String
    Public MemberStat As String
    Public Status As String
    Public Comments2() As String



    Public Overrides Function ToString() As String
        Return Name & Status
    End Function
End Class
Any idea ><?

If this isnt enough info post here or send me an email at t2small AT yahoo DOT com and I can send you the sorce code.
 
Do you want to add the same number of points to everyone? Is there a way to tell who needs the points if you're looking at a person individually? You probably need a variable as an integer to act as a counter and then do something like
VB.NET:
For x = 0 to Listbox1.Items.Count - 1
'give the points here
Next
Sorry, I'm not looking at this code too closely tonight, but this one line caught my eye.
VB.NET:
  If chkbox1.SelectedIndex > -1 Then
Won't the index always be greater than -1?
 
RE: the -1 issue - ah... no, because if nothing is currently selected, the selected index = -1.

As for why it only adds to one person.... you only ever deal with the SelectedItem.... not the entire CheckedITems collection.

-tg
 
Changing it to CheckedItems I get the following errors:

Error 2 'System.Windows.Forms.CheckedListBox.CheckedItemCollection' cannot be converted to 'TaruTaruProductions.Person'.
Error 'System.Windows.Forms.CheckedListBox.CheckedItemCollection' cannot be converted to 'Integer'.
 
For one thing, I think this line will give you a problem.
VB.NET:
Dim users() As Person

I don't think that is returning what you want. It looks like that will return a number of how many items are in the checkbox (minus one), not individual people. You might want to iterate through the whole checkbox with a For...Next loop and if the item at the current index is checked, then do stuff with it.

Also, at what line are you hitting the second error that you posted?
 
Back
Top