Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

list box to text box 1

Status
Not open for further replies.

hendrikbez

Technical User
Nov 3, 2003
54
ZA
I have to forms. a.vb and b.vb. on A.vb I have a text box "a.text", and on form b.vb I have a listbox with a few names. I want to know how do I chosse or click a name from the listbox in b.vb and when I have click the nameI want, how do I get it to show in the a.text box in a.vb.

Can you plase walk me true this

Thanks

How will you spend eternity - Smoking or Non Smoking?
 
Which form loads first, a or b?
or you use a module with the main sub?
 
Never mind, here it is:

NOTICE:
Your a and b forms are mine form1 and form2.
Form1 has a textbox and a button (name=select).
Form2 a listbox and a button. The button's property dialogresult is set to OK.

CODE in select button in form1:

Private Sub Select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Select.Click
Dim f As New Form2
f.ShowDialog()

If f.DialogResult = DialogResult.OK Then
Me.TextBox1.Text = f.ListBox1.Text
End If
End Sub

EXPLANATION:
In form1 i have a button (Select). When clicked, it executes the above code. It load a new form2 that has the listbox with the names.


Ok?
 
TipGiver's solution requires that the second form (the one with the listbox) is opened as a dialog (modal window).

This question has in fact been answered several times on this forum, but a couple of alternative solutions that do not require .ShowDialog are as follows:

In each case Form1 is the calling form (ie has the textbox)

(1)
Form2 knows about Form1

Form1:

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

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

  End Sub

Form2:

Code:
  Private CallingForm As Form1

  Public Sub New(ByVal Caller As Form1)

    Me.new()
    CallingForm = Caller

  End Sub

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

    Me.Close()

  End Sub

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    CallingForm.TextBox1.Text = ListBox1.SelectedItem.ToString

  End Sub


(2)
Form3 (I used the same project so the second form is form3, the button on form1 is button2 and the textbox is textbox2) raises an Event to tell anyone who is interested (Form1) that the listbox selection has changed.

Form1:
Code:
  Private Sub Form3ListBoxChanged(ByVal Value As String)

    TextBox2.Text = Value

  End Sub

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

    f3 = New Form3
    AddHandler f3.CurrentListBoxItem, AddressOf Form3ListBoxChanged
    f3.Show()

  End Sub

Form3:
Code:
  Public Event CurrentListBoxItem(ByVal Value As String)

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

    Me.Close()

  End Sub

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    RaiseEvent CurrentListBoxItem(ListBox1.SelectedItem.ToString)

  End Sub


There are obviously several variations of these two (and Tipgiver's) solutions

Hope this helps.



[vampire][bat]
 
Thanks for you reply, I wil try it

Greetings
Hendrik

How will you spend eternity - Smoking or Non Smoking?
 
Earthanddire

I have try to use the sample that you have given me.

form1
Code:
 Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Piconderwyserlys.Click

        'from tex tips
        Dim onderwyserlys As New Onderwyserlist
        onderwyserlys.Show()
    End Sub

From2

Code:
Imports System.Data.OleDb
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Collections
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.Threading

Public Class Onderwyserlist

    Inherits System.Windows.Forms.Form

    'from tex tips
    Private callingform As SondagSkool

#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 Label1 As System.Windows.Forms.Label
    Friend WithEvents ok As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Onderwyserlist))
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.ok = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(0, 48)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(240, 173)
        Me.ListBox1.TabIndex = 223
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Verdana", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.Label1.Location = New System.Drawing.Point(40, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(160, 23)
        Me.Label1.TabIndex = 224
        Me.Label1.Text = "Kies onderwyser"
        '
        'ok
        '
        Me.ok.Location = New System.Drawing.Point(80, 240)
        Me.ok.Name = "ok"
        Me.ok.TabIndex = 225
        Me.ok.Text = "Kies"
        '
        'Onderwyserlist
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(224, Byte), CType(192, Byte))
        Me.ClientSize = New System.Drawing.Size(256, 270)
        Me.Controls.Add(Me.ok)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.ListBox1)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Onderwyserlist"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Onderwyserlys"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim DB As OleDb.OleDbConnection
    Dim Dc As New OleDb.OleDbCommand
    Dim Da As New OleDb.OleDbDataAdapter
    Dim Ds As New DataSet
    Dim Nextb As Long
    Dim PrevB As Long
    Dim TotalC As Long

  
    Public Sub New(ByVal caller As SondagSkool)
        Me.New()
        callingform = caller
    End Sub
    
    Private Sub ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ok.Click
        Me.Close()
    End Sub

        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        callingform.txtonnie.Text = ListBox1.SelectedItem.ToString
    End Sub

I can click on form1 buton and it goes to the next form, when I click on the name that I want on the listbox in form2 and then click on the button I am getting this error:
"An unhandled exception of type 'System.NullReferenceException' Occured in Sondagskool.exe"

It happens at the listbox_selectedindexChanged
callingform.txtonnie.Text = ListBox1.SelectedItem.ToString

What did I do wrong

Hendrik


How will you spend eternity - Smoking or Non Smoking?
 
I think the problem is here:

Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Piconderwyserlys.Click

'from tex tips
Dim onderwyserlys As New Onderwyserlist
onderwyserlys.Show()
End Sub


Try changing it to:

Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Piconderwyserlys.Click

'from tex tips
Dim onderwyserlys As New Onderwyserlist(Me)
onderwyserlys.Show()
End Sub

In Onderwyserlist there are 2 Constructors(New), one is the default - in the designer's region which takes no parameters, the other is in the code you added which takes a Form of type SondagSkool as a parameter. Its this second constructor that you must use as this initialises the variable callingform.

Hope this helps.

[vampire][bat]
 
Earthandfire

Thanks it is working now

How will you spend eternity - Smoking or Non Smoking?
 
Earthhandfire

It is working now, but now every time I open my main form and click on the buton to open the second form,it opens a new window, and when i click to go back to my 1st form, it open a new window again, after a while I saw that I have 10 windows open, how can I stop it to open a new windpow evry time.

Thanks

How will you spend eternity - Smoking or Non Smoking?
 
A couple of options:

Take the declaration for the forms outside of the subs ie instead of:

Sub ButtonCLick etc
dim f2 as new form2(Me)
etc
etc
End Sub

use:

outside of a sub
Private f2 as New Form2(Me)

and then when you need to show it
Sub ButtonCLick etc
f2.Show
etc
etc
End Sub

is probably the easiest method



Hope this helps.


[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top