473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trying to return a selected listbox item to another form

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
2 4840
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Geir Baardsen | last post by:
Hi! I have the following headpuzzle: On a form (frmAgenda) I have a calendar control and a listbox. When user clicks date in calendar, the listbox will show some information that will be...
5
by: John O'Donnell | last post by:
Hello on my asp.net code I load up a listbox on the page_load method. The problem is that while the items I have added in the page_load do appear when i run the page, i am unable to get the...
1
by: Karen Grube | last post by:
Hi! I'm using a standard server side ASP.Net listbox control on a web form. The page is basically various shades of green. The listbox itself has a pale green background and forest green text...
7
by: Brian Henry | last post by:
I have a list box bound to a data set, of course the datasource,datamember, and valuemember are set, i tell it selecteditem = -1 to make nothing selected, select mode is set to only one item at a...
1
by: acord | last post by:
Hi, I am having problem to get a value of the selected item from a dropdown listbox. Here is the JS function; function getSelectedItem(objSelect) { alert("in getSelectedItem"); alert...
2
by: John | last post by:
I have a listbox that is databound when my form loads. A user can then select and option using a drop down box. When the user selects an option the corresponding items in the listbox gets selected....
3
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 "item 2" 2 "item 3" and so on. I keep...
4
by: rn5a | last post by:
I am binding a DropDownList with records existing in a database table. I want to add an extra item *SELECT COMPANY* at index 0 so that by default, it gets selected. This is how I tried it but the...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.