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. text from form1 to form 2

text from form1 to form 2

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
2 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.
  • I Offline
    I Offline
    ineedhelp
    wrote on last edited by
    #1

    this may sound really basic and i'm sure it is. but throughout all of the books i've read (being self taught) i've never come across how to get the string in a text box on one form another text box/label/whatever on another form. for example: say form1 has a textbox for inputting a users name. how can i then display that name on form2 in a label control? thanks in advance cheers

    D 1 Reply Last reply
    0
    • I ineedhelp

      this may sound really basic and i'm sure it is. but throughout all of the books i've read (being self taught) i've never come across how to get the string in a text box on one form another text box/label/whatever on another form. for example: say form1 has a textbox for inputting a users name. how can i then display that name on form2 in a label control? thanks in advance cheers

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      xtremean wrote: i've never come across how to get the string in a text box on one form another text box/label/whatever on another form. That's because your thinking about the code on one form directly manipulating the controls on another form. The reason you don't see that in the books is because it's bad practice. What you should be thinking about is having public variables somewhere that both forms can access. For example, you have two forms, one has a string that needs to be passed to the second. The first form just has a label, a textbox and a button on it:

      Public Class Form1
      Inherits System.Windows.Forms.Form
       
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim newForm As New Form2
       
      newForm.strDataThisFormNeeds = TextBox1.Text
      newForm.Show()
      End Sub
      End Class

      Form1 is simple, just type something in the textbox and click a button to send the string to Form2. The code in the button click just creates a new instance of Form2 and set the public variable that Form2 exposes to the string in Form1's textbox. Form2 just has a label and a textbox on it and exposes a public string like this:

      Public Class Form2
      Inherits System.Windows.Forms.Form
       
      Public strDataThisFormNeeds As String
       
      Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
      TextBox1.Text = strDataThisFormNeeds
      End Sub
      End Class

      This is just a VERY simple example and does NOT show all the possible methods of passing data between forms! RageInTheMachine9532

      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