Question Variable from form1 to an other form

groover

Member
Joined
Oct 10, 2009
Messages
23
Programming Experience
1-3
I want to pass variables from one form to an other.

I googled a lot but nothing seems to work for me.

I am a newbie so code example is importand to me

The variables i I need in the second form( called" Hulsview") are :" hulsbreedte" and" hulslengte".

To check if the varables are passed I added a textbox in the second form,called" hulsview" to check the variable" hulsbreedte".

The value selected in the combobox on on form1" hulsbreedte = HulsBreedte1.SelectedValue" returns always 0 in the textbox on de second form.

Here is te code form1:

VB.NET:
Imports System.Drawing.Drawing2D
Public Class Form1
    Inherits System.Windows.Forms.Form

    Public hlb As Point 'not used
    Public hrb As Point 'not used
    Public hlo As Point 'not used
    Public hro As Point 'not used
    Public hulsbreedte As Integer
    Public hulslengte As Integer
    Public OK As Integer 'not used


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


        Me.Height = Screen.PrimaryScreen.Bounds.Height
        Me.Width = Screen.PrimaryScreen.Bounds.Width / 2
    End Sub

    Private Sub ExitButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton2.Click
        Application.Exit()
    End Sub

    Private Sub HulsBreedte1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HulsBreedte1.SelectedIndexChanged
        hulsbreedte = HulsBreedte1.SelectedValue
    End Sub
    Private Sub HulsLengte1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HulsLengte1.SelectedIndexChanged
        hulslengte = HulsLengte1.SelectedValue
        HulsView.Show() 'second form

    End Sub
End Class

And here the code for the second form:
VB.NET:
Imports System.Drawing.Drawing2D

Public Class HulsView
    Inherits System.Windows.Forms.Form
    Public hulsbreedte As Integer = Form1.hulsbreedte
    Public hulslengte As Integer = Form1.hulslengte
    Public ScrXY As Point
    Public Centerpoint As Point

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

        Call ScreenResolution()

        Me.Location = ScrXY
        Me.Width = Screen.PrimaryScreen.Bounds.Width / 2
        Me.Height = Screen.PrimaryScreen.Bounds.Height
    End Sub
    Public Function ScreenResolution()
        Dim ScrX As Double = Screen.PrimaryScreen.Bounds.Width

        ScrXY.X = ScrX / 2
        ScrXY.Y = 0
        Return ScrXY
    End Function
    Public Function FormCenter()
        Dim FrmX As Double = Me.Location.X + Me.Bounds.Width
        Dim FrmY As Double = Me.Location.Y + Me.Bounds.Height
        Centerpoint.X = FrmX / 2
        Centerpoint.Y = FrmY / 2
        Return Centerpoint
    End Function
    Private Sub HulsView_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim Hulsoutline As New Drawing.Pen(Brushes.LightGray, 8)
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality
        Call FormCenter()

        Dim point1 As New Point(point1.X, point1.Y)

        point1.X = Centerpoint.X - hulsbreedte / 2
        point1.Y = Centerpoint.Y - hulslengte / 2

        Dim point2 As New Point(point2.X, point2.Y)
        point2.X = Centerpoint.X + hulsbreedte / 2
        point2.Y = Centerpoint.Y - hulslengte / 2

        Dim point3 As New Point(point3.X, point3.Y)
        point3.X = Centerpoint.X + hulsbreedte / 2
        point3.Y = Centerpoint.Y + hulslengte / 2

        Dim point4 As New Point(point4.X, point4.Y)
        point4.X = Centerpoint.X - hulsbreedte / 2
        point4.Y = Centerpoint.Y - hulslengte / 2

        Dim curvePoints As Point() = {point1, point2, point3, point4}
7:
        e.Graphics.DrawPolygon(Hulsoutline, curvePoints)
        TextBox1.Text = Form1.hulsbreedte.ToString''
        Me.Refresh()
    End Sub
End Class



I am using vb express 2010.



Thanks in advance,



Groover
 
First up, keep in mind that you don't actually pass variables from one for to another. The first form will have a variable and that variable will have a value. You can pass that value to a variable in the second form. The variables are distinct, but they will have the same value.

How do you usually pass data from one object to another, e.g. how do you pass a String into a TextBox for display? You set the Text property, right? That is the most common way to get data into an object: set a property. Your second form is an object, so your first form can set a property on the second form to pass data in. It's your own custom data so it's up to you to write your own custom property. Inside the property setter, you assign the data to the appropriate variable.

Another option is to pass data to the constructor of the second form. The advantage this has over a property is that it forces anyone who creates an instance to pass in the data, while a property is optional. Again, you would have to write your own constructor in the second form.

There's a link in my signature on using multiple forms. You should follow it and read the whole thing.
 
Back
Top