By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

Trying to return a selected listbox item to another form

P: n/a
Trying to return a selected listbox item to another form .tried lots of
ways; defining public variables and passing those as well as textboxes ..I'
m able to display the chosen item on it's form in a textbox and I'm able to
pass other variables to the 2nd form, but not the data I want . I should add
this is a smartdeviceapplication(if that matters; not sure)

Here's most of the test code .

(for form 1)

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

ListBox1.Items.Add("audi")

ListBox1.Items.Add("buick")

ListBox1.Items.Add("chevy")

ListBox1.Items.Add("ford")

End Sub

Public curitem As String

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

' Dim curItem As String = ListBox1.SelectedItem.ToString()

'Friend curItem As String

curItem = ListBox1.SelectedItem

TextBox1.Text = curItem

vcar = curItem

End Sub

Public Property vcar() As String ' nothing comes back from this

Get

Return ListBox1.SelectedItem

'return vcar

'return curitem

End Get

Set(ByVal Value As String)

'TextBox1.Text = Value

End Set

End Property

Public Property vn() As String 'this retuns "textbox1"

Get

Return TextBox1.Text

End Get

Set(ByVal Value As String)

TextBox1.Text = Value

End Set

End Property

Public Property t() As String 'this works

Get

Return "test"

End Get

Set(ByVal Value As String)

End Set

End Property

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim f2 As Form2

f2 = New Form2

f2.Show()

End Sub

On form2 I've got

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

TextBox1.Text = f1.vcar

TextBox2.Text = f1.vn

TextBox3.Text = f1.t

End Sub
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ok, there is a lot going on here that isn't right but it seems to me this
biggest problem you are having is that Form2 never has access to Form1. You
need to pass a reference to Form2 so it can open Form1 public members. Here
is the working idea. The key points are that when I make a new Form2, I
pass in a copy of F1. Check out the changes in the new constructor for
Form2.

Good luck
Chris

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

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

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(28, 62)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120, 95)
Me.ListBox1.TabIndex = 5
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 72)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 6
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Public curitem As String

Public ReadOnly Property GetCurrentSelectedItem() As String ' nothing
comes back from this

Get
Return ListBox1.SelectedItem
End Get

End Property

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

ListBox1.Items.Add("audi")
ListBox1.Items.Add("buick")
ListBox1.Items.Add("chevy")
ListBox1.Items.Add("ford")
ListBox1.SelectedIndex = 0

End Sub

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

Dim f2 As Form2
f2 = New Form2(Me)
f2.Show()

End Sub
End Class

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New(ByVal F As Form1)
MyBase.New()

F1 = F

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

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

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 72)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 3
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 4
Me.TextBox1.Text = "TextBox1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(176, 126)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

Dim F1 As Form1

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

TextBox1.Text = F1.GetCurrentSelectedItem()

End Sub
End Class

Nov 21 '05 #2

P: n/a
Got it working by adding "shared"
Public Shared curitem As String

and (perhaps) by moving the declaration to the tip of the file, as in :

Public Class Form1

Inherits System.Windows.Forms.Form

Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

Friend WithEvents Button1 As System.Windows.Forms.Button

Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu

Public Shared curitem As String


"baret bonden" <ar****@networks-cc.com> wrote in message
news:30*************@uni-berlin.de...
Trying to return a selected listbox item to another form .tried lots of
ways; defining public variables and passing those as well as textboxes ...I' m able to display the chosen item on it's form in a textbox and I'm able to pass other variables to the 2nd form, but not the data I want . I should add this is a smartdeviceapplication(if that matters; not sure)

Here's most of the test code .

(for form 1)

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

ListBox1.Items.Add("audi")

ListBox1.Items.Add("buick")

ListBox1.Items.Add("chevy")

ListBox1.Items.Add("ford")

End Sub

Public curitem As String

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

' Dim curItem As String = ListBox1.SelectedItem.ToString()

'Friend curItem As String

curItem = ListBox1.SelectedItem

TextBox1.Text = curItem

vcar = curItem

End Sub

Public Property vcar() As String ' nothing comes back from this

Get

Return ListBox1.SelectedItem

'return vcar

'return curitem

End Get

Set(ByVal Value As String)

'TextBox1.Text = Value

End Set

End Property

Public Property vn() As String 'this retuns "textbox1"

Get

Return TextBox1.Text

End Get

Set(ByVal Value As String)

TextBox1.Text = Value

End Set

End Property

Public Property t() As String 'this works

Get

Return "test"

End Get

Set(ByVal Value As String)

End Set

End Property

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click

Dim f2 As Form2

f2 = New Form2

f2.Show()

End Sub

On form2 I've got

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

TextBox1.Text = f1.vcar

TextBox2.Text = f1.vn

TextBox3.Text = f1.t

End Sub

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.