none
During create and write a text file which written in form load, the form is hang. RRS feed

  • Question

  • Hi,

    I have a problem (form hang) for the below code, which code written in form load.

    Me.RichTextBox1.AppendText(Environment.NewLine & "     Coordinates of reinforcement with respect to center of gravity of the shape")
                Me.RichTextBox1.AppendText(Environment.NewLine & "                        Dia            R-X-Coor        R-Y-Coor")
                Me.RichTextBox1.AppendText(Environment.NewLine & "     No.                mm               mm               mm")
                For I = 0 To NumBarcoor - 1
                    Dim line As String
                    line = String.Format(format6, I + 1, Dia(I), coorx1(I), coory1(I))
                    Me.RichTextBox1.AppendText(Environment.NewLine & line)
                Next
                Me.RichTextBox1.AppendText(Environment.NewLine & "     The above coordinates of reinforcement are not shown in the hard print while it can be")
                Me.RichTextBox1.AppendText(Environment.NewLine & "     shown in text file")
                Dim file As System.IO.StreamWriter
                If My.Settings.FileName1 = "" Then
                    file = My.Computer.FileSystem.OpenTextFileWriter("D:\Untitled.txt", True)
                Else
                    file = My.Computer.FileSystem.OpenTextFileWriter("D:\" & My.Settings.FileName1 & ".txt", True)
                End If
                file.WriteLine(TV)
                file.WriteLine(Str2007)
                file.close()
    


    And when I change the code to be

    Me.RichTextBox1.AppendText(Environment.NewLine & "     Coordinates of reinforcement with respect to center of gravity of the shape")
                Me.RichTextBox1.AppendText(Environment.NewLine & "                        Dia            R-X-Coor        R-Y-Coor")
                Me.RichTextBox1.AppendText(Environment.NewLine & "     No.                mm               mm               mm")
                For I = 0 To NumBarcoor - 1
                    Dim line As String
                    line = String.Format(format6, I + 1, Dia(I), coorx1(I), coory1(I))
                    Me.RichTextBox1.AppendText(Environment.NewLine & line)
                Next
                Me.RichTextBox1.AppendText(Environment.NewLine & "     The above coordinates of reinforcement are not shown in the hard print while it can be")
                Me.RichTextBox1.AppendText(Environment.NewLine & "     shown in text file")
                Dim file As System.IO.StreamWriter
                If My.Settings.FileName1 = "" Then
                    file = My.Computer.FileSystem.OpenTextFileWriter("D:\\Untitled.txt", True)
                Else
                    file = My.Computer.FileSystem.OpenTextFileWriter("D:\\" & My.Settings.FileName1 & ".txt", True)
                End If
                file.WriteLine(TV)
                file.WriteLine(Str2007)



    I still have the same problem (form hang).

    Kind Regards,


    Hany Metry






    • Edited by Hany Metry Tuesday, November 12, 2019 9:10 AM add
    Tuesday, November 12, 2019 7:51 AM

Answers

  • Hi,

    When I remove the below code line,

    file = My.Computer.FileSystem.OpenTextFileWriter("D:\" & My.Settings.FileName1 & ".txt", True)

    or make file name untitled, then the problem of Hang is solved.

    Kind Regards,


    Hany Metry

    • Marked as answer by Hany Metry Wednesday, November 13, 2019 6:24 AM
    Wednesday, November 13, 2019 6:24 AM

All replies

  • Hi Hany,
    please, show how you declare and load values in NumBarcoor, Dia, coorx1, coory1, TV and Str2007.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, November 12, 2019 8:31 AM
  • Hi,

    You can try my code as follows:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Write
            Dim file As StreamWriter
            If My.Settings.FileName1 = "" Then
                file = My.Computer.FileSystem.OpenTextFileWriter("D:\Untitled.txt", True)
            Else
                file = My.Computer.FileSystem.OpenTextFileWriter("D:\" & My.Settings.FileName1 & ".txt", True)
            End If
            file.WriteLine("TV")
            file.WriteLine("Str2007")
            file.Close()
    
            'Read
            Dim line As String = ""
            Using str As StreamReader = New StreamReader("D:\Untitled.txt")
                TextBox1.Text = str.ReadToEnd
            End Using
        End Sub

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, November 12, 2019 8:50 AM
    Moderator
  • Hi,

    When I remove the below code line,

    file = My.Computer.FileSystem.OpenTextFileWriter("D:\" & My.Settings.FileName1 & ".txt", True)

    or make file name untitled, then the problem of Hang is solved.

    Kind Regards,


    Hany Metry

    • Marked as answer by Hany Metry Wednesday, November 13, 2019 6:24 AM
    Wednesday, November 13, 2019 6:24 AM