0

TabLoad ()関数は、この動的ボタンをクリックしても機能していないようです。このボタン クリック イベントの目的は、テキスト ファイルからテキストを削除し、フォームを再度読み込むことです。

以下は完全なコードです。TabLoad ()関数は、最後のNextDelbtn_Clickサブルーチンにあります。

また、コードの変更に関する提案も大歓迎です。

Imports System.IO
Public Class Form1
    Dim str As String
    Dim FILE_NAME As String = "D:\1.txt"
    Dim file_exists As Boolean = File.Exists(FILE_NAME)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            MsgBox("Please enter text that you want to save", MsgBoxStyle.Information, "TOCC Error")
        Else
            str = TextBox1.Text
            Dim fs As FileStream = Nothing
            If (Not File.Exists(FILE_NAME)) Then
                fs = File.Create(FILE_NAME)
                Using fs
                End Using
            End If
            If File.Exists(FILE_NAME) Then
                Dim sw As StreamWriter
                sw = File.AppendText(FILE_NAME)
                sw.WriteLine(str)
                sw.Flush()
                sw.Close()
                TabLoad()
                TextBox1.Text = ""
            End If
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TabControl1.SelectedIndex = TabControl1.TabIndex + 1
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TabLoad()
    End Sub

    Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
        Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
        Clipboard.SetText(s)
    End Sub

    Private Sub TabLoad()
        Dim i As Integer = 1
        For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
            Dim NextLabel As New Label
            Dim Nextbtn As New Button
            Dim NextDelbtn As New Button
            NextLabel.Text = line
            Nextbtn.Text = "Copy"
            NextDelbtn.Text = "Delete"
            NextLabel.Height = 22
            Nextbtn.Width = 55
            Nextbtn.Height = 22
            NextDelbtn.Width = 55
            NextDelbtn.Height = 22
            Nextbtn.BackColor = Color.WhiteSmoke
            NextLabel.Tag = Nextbtn
            Nextbtn.Tag = NextLabel
            NextDelbtn.Tag = NextLabel
            NextDelbtn.BackColor = Color.WhiteSmoke
            NextLabel.BackColor = Color.Yellow
            TabPage2.Controls.Add(NextLabel)
            TabPage2.Controls.Add(Nextbtn)
            TabPage2.Controls.Add(NextDelbtn)
            NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
            Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
            NextDelbtn.Location = New Point(180, 10 * i + ((i - 1) * NextDelbtn.Height))
            AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
            AddHandler NextDelbtn.Click, AddressOf Me.NextDelbtn_Click
            i += 1
        Next
        File.WriteAllLines(FILE_NAME,
                   File.ReadAllLines(FILE_NAME).Where(Function(y) y <> String.Empty))
    End Sub

    Private Sub NextDelbtn_Click(sender As Object, e As EventArgs)
        Dim btn As Button = CType(sender, Button)
        Dim s As String = (btn.Tag).Text
        Dim lines() As String = IO.File.ReadAllLines(FILE_NAME)
        Using sw As New IO.StreamWriter(FILE_NAME)
            For Each line As String In lines
                If line = s Then
                    line = ""
                End If
                sw.WriteLine(line)
            Next
            sw.Flush()
            sw.Close()

            TabLoad()
        End Using
    End Sub

End Class
4

1 に答える 1

0

あなたの問題は、新しいコントロールを初期化しているように見えますが、フォーム上のコントロールを変更したり、新しいフォームを作成したりしていません。

于 2013-09-16T03:33:46.497 に答える