Member Avatar for Ray Adrian_1

Hello! I need some help regarding to this post
https://www.daniweb.com/software-development/vbnet/threads/159483/adding-item-from-text-box-to-list-box#post747691

When I add a text from textbox1 to listbox1, the textbox1 seems to fill up the whole listbox1 with the said text, the thing is the listbox1 is constantly flashing and the program goes to being not responding. My PC is kinda fast so its not its fault for the not responding problem. Any replies will surely be appreciated. :)

Pls. Post your codes what did you do and describe your problem.

Member Avatar for Ray Adrian_1

Sorry about that. Well here it is, I tried to copy the ones that were said.
I'm very sorry because I just started out in studying VB so there may be pure idiotic contents. Like I said, the problem is that when I click the add(button1) button the text from textbox1 goes to listbox1 but then the listbox1 continues to add the text and fills up the whole listbox1, it keeps flashing and when I try to press any button the program just crashes.

Public Class Form1

Private Path As String = "C:VB"
Dim datasource As New List(Of String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim newlineindex As Integer = 0
    If My.Computer.FileSystem.FileExists(Path & "asd.txt") Then
        Do Until newlineindex = -1
            datasource.Add(TextBox1.Text())
            lb1.DataSource = Nothing
            lb1.DataSource = datasource
            lb1.Refresh()

            TextBox1.Focus()
        Loop
    End If

End Sub

Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
    TextBox1.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    TextBox1.Text = "Type Your Name Here"
    TextBox2.Text = "Hello!"
    TextBox3.Text = "The Time Now is..."
End Sub

Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    If lb1.SelectedIndex > -1 Then
        datasource.RemoveAt(lb1.SelectedIndex)
        SetDatasource()

    End If
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Me.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    lb1.Items.Clear()
    My.Computer.FileSystem.OpenTextFileReader(Path & "asd.txt")

    Dim a() As String = IO.File.ReadAllLines(Path & "asd.txt")
    For Each content As String In a
        datasource.Add(content)
    Next
    lb1.DataSource = datasource
End Sub

Private Sub SetDatasource()
    lb1.DataSource = Nothing
    lb1.DataSource = datasource
    lb1.Refresh()
End Sub

Private Sub txtinput_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtinput.Enter
    txtinput.SelectAll()
End Sub

End Class

I just started out in studying VB

Why did you try to copy others codes. Nothing could be learnt by copy paste. Learn step by step then use it with full conception.

Ok!
If you simply try to add the text of TextBox1 to the ListBox lb1, you can do it simply. You have no need to create any List object.
Do it as

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    lb1.Items.Add(TextBox1.Text)
End Sub

To remove any item from listbox do it.

Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    lb1.Items.RemoveAt(lb1.SelectedIndex)
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.