动态按钮单击事件无法调用函数vb.net

时间:2013-09-15 04:25:37

标签: vb.net event-handling dynamic-controls

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

1 个答案:

答案 0 :(得分:0)

您的问题似乎是您正在初始化新控件,但您没有更改表单上的控件,甚至没有创建新表单。