Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. access to another forms objects

access to another forms objects

Scheduled Pinned Locked Moved Visual Basic
winformsquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hakanaktan
    wrote on last edited by
    #1

    Hi, I have two windows forms.User opens the first form(form1) and clicks "open" button then a new form opens(form2). He makes a choice in form2 .I need to add some string to listBox that is on form1 when user clicks button on the form2. Both forms are open at this time. If i use "Dim myForm As New Form1" it makes the change i need but it creates a new form1. would someone send some syntax for that? Thanx in advance.. --junior coder--

    P 1 Reply Last reply
    0
    • H hakanaktan

      Hi, I have two windows forms.User opens the first form(form1) and clicks "open" button then a new form opens(form2). He makes a choice in form2 .I need to add some string to listBox that is on form1 when user clicks button on the form2. Both forms are open at this time. If i use "Dim myForm As New Form1" it makes the change i need but it creates a new form1. would someone send some syntax for that? Thanx in advance.. --junior coder--

      P Offline
      P Offline
      prathiba_naresh
      wrote on last edited by
      #2

      You can pass the form1 as reference to form2 and when the user clicks button on form2 set the string in form1's reference. That is, When displaying form2 from form1 you would say, Dim form2 as new form2(form1) In the Form2's constructor(Public Sub New) say Public Sum New(byref Form1 as form1) Hope this helps.

      H 1 Reply Last reply
      0
      • P prathiba_naresh

        You can pass the form1 as reference to form2 and when the user clicks button on form2 set the string in form1's reference. That is, When displaying form2 from form1 you would say, Dim form2 as new form2(form1) In the Form2's constructor(Public Sub New) say Public Sum New(byref Form1 as form1) Hope this helps.

        H Offline
        H Offline
        hakanaktan
        wrote on last edited by
        #3

        Here is form1 code: Public myform2 As New form2 I defined form2 as global variable Here is form2 code when you hit a button: Dim f1 As New Form1 f1.Show() f1 = Me.Owner f1.textBox1.Text="something else" But it does not work :confused: here is error message: An unhandled exception of type 'System.NullReferenceException' occurred in TestForms.exe Additional information: Object reference not set to an instance of an object. --junior coder--

        P 1 Reply Last reply
        0
        • H hakanaktan

          Here is form1 code: Public myform2 As New form2 I defined form2 as global variable Here is form2 code when you hit a button: Dim f1 As New Form1 f1.Show() f1 = Me.Owner f1.textBox1.Text="something else" But it does not work :confused: here is error message: An unhandled exception of type 'System.NullReferenceException' occurred in TestForms.exe Additional information: Object reference not set to an instance of an object. --junior coder--

          P Offline
          P Offline
          prathiba_naresh
          wrote on last edited by
          #4

          I assume you have a button in form1 to invoke form2. So put this code in button_click event of the form1 where you call the form2. Form1 Code ---------- Dim myform2 As New Form2(Me) myform2.ShowDialog() In form2 Declare a Variable Dim prevform As Form1 If you click on the + sign near the Windows Form Designer generated code,You will see the Public Sub New. it should look like this. Public Sub New(ByRef form1 As Form1) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() prevform = form1 'Add any initialization after the InitializeComponent() call End Sub In the Form2 button_click (where you want to call form1, paste this code. prevform.TextBox1.Text = "something else" Me.Hide() This should work fine.

          H 1 Reply Last reply
          0
          • P prathiba_naresh

            I assume you have a button in form1 to invoke form2. So put this code in button_click event of the form1 where you call the form2. Form1 Code ---------- Dim myform2 As New Form2(Me) myform2.ShowDialog() In form2 Declare a Variable Dim prevform As Form1 If you click on the + sign near the Windows Form Designer generated code,You will see the Public Sub New. it should look like this. Public Sub New(ByRef form1 As Form1) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() prevform = form1 'Add any initialization after the InitializeComponent() call End Sub In the Form2 button_click (where you want to call form1, paste this code. prevform.TextBox1.Text = "something else" Me.Hide() This should work fine.

            H Offline
            H Offline
            hakanaktan
            wrote on last edited by
            #5

            thanx it works fine..:-D --junior coder--

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups