Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# WinForms ~ Trouble with making Form1 being the parent of Form2

c# winforms

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 FernandoMAlbert

FernandoMAlbert

    CC Lurker

  • New Member
  • Pip
  • 9 posts

Posted 12 November 2014 - 11:29 AM

Hi folks!

 

I am working on a pretty cool project, something I am doing at my own pace as I really love to code, but I am still a bit of a noob (years ago, I learned Visual Basic, but now I want to venture well into C#. This is not homework as I am researching on my own, but believe is the best place to post this.

 

So pretty much I am coding a Journal. The user can make entries and he/she can save the content in <RichTextBox1> to a file. Text formatting and styling buttons have been added and coded fine. Now, here comes the tricky deal. I want to code a button that will do the following:

 

- Opening a website (blog) -coded- 

- Calling Form2 as a popup window -coded- Form2 contains another <RichTextBox> where Read Only = true. -coded- 

 

I need for the program to transfer all the information from <Form1.RichTextBox1> to <Form2.RichTextBox1>  and I believe I need to make the Parent-Child connection, but I have no clue how. 

 

So far, the code for my button is this:

private void Post_Click(object sender, EventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo("http://www.thesite.com/blog_post.php?do=newblog");
            Process.Start(sInfo);
            DreamPost form2object = new DreamPost();
            form2object.Show();
        }

So pretty much it launches a website and launches my second form. Now, my question is about the code I need to enter so all the information from the Form1.RichTextBox is transferred to Form2.RichTextBox. And also, how to make the parent-child reference, as I have been a good two hours without any success. 

 

Could you guys give me a hand?

 


Edited by FernandoMAlbert, 12 November 2014 - 11:30 AM.


#2 Blimp

Blimp

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 199 posts

Posted 13 November 2014 - 01:13 AM

Go to the DreamPost class and add the following:

 

public DreamPost(string data)

{

  // Assign 'data' to your richtextbox . eg:

  richtextbox1.text = data;

}

 

Then when creating your new DreamPost class, on this line:


DreamPost form2object = new DreamPost();
 
Change that to:
 
DreamPost form2object = new DreamPost(richtextbox1.text);
 
 
Something along the lines of the above would work :-)


#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 14 November 2014 - 06:43 AM

welcome aboard,

 

In learning's sake, there's also the chance in C# of passing variables by reference, so you could write from form2 to form1 whatever you want if you pass form1 as a parameter to form2, by reference.



#4 FernandoMAlbert

FernandoMAlbert

    CC Lurker

  • New Member
  • Pip
  • 9 posts

Posted 14 November 2014 - 08:06 AM

Thanks a lot folks :)

 

I also had a bug in the parent-child relationship, but is fixed now!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download