PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 StreamWriter Bad Formating?-VBForums
Results 1 to 21 of 21

Thread: StreamWriter Bad Formating?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    StreamWriter Bad Formating?

    Hey, I got 3 listboxes.

    2 Listboxes gives values to listbox3 like "listbox1item:listbox2item"

    When I export it to txt file and open in notepad, it shows

    Code:
    listbox1item:listbox2item
    
    listbox1item:listbox2item
    
    listbox1item:listbox2item
    but when I open same file on wordpad and other text editors it shows like
    Code:
    listbox1item:listbox2item
    
    listbox1item:
    listbox2item
    
    listbox1item:
    listbox2item
    Only first is as it should be, rest isnt on same line.
    Here is my code that exports listbox3


    Code:
            Dim myStream As Stream
            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "text files (*.txt)| *.txt"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True
            Dim fname As String = saveFileDialog1.FileName
            Dim i As Integer
            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
    
                myStream = saveFileDialog1.OpenFile()
    
                Using sw As StreamWriter = New StreamWriter(myStream)
    
                    For i = 0 To ListBox3.Items.Count - 1
                        sw.WriteLine(ListBox3.Items.Item(i))
                    Next
                End Using
                myStream.Close()
            End If
    Need some help

  2. #2
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: StreamWriter Bad Formating?

    You might try avoiding the stream.

    Using sw As New StreamWriter(fname)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Quote Originally Posted by ZenDisaster View Post
    You might try avoiding the stream.

    Using sw As New StreamWriter(fname)
    Thanks but doesnt work.

  4. #4
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: StreamWriter Bad Formating?

    change:
    sw.WriteLine(ListBox3.Items.Item(i))

    to:
    sw.WriteLine(ListBox3.Items(i))

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Dont know why but it still does the same...

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    what code did you use to join the item from Listbox1 with the item from Listbox2 before adding it to Listbox3?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Quote Originally Posted by Inferrd View Post
    what code did you use to join the item from Listbox1 with the item from Listbox2 before adding it to Listbox3?
    I actually found my problem allready but I cant fix it. Indeed, its code that makes item from listboxes to listbox3. I dont know how to check if textbox contains multiple strings.

    I need it like this:

    If textbox contains "1" and "2" then
    elseif textbox contains "1" then
    elseif textbox contains "2" then
    else
    end if

    If somebody can help me with that, it should work.

  8. #8
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    I'm pretty sure I'm not understanding, but
    vb Code:
    1. If TextBox1.Text.Contains("1") AndAlso TextBox1.Text.Contains("2") Then
    2.             MessageBox.Show("Contains 1 and 2")
    3.         ElseIf TextBox1.Text.Contains("1") Then
    4.             MessageBox.Show("Contains 1")
    5.         ElseIf TextBox1.Text.Contains("2") Then
    6.             MessageBox.Show("Contains 2")
    7.         Else
    8.             MessageBox.Show("Can't see 1 or 2")
    9. End If
    but will work for "1234" also

    Is the text as "12" or "1 2" or " 1,2" , or on different lines ..... ?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Quote Originally Posted by Inferrd View Post
    I'm pretty sure I'm not understanding, but
    vb Code:
    1. If TextBox1.Text.Contains("1") AndAlso TextBox1.Text.Contains("2") Then
    2.             MessageBox.Show("Contains 1 and 2")
    3.         ElseIf TextBox1.Text.Contains("1") Then
    4.             MessageBox.Show("Contains 1")
    5.         ElseIf TextBox1.Text.Contains("2") Then
    6.             MessageBox.Show("Contains 2")
    7.         Else
    8.             MessageBox.Show("Can't see 1 or 2")
    9. End If
    but will work for "1234" also

    Is the text as "12" or "1 2" or " 1,2" , or on different lines ..... ?
    Thanks!

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    I didnt still get it working. Here is my code before exporting.

    Textbox1 text is "example1_example2"
    Code:
                        string1 = TextBox1.Text
                        For Each item1 As Object In ListBox1.Items
                            For Each item2 As Object In ListBox2.Items
                                string1 = string1.Replace("example1", item1)
                                string1 = string1.Replace("example2", item2)
                                ListBox3.Items.Add(string1)
                            Next
                        Next
    here is result of listbox3, as example

    Code:
    random1_test1
    random2_test1
    random3_test1
    random4_test1
    random5_test1
    random1_test2
    random2_test2
    random3_test2
    random4_test2
    random5_test2
    etc...

    But when random1 changes to random2, WordPad and other text editors starts to split those like:

    Code:
    random1_test1
    random2_
    test1
    random3_
    test1
    random4_
    test1
    random5_
    test1
    random1_
    test2
    random2_
    test2
    random3_
    test2
    random4_
    test2
    random5_
    test2
    etc...

    But it shows only in notepad how it should be, and even if I copy from notepad to wordpad, it splits those.

    Any help? I have tried to export with different encoders also.

  11. #11
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    Listbox 1 contains random1 , random2 , random3 , random 4 , random 5
    and Listbox 2 contains test1 , test2 (all on separate lines), right?
    Then
    Code:
                        string1 = TextBox1.Text
                        For Each item1 As Object In ListBox1.Items
                            For Each item2 As Object In ListBox2.Items
                                string1 = string1.Replace("example1", item1)
                                string1 = string1.Replace("example2", item2)
                                ListBox3.Items.Add(string1)
                            Next
                        Next
    doesn't produce the output you gave because after the first iteration string1 no longer contains "example1_example2" ??

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Code:
                        string1 = TextBox1.Text
                        For Each item1 As Object In ListBox1.Items
                            For Each item2 As Object In ListBox2.Items
                                string1 = string1.Replace("example1", item1)
                                string1 = string1.Replace("example2", item2)
                                ListBox3.Items.Add(string1)
                                string1 = TextBox1.Text
                            Next
                        Next
    Should fix it then?

  13. #13
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    Should produce the output you want, aye. Does it still split in WordPad ?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Yes... It still does that. Will that make any difference if listbox1 and listbox2 doesnt got same amount of items in there?

  15. #15
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    Should make no difference. Is anything changing the value in TextBox1 ?

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    No... Its strange. Its how it should be on listbox3, its how it should be on notepad, its not how it should be on other text editors. So, when its done all listbox1 + listbox2 items first time, it stars to split those.

  17. #17
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    That splitting normally happens to me when the text contains spurious control characters like Linefeed or Carriage Return. Notepad doesn't split on them, but Wordpad etc do. Can you examine the text file in a hex editor ? Although if that reveals embedded control characters, it won't explain where they came from. Can you post any more of your code?

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    It just loads items to listboxes from .txt file hosted on other server.
    If problem is about those strange characters or something, is there way to replace those also?

  19. #19
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    You would need to know if that was actually the problem, tho.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Feb 2011
    Posts
    151

    Re: StreamWriter Bad Formating?

    Quote Originally Posted by Inferrd View Post
    You would need to know if that was actually the problem, tho.
    Thanks for your help This actually was that problem :P
    This fixed it:
    Code:
    Dim cleanString1 As String = Regex.Replace(item1, "[^A-Za-z0-9]", "")

  21. #21
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: StreamWriter Bad Formating?

    Ah good. Glad you got it sorted

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width