JohnnyCake
JohnnyCake

Reputation: 7

[VB.NET]How to add a string item, returned from another form, to a list box at a specific spot in place of another?

The idea is that there's a list-box with items, and say you want to modify an item in the middle of the list. You select that item and click "Modify" button and a new form appears with the previously selected item data from first form ready to be modified in a text-box. After modifying and clicking Ok the second form suppose to return that modified string to the first form and insert the modified string into the same spot instead of the originally selected item, so it looks like it was edited to the user.

Upvotes: 0

Views: 3795

Answers (2)

NoAlias
NoAlias

Reputation: 9193

Make sure that the form that pops up is Modal. Here is a simple example of what you can do. (This assumes your listbox items are strings and is an example for editing up to three listbox items only. If the list is going to be much larger you will want to pursue a different architecture.)

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim intTextboxCounter As Integer = 0
    For Each i As Integer In Form1.ListBox1.SelectedIndices

        Select Case intTextboxCounter
            Case 0
                TextBox1.Text = Form1.ListBox1.Items(i)
            Case 1
                TextBox2.Text = Form1.ListBox1.Items(i)
            Case 3
                TextBox3.Text = Form1.ListBox1.Items(i)
        End Select

        intTextboxCounter += 1

    Next

End Sub

When this loads it will spin through the selected list items and put its value in a textbox. To update the values...

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

    Dim intTextboxCounter As Integer = 0
    For Each i As Integer In Form1.ListBox1.SelectedIndices

        Select Case intTextboxCounter
            Case 0
                Form1.ListBox1.Items(i) = TextBox1.Text
            Case 1
                Form1.ListBox1.Items(i) = TextBox2.Text
            Case 2
                Form1.ListBox1.Items(i) = TextBox3.Text
        End Select

    Next

End Sub

Upvotes: 0

Henry H
Henry H

Reputation: 103

Edit: Translated the pseudo code to actual VB.NET code to refresh my own memory :D

string = InputBox("Enter text")
// Do whatever you want with the string
x = listBox.SelectedIndex
listBox.Items(x) = string

You can try Content in place of Text too.

Upvotes: 1

Related Questions