cancel
Showing results for 
Search instead for 
Did you mean: 

help with winform listbox in vb

Valued Contributor
Valued Contributor

I am new to listboxes in winforms and have having trouble determining what is selected from the list box. I have it displayed and able to get information into it. I am hoping I can select multiple lines and then press a button and have the selected items added to a list. I run it as a journal okay, but then I get an error.

 

 

 

 error.jpg

 

 

the code I have for the button action is the following. I was trying to write in a file only to trouble shoot. I commented some lines out. this is the whole winform code

 

Public Class OptionPrompt
    Private _frmcurrentnotestring As String
    Public Property currentnotestring() As String
        Get
            Return _frmcurrentnotestring
        End Get
        Set(ByVal value As String)
            _frmcurrentnotestring = value
        End Set
    End Property

    Private _canceled As Boolean = False
    Public ReadOnly Property Canceled() As Boolean
        Get
            Return _canceled
        End Get
    End Property



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






    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        _canceled = True
        Me.Close()
    End Sub

    Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
        ' Get the currently selected item in the ListBox.
        'Dim curItem As String = listBox1.SelectedItem.ToString()

        Dim writefile As System.IO.StreamWriter
        writefile = My.Computer.FileSystem.OpenTextFileWriter("M:\NX_info\tempfiletest.txt", True)

        writefile.WriteLine(listBox1.SelectedItem(0).ToString)
 

        writefile.Close()




    End Sub

    Private Sub btnOpenFile_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenFile.Click



        Dim writefile As System.IO.StreamWriter
        writefile = My.Computer.FileSystem.OpenTextFileWriter("M:\NX_info\tempfiletest.txt", True)

        writefile.WriteLine(listBox1.SelectedItem(0).ToString)


        writefile.Close()


        'Dim curItem As String = listBox1.SelectedItem.ToString
        'Dim x As Integer
        'For x = Me.listBox1.SelectedIndex - 1 To 0 Step -1
        'Me.listBox1.Items(x)
        'Dim p = Process.Start("Notepad++.exe", listBox1.Items(x))
        'Next x

    End Sub



End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class OptionPrompt
    Inherits System.Windows.Forms.Form

    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()



        'create and get data for comboboxes
        Dim filename As String = "M:\NX_info\tempfile.txt"
        Dim SearchList As New List(Of String)

        Dim onlist As String = "NO"
        Dim linestring As String = Nothing
        Dim linestringparts() As String
        Dim sr As StreamReader = File.OpenText(filename)
        Try
            Do While sr.Peek >= 1
                linestring = sr.ReadLine
                SearchList.Add(linestring)
            Loop
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        sr.Close()



        Me.btnCancel = New System.Windows.Forms.Button()
        Me.btnOpenFile = New System.Windows.Forms.Button()
        Me.btnOpenList = New System.Windows.Forms.Button()


        Me.textLabel = New System.Windows.Forms.Label()
        Dim tabHeight As Integer = 350
        Dim TabWidth As Integer = 600

        Dim listBox1 As New ListBox()
        listBox1.Size = New System.Drawing.Size(TabWidth, tabHeight)
        listBox1.Location = New System.Drawing.Point(10, 10)
        Me.Controls.Add(listBox1)
        listBox1.MultiColumn = False
        ' Set the selection mode to multiple and extended.
        listBox1.SelectionMode = SelectionMode.MultiExtended

        ' Shutdown the painting of the ListBox as items are added.
        listBox1.BeginUpdate()
        Dim FileItemParts() As String
        For Each FileItem As String In SearchList
            FileItemParts = FileItem.split("\")
            'listBox1.Items.Add(FileItemParts(FileItemParts.Length - 1))
            listBox1.Items.Add(FileItem)
        Next
        ' Allow the ListBox to repaint and display the new items.
        listBox1.EndUpdate()






        'buttons
        Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnCancel.Location = New System.Drawing.Point(230, TabHeight + 30)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(80, 30)
        Me.btnCancel.TabIndex = 0
        Me.btnCancel.Text = "Cancel"
        Me.btnCancel.UseVisualStyleBackColor = True
        '        
        Me.btnOpenFile.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnOpenFile.Location = New System.Drawing.Point(30, TabHeight + 30)
        Me.btnOpenFile.Name = "btnOpenFile"
        Me.btnOpenFile.Size = New System.Drawing.Size(80, 30)
        Me.btnOpenFile.TabIndex = 0
        Me.btnOpenFile.Text = "Open File"
        Me.btnOpenFile.UseVisualStyleBackColor = True
        '      
        Me.btnOpenList.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.btnOpenList.Location = New System.Drawing.Point(130, TabHeight + 30)
        Me.btnOpenList.Name = "btnOpenList"
        Me.btnOpenList.Size = New System.Drawing.Size(80, 30)
        Me.btnOpenList.TabIndex = 0
        Me.btnOpenList.Text = "Open List"
        Me.btnOpenList.UseVisualStyleBackColor = True


        'label
        Me.textLabel.Location = New System.Drawing.Point(25, 25)
        Me.textLabel.Name = "text"
        Me.textLabel.Size = New System.Drawing.Size(TabWidth - 50, 200)
        Me.textLabel.TabIndex = 2
        Me.textLabel.Text = "FIND WHAT: "
        Me.textLabel.TextAlign = System.Drawing.ContentAlignment.TopLeft
        Me.textLabel.Font = New System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.italic)



        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.CancelButton = Me.btnCancel
        Me.ClientSize = New System.Drawing.Size(TabWidth + 20, TabHeight + 80)
        'Me.BackColor = System.Windows.Forms.Color.FromArgb(255, 255, 255)
        Me.Padding = New System.Windows.Forms.Padding(20)

        '
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        'Me.Controlbox = False
        Me.Name = "FormValueStream"
        Me.Text = "FIND AND REPLACE"
        Me.ResumeLayout(False)
        'Me.PerformLayout()

        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnOpenFile)
        Me.Controls.Add(Me.btnOpenList)
        Me.Controls.Add(Me.textLabel)


    End Sub
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOpenList As System.Windows.Forms.Button
    Friend WithEvents btnOpenFile As System.Windows.Forms.Button
    Friend WithEvents textLabel As System.Windows.Forms.Label
    Friend WithEvents listbox1 As System.Windows.Forms.Listbox


End Class

 

 

Using NX 8.0.3.4
3 REPLIES

Re: help with winform listbox in vb

Siemens Phenom Siemens Phenom
Siemens Phenom

System.Windows.Forms.ListBox.SelectedItem() returns a single object. listBox1.SelectedItem(0) is the reference that is not instantiated (since it is not an array).

 

To get the multi-select list, use System.Windows.Forms.ListBox.SelectedItems() to return a System.Windows.Forms.ListBox.SelectedObjectCollection

Re: help with winform listbox in vb

Valued Contributor
Valued Contributor

tried to add an "s" after items and I still get that issue. could it be something else.

Using NX 8.0.3.4

Re: help with winform listbox in vb

Siemens Phenom Siemens Phenom
Siemens Phenom

It's not that simple. SelectedItem() returns an object. SelectedItems returns a collection (not an array of objects).

 

Since this issue is about winforms (and not NXOpen api), you might be better off posing the question in a .NET discussion forum.