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



TabLoad() 函数似乎无法在单击此动态按钮时工作。此按钮单击事件的目的是从文本文件中删除文本并再次加载表单。

下面是完整的代码。TabLoad() 函数位于末尾的 NextDelbtn_Click sub 中。

此外,非常感谢有关代码更改的任何建议。

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

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

最新更新