Results 1 to 3 of 3

Thread: [RESOLVED] sending values to main form from other forms

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Cochin, India
    Posts
    350

    Resolved [RESOLVED] sending values to main form from other forms

    I declared a public variable in my main form(startup form). then the user clicks on a button and a new form with a textbox appears. the user enters some string in the textbox and clicks a button to close the form. Now i have to show that string in the main form. how?
    I cant write form1.label1.text=strresult because
    'Object reference not set to an instance of the object'

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: sending values to main form from other forms

    you need to modify the New() constructor of the ' other ' forms or classes

    eg:
    VB Code:
    1. Public Class Form2
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     '/// this will be used to access it...
    7.     Private frmMain As Form1
    8.  
    9.     Public Sub New(ByVal f As Form)
    10.         MyBase.New()
    11.  
    12.         frmMain = DirectCast(f, Form1)
    13.  
    14.         'This call is required by the Windows Form Designer.
    15.         InitializeComponent()
    16.  
    17.         'Add any initialization after the InitializeComponent() call
    18.  
    19.     End Sub
    20. '/// rest of Form2's stuff here...
    21. '/// then on a sub ( ie:  a button click ) ...
    22.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    23.         '/// pass some data to Form1 ( the main form )
    24.         frmMain.TextBox1.Text = "some stuff passed to Form1 !!!"
    25.     End Sub

    to create Form2 ( from form1 when launching it ) ...
    VB Code:
    1. '/// when creating Form2 you can now pass the main form ( ie: this form ) as part of the constructor.
    2.         Dim frm2 As New Form2(Me)
    3.         frm2.Show()
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Cochin, India
    Posts
    350

    Re: sending values to main form from other forms

    thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width