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

Arraylist StringFormat / StringAlignment Problem vb.net vb code

I have an arraylist filled with text that is used to populate a bunch of drawable text.  I include in the arraylist a stringformat collection which is built from a file.

My problem is this, when ever I change stringformat.stringalignment.alignment, VB wants to update the value of alignment in all the text in the arraylist.  All I really want to do is to store the Near, Center and Far inforamtion and use it to format the text when the drawabletext box is created at runtime.  Any Ideas why the arraylist might be updated in the manner.
0
atlind
Asked:
atlind
  • 3
  • 2
1 Solution
 
PaulHewsCommented:
>I include in the arraylist a stringformat collection which is built from a file.

How do you include it?  Sounds like you have one object that you are updating as you go instead of new objects that you are adding to the collection.  Post your code and we can fix it.
0
 
atlindAuthor Commented:
Thanks for the response....

It would be difficult to post the code as it spans several files.  But basically I have a text file that contains all the information I need to create a drawable text box.

ex..
ForeColor = Green
BackColor = Blue
Text = Some Text
Font = Arial, 12, Bold
Alignment = Center

I then read this file covert colors from text to Hex Color, Convert Font text to .font and convert Alignment into a StringFormat with the alignment set at what ever the text file has in it.

Now this file contains hundreds of text info blocks so I collect the information from one text block, do any conversion I need and store the converted info into an arraylist, then repeat for each text block in the file.

This issue I am having with the arraylist, is when ever I add a new set of information to the arraylist, when the stringformat inforation is added to the arraylist, all previously stored stringformat information is updated to match the most recently added info.

Example
Arraylist(0).stringformat.alignment = Center
Arraylist(1).stringformat.alignment = Center
Arraylist(2).stringformat.alignment = Far

In this example as soon as I store the info for the "Far" alignment I will get the following...
Arraylist(0).stringformat.alignment = Far
Arraylist(1).stringformat.alignment = Far
Arraylist(2).stringformat.alignment = Far

Likewise if I add Arraylist(3).stringformat.alignment = Near, then I get..
Arraylist(0).stringformat.alignment = Near
Arraylist(1).stringformat.alignment = Near
Arraylist(2).stringformat.alignment = Near
Arraylist(3).stringformat.alignment = Near

Hope this is a little more clear...
I know this may be troublesome to understand without the code, but my bose would not be none to happy If I put it on display...lol
0
 
PaulHewsCommented:
Right... But the problem is that each element of the arraylist points to the same object. Here's an example that demonstrates how the problem can occur, and shows how to fix it.


Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim List As New ArrayList
        Dim sf As New StringFormat
        For i As Integer = 1 To 5
            Dim TB As New TextBlock
            TB.Format = sf
            TB.Text = String.Format("Text {0}", i)
            TB.Format.Alignment = CType(i Mod 3, StringAlignment) 'Use Mod operator to cycle string alignments.'
            List.Add(TB)
        Next
        Debug.WriteLine("These will all be Far")
        For Each tb As TextBlock In List
            Debug.WriteLine(tb.Format.Alignment)  'Now they're all Far because they all point to one object'
        Next
 
        List.Clear()
 
        For i As Integer = 1 To 5
            Dim TB As New TextBlock
            TB.Format = New StringFormat
            TB.Text = String.Format("Text {0}", i)
            TB.Format.Alignment = CType(i Mod 3, StringAlignment)
            List.Add(TB)
        Next
        Debug.WriteLine("Cycling Center, Far, Near")
        For Each tb As TextBlock In List
            Debug.WriteLine(tb.Format.Alignment)  'Now they will cycle because they don't point to one object'
        Next
 
    End Sub
End Class
 
Public Class TextBlock
    Public Text As String
    Public Format As StringFormat
End Class

Open in new window

0
 
atlindAuthor Commented:
Ah perfect I knew it had to be simple...lol

Thank you so very much.  You were absolutely correct!!!  Well Done!
0
 
atlindAuthor Commented:
Perfect! Thank You Very Much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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