Dynamic button click event not able to call a function vb.net

1.2k Views Asked by At

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.

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
1

There are 1 best solutions below

2
On

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.