Using controls from one form to another

I have an application that in one form ( Form1) I have a lot of checkBoxes and textBoxes, and Form2I only have a text field in it, but I want to put some content from text fields Form1and put in a Form2textBox like this, but between forms:

textBox1.Text = "Test: " + textBox1 + "\n" + textBox3;

Like textBox1in Form2, and the second textBox1and textBox3in Form1, but how can I do this? Thanks.

+3
source share
5 answers

, , - Form2, Form1, MDI, , Form1.

, Form2:

public partial class Form2 : Form
{
    public Form1 LaunchOrigin { get; set; }
    // and so on

LaunchOrigin Form2. :

Form2 newForm = new Form2();
newForm.LaunchOrigin = this;
newForm.Show();

Form1 . :

    private void Form2_Load(object sender, EventArgs e)
    {
        this.Text = LaunchOrigin.Text;
    }

, , . Form1, , . , , - , , Form1:

public partial class Form1 : Form
{
    public Button theButton;
    public Form1()
    {
        InitializeComponent();
        theButton = button1; // where button1 is what I dragged on
    }
    // and so on

, , , . , , , , , , .

+7

... UGLY:

  • - Form1 Form2, . , . Form1 Form2.

  • , Form1 Form2 - / Form2. , Form1 Form2. , Form2 Form1 .

... .

, .

, "pub/sub" - publish/subscribe. , , ( - ) , .

Form 1 , ", " ( - , ). Form 2 , "Yo, - , ".

, , (IPublisher, ISubscriber) - / . , , , ( , "" "" ). MessageReceived /, .

, .

+7

, , , , , "" , , , , .

, , , , " " " ": " " "" "" " ", .

, .

, , ( - ): (): : ( )

using System;
using System.Collections.Generic;
using System.Windows.Forms;

// note : compiles against FrameWork 2.0 and 4.0
// wanted this to work w/o Linq, automatic properties, etc.

namespace MessageHandler
{
    public static class TextBoxMessenger
    {
        // internal List of TextBoxes of interest on all Forms
        internal static List<TextBox> _messageEnabledTBxes = new List<TextBox>();

        // public Property to get/set the collection of TextBoxes of interest
        public static List<TextBox>MessageEnabledTextBoxes
        {
            get { return _messageEnabledTBxes; }

            set { _messageEnabledTBxes = value; }
        }

        // in case you want to register one TextBox at a time
        public static void RegisterTextBoxForMessaging(TextBox theTBx)
        {
            _messageEnabledTBxes.Add(theTBx);
        }

       // send from one specific TBx to another
        public static void setText(TextBox originTBx, TextBox destinationTBx)
       {
           destinationTBx.Text = originTBx.Text;
       }

       // send to a specified list of TextBoxes
        public static void setText(TextBox originTBx, List<TextBox> destinationTBxs)
       {
           foreach (TextBox theTBx in destinationTBxs)
           {
               theTBx.Text = originTBx.Text;
           }
       }

        // set text in all other TextBoxes in MessageEnabledTextBoxes list
        public static void setText(TextBox originTBx)
       {
           foreach (TextBox theTBx in _messageEnabledTBxes)
           {
               // a needless check, since assigning the text of the
               // original TextBox to itself wouldn't "hurt" anything
               // but, imho, much better "practice" to always test
               if (theTBx != originTBx) theTBx.Text = originTBx.Text;
           }
       }
    }
}

, , : , Form1 Load :

// assume Form2 has a single TextBox on it named 'textBox1'
public Form2 myForm2;

private void Form1_Load(object sender, EventArgs e)
{
    myForm2 = new Form2();
    myForm2.Show();

    // register all the TextBoxes

    // note the redundant use of 'this here : it a deliberate choice to make
    // the code communicate to a future user/reader/maintainter
    TextBoxMessenger.RegisterTextBoxForMessaging(this.textBox1);
    TextBoxMessenger.RegisterTextBoxForMessaging(this.textBox2);
    TextBoxMessenger.RegisterTextBoxForMessaging((TextBox)myForm2.Controls["textBox1"]);

    // or ...
    //TextBoxMessenger.MessageEnabledTextBoxes = new List<TextBox> 
    //{
    //    this.textBox1, this.textBox2, (TextBox)myForm2.Controls["textBox1"]
    //};
}

, , Form1:

private void button1_Click(object sender, EventArgs e)
{
    // tests

    // one to all ...
    //TextBoxMessenger.setText(this.textBox1);

    // one to a specific TextBox on another Form
    //TextBoxMessenger.setText(this.textBox1, (TextBox) myForm2.Controls["textBox1"]);

    // one to a List<TextBox>
    TextBoxMessenger.setText(this.textBox1, new List<TextBox> { this.textBox2, (TextBox)myForm2.Controls["textBox1"]});
}

"", " ", , :

(TextBox) myForm2.Controls [ "textBox1" ]// - ! !

, , , , "", , , .

, . " " , , , , , - imho, " ", "", . ,

+3

, , StackOverflow. , Visual Studio.

- , . .

, , ( ), , - "ApplicationData". , .

, , get/set ApplicationData. , ApplicationData .

, . (.. ), , .

. : :

class ApplicationData{
    private string _firstName;
    public string FirstName;
    {
       get { return _firstName;; }
       set { __firstName;=value; }
    }

    private string _lastName;
    public string LastName;
    {
       get { return _lastName; }
       set { __lastName=value; }
    }
}

class ChildForm : Form
{
   private ApplicationData _applicationData=null;

   public ApplicationData AppData
   {
       get { return _applicationData; }
       set { _applicationData=value; }
   }

   void Load_Form(object sender, EventArgs args)
   {
         txtFirstName.Text=AppData.FirstName;
         txtLastName.Text=AppData.LastName;
    }

   void Form_Closing(object sender, EventArgs args)
   {
         AppData.FirstName=txtFirstName.Text;
         AppData.LastName=txtLastName.Text;
    }

}

class MainForm : Form
{
    private ApplicationData _applicationData=new ApplicationData();

    void Button_Click(object sender, EventArgs args)
    {
        ChildForm childForm=new ChildForm ();

        ChildForm.AppData=_applicatonData;

        ChildForm.ShowDialog();

        string fullName=_applicatonData.LastName + " " + _applicatonData.FirstName
    }
}

, /, , . .

, Visual Studio , . .

The design pattern for creating and using the ApplicationData class is the first step in separating the presentation from your content. The shame of Visual Studio makes no more recommendations in this area.

+1
source

You can even do this by writing a parameterized constructor.

eg.

namespace SomeNamespace
{
    public partial class Form2 : Form
    {
       string someStr = string.Empty;

        public Form2()
        {
            InitializeComponent();
        }

        public Form2(string params) // Parametrized Constructor
        {
            InitializeComponent();

            someStr  = params
        }

        private void Form2_Load(object sender, EventArgs e)
        {
          Form2TextBox.Text = someStr  ;
        }
    }
}

Now from Form1 call Form2 this way

Form2 objF2 = new Form2(Form1TextBox1.Text);
objF2.Showdialog();
0
source

Source: https://habr.com/ru/post/1722893/


All Articles