Results 1 to 6 of 6

Thread: Copying text to another form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    6

    Copying text to another form

    I am relatively new to Visual Basic; in the sense that I have had it for a while, but haven't used it enough to actual get good and confident with it.

    I am trying to create a program for the 3D Space Simulator "Celestia". You can create stars through Notepad on it, but I wanted to see if I can create a 'Star builder'.

    So I have 2 forms:
    1# Where you put in all the information needed
    2# Where it gets automatically copied and saved

    I can't seem to get the text to copy though.

    I have 7 textbox's on form 1, with a button named 'Build' which has the code 'Form2.Show()' on it's click event.

    The 2nd form has a RichTextBox on it, which I have put the code:

    Code:
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.RichTextBox1.Text = Form1.TextBox1.Text & vbNewLine &
            Me.RichTextBox1.Text = "{" & vbNewLine &
            Me.RichTextBox1.Text = "RA " + Form1.TextBox2.Text & vbNewLine &
            Me.RichTextBox1.Text = "Dec " + Form1.TextBox3.Text & vbNewLine &
            Me.RichTextBox1.Text = "Spectral Type " + Form1.TextBox4.Text & vbNewLine &
            Me.RichTextBox1.Text = "Distance " + Form1.TextBox5.Text & vbNewLine &
            Me.RichTextBox1.Text = "AppMag " + Form1.TextBox6.Text & vbNewLine
            If Form1.TextBox7.Text = "" Then
                Me.RichTextBox1.Text = "}" & vbNewLine
                End
            Else
                Me.RichTextBox1.Text = "Radius " + Form1.TextBox7.Text & vbNewLine &
                Me.RichTextBox1.Text = "}"
            End If
        End Sub
    I have a feeling the answer will be obvious..but I really can't figure it out for myself.

    Thanks,
    Dryda

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Copying text to another form

    I would use the following method in that you assign infor to RichTextBox1 in Form2 in the calling form.

    Code:
    Dim f As New Form2
    
    Try
        f.RichTextBox1.Text = TextBox1.Text & vbNewLine
        '
        ' do the rest of your assigments
        '
        f.ShowDialog()
    Finally
        f.Dispose()
    End Try

  3. #3
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Copying text to another form

    vb Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.  
    3.         Dim Var_FromT1 As String = "Some value1" ' lets say these are the values
    4.         Dim Var_FromT2 As String = "Some value2" ' from your text boxes
    5.         Dim Var_FromT3 As String = "Some value3" ' in form 1
    6.         Dim Var_FromT4 As String = "Some value4" ' & u are passing these values on to
    7.         Dim Var_FromT5 As String = "Some value5" ' form2
    8.         Dim Var_FromT6 As String = "Some value6" ' create a public sub in side the form2
    9.         ' as fillRTB Refer to Sub routine FillRTB()
    10.         Dim StringCollector As New System.Text.StringBuilder
    11.  
    12.         With StringCollector
    13.             .Append(Var_FromT1).AppendLine()
    14.             .Append(Var_FromT2).AppendLine()
    15.             .Append(Var_FromT3).AppendLine()
    16.             .Append(Var_FromT4).AppendLine()
    17.             .Append(Var_FromT5).AppendLine()
    18.             .Append(Var_FromT6).AppendLine()
    19.         End With
    20.  
    21.         Dim New_Form As New Form2
    22.         With New_Form
    23.             .FillRTB(StringCollector.ToString())
    24.             .Show()
    25.         End With
    26.  
    27.     End Sub
    vb Code:
    1. 'This will be a sub routine in your another form
    2. 'on which you are having Ritch text box
    3. ' u can do the same even by creating some properties
    4. ' in form2 as well
    5. Public Sub FillRTB(ByVal Imported_Variables As String)
    6.         Me.RichTextBox1.Text = Imported_Variables
    7.     End Sub
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    6

    Re: Copying text to another form

    @Make me rain

    Thank you so much That worked perfectly!

    @Kevininstructor Thank you for the help Yours didn't seem to copy it all down, but that code did help neaten some things up.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    6

    Re: Copying text to another form

    Thank you both Worked perfectly

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Copying text to another form

    Quote Originally Posted by Drydareelin View Post
    @Make me rain

    Thank you so much That worked perfectly!

    @Kevininstructor Thank you for the help Yours didn't seem to copy it all down, but that code did help neaten some things up.
    I figured why take all the fun away from you of completing the code. Being a developer means you are expected to code not others. Any ways good to hear you have a solution now.

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