[VB.NET] Passage de variables entre forms

Signaler
Messages postés
50
Date d'inscription
samedi 28 février 2004
Statut
Membre
Dernière intervention
6 juin 2006
-
playmo420
Messages postés
50
Date d'inscription
samedi 28 février 2004
Statut
Membre
Dernière intervention
6 juin 2006
-
Salut
J'immagine que cette question a deja ete posée sur ce forum mais je n'ai pas reussi a trouver la réponse...

Voila mon probleme :
J'ai deux forms. La premiere contient un dataSet que je souhaite remplir à partir d'informations que l'utilisateur entre à partir de la deuxième form (modale). Si je veux utiliser une variable de la deuxième form à partir de la première, pas de problème :

Dans Form1 :
Public Form2 as new Form2()
...
Form2.maVariable = qqchose


Mais je ne réussi pas à le faire dans le sens inverse (form2 -> form1)...

Si vous pouviez me donner un ptit coup de main parceque j'ai essayé pas mal de trucs mais là je bloque...

7 réponses

Messages postés
70
Date d'inscription
mercredi 11 juin 2003
Statut
Membre
Dernière intervention
2 octobre 2006
1
C'est simple
Il suffit de modifier la creation de la form dans la region " Windows Form Designer generated code " de ta form2

#Region " Windows Form Designer generated code "

Public Sub New(Byref VarATrans As String)
MyBase.New()

dans ta form1
dim vartrans as string
Dim frmDetail As frmForm2 = New frmForm2 (vartrans )
frmDetail.ShowDialog(Me)

la variable VarATrans est alors transmise et disponible ta form

glplpmat
Messages postés
50
Date d'inscription
samedi 28 février 2004
Statut
Membre
Dernière intervention
6 juin 2006

Merci beaucoup, je l'aurais pas trouvé tout seul ;)
Messages postés
50
Date d'inscription
samedi 28 février 2004
Statut
Membre
Dernière intervention
6 juin 2006

J'ai essayé mais y'a un problème... Je pense que la variable est bien accessible (le menu contextuel s'ouvre avec la liste des méthodes de l'object passé en paramètre), mais lors de l'exécution (au tout début) le debugger just in time lance me montre une erreur : exception system.NullReferenceException...
Une idée ?
Messages postés
70
Date d'inscription
mercredi 11 juin 2003
Statut
Membre
Dernière intervention
2 octobre 2006
1
Voici le code de deux forms qui se partage des variables

Form1

Public Class Form2
Inherits System.Windows.Forms.Form
Dim aaa As Form1

#Region " Windows Form Designer generated code "
Private deMere As String
Public Sub New(ByRef Atran As String)
MyBase.New()


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

'Add any initialization after the InitializeComponent() call
deMere = Atran

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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(72, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(72, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(136, 120)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = deMere
aaa = Me.Owner
End Sub

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

aaa.retour = "AAA"
Me.Close()

End Sub
End Class

Form2:

Public Class Form2
Inherits System.Windows.Forms.Form
Dim aaa As Form1

#Region " Windows Form Designer generated code "
Private deMere As String
Public Sub New(ByRef Atran As String)
MyBase.New()


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

'Add any initialization after the InitializeComponent() call
deMere = Atran

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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(72, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(72, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(136, 120)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = deMere
aaa = Me.Owner
End Sub

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

aaa.retour = "AAA"
Me.Close()

End Sub
End Class

Essaie sur cette base
A+

glplpmat
Messages postés
50
Date d'inscription
samedi 28 février 2004
Statut
Membre
Dernière intervention
6 juin 2006

Merci mais t'as mis 2 fois le code de la form2 ;)
Messages postés
70
Date d'inscription
mercredi 11 juin 2003
Statut
Membre
Dernière intervention
2 octobre 2006
1
Désolé

Revoici les sources

FORM 1 ------------------------------------------------------------

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 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(184, 72)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(64, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(192, 136)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(56, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "Exple"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region
Public retour As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim transf As String
transf = Me.TextBox1.Text
Dim frmDetail As Form2 = New Form2(transf)
frmDetail.ShowDialog(Me)
Me.TextBox1.Text = retour

End Sub

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

End Sub
End Class

Form 2 -------------------------------------------------------------

Public Class Form2
Inherits System.Windows.Forms.Form
Dim aaa As Form1

#Region " Windows Form Designer generated code "
Private deMere As String
Public Sub New(ByRef Atran As String)
MyBase.New()


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

'Add any initialization after the InitializeComponent() call
deMere = Atran

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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(72, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(72, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(136, 120)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = deMere
aaa = Me.Owner
End Sub

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

aaa.retour = "AAA"
Me.Close()

End Sub
End Class

glplpmat
Messages postés
50
Date d'inscription
samedi 28 février 2004
Statut
Membre
Dernière intervention
6 juin 2006

Ca y est, j'ai (enfin) réussi !!! Merci beaucoup pour ton exemple !!! :)