动态按钮点击事件无法调用函数vb.net [英] Dynamic button click event not able to call a function vb.net

查看:146
本文介绍了动态按钮点击事件无法调用函数vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The TabLoad() function doesn't seem to be working on clicking of this dynamic button. The aim of this button click event is that it deletes text from a text file and loads the form again.

以下是完整的代码。 TabLoad()功能位于最后的 NextDelbtn_Click 子句。

Below is the complete code. The TabLoad() function is in the NextDelbtn_Click sub at the end.

还有关于更改代码的任何建议非常感谢。

Also any suggestion regarding change of code is appreciated.

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


推荐答案

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

Your problem appears to be that you're initializing new controls but you're not changing the controls on the form or even creating a new form.

这篇关于动态按钮点击事件无法调用函数vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆