question

JohnCTX-6479 avatar image
0 Votes"
JohnCTX-6479 asked karenpayneoregon answered

Printing out text between forms

I am having too many issues when it comes to popup forms.
What I want to do is to apply text between forms.

Here is the code snippet:
using System;
using System.Windows.Forms;

 namespace Source_Code_Assistant_Build_0007
 {
     public partial class FormTwo : Form
     {
           
    
         public FormTwo()
         {
             InitializeComponent();
         }
    
         private void ApplyDataButton_Click(object sender, EventArgs e)
         {
             SourceCodeAssistantForm form = new SourceCodeAssistantForm();
             SourceCodeTextBox.Text += form.Controls[0].Text;
    
         }
    
         private void SourceCodeTextBox_TextChanged(object sender, EventArgs e)
         {
                
         }
     }
 }

namespace Source_Code_Assistant_Build_0007
{
public partial class SourceCodeAssistantForm : Form
{
FormTwo two;

     public SourceCodeAssistantForm()
     {
         InitializeComponent();
     }

     private void SourceCodeAssistantForm_Load(object sender, EventArgs e)
     {
         FormTwo two = new FormTwo();
         two.Show();
            

     }
 }

}

And here are the forms:

122915-screenshot-2.png


dotnet-csharp
screenshot-2.png (281.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

See the following project on GitHub which is a basic code sample for passing data from child to parent form. A delegate and event are used found here. The parent form subscribes to the event here. In the following method data is received from the child form.

Many developers do not consider using delegates-events but they can make things very simple once you understand them.

To pass data from parent to child, create an overloaded constructor for the child form, pass data via this constructor then assign that data to a private variable to be used in say the child form shown event. Alternate is to cast the Owner property of the child form to the type of the parent form but consider there many be cases that will not work when dealing with different scopes of data.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.