Results 1 to 4 of 4

Thread: [RESOLVED] Passing text between multiple forms

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] Passing text between multiple forms

    Hi,

    I have a form with RichTextbox in It. It serves as a "zoom window" - to show entire content of a From1 Textbox.Text. I've got this working by passing form instances and Modifiers = Public of Textbox and RichTextbox. However, I need this "zoom window" in several forms, so I should write a code to determine what control to be updated with Text. Here is my full code:

    Form1 :

    Code:
     TextZoom frmTextZoom;
    
     public Form1()
     {
           InitializeComponent();
           frmTextZoom= new TextZoom(this);
     }
    
     private void ShowZoomWindow()
     {
           Point New_Loc = Textbox1.PointToScreen(Point.Empty);
          
           frmTextZoom.StartPosition = FormStartPosition.Manual;
           frmTextZoom.Location = New_Loc;
           frmTextZoom.richTextBox1.Text = Textbox1.Text;
           frmTextZoom.ShowDialog();
     }
    
     private void Form1_KeyDown(object sender, KeyEventArgs e)
     {
                dynamic ctl = ActiveControl;
    
                switch (e.KeyCode)
                {
                    case (Keys.ControlKey):
    
                        if (ctl is TextBox)
                        {
                            TextBox txt = (TextBox)ctl;
    
                            if (object.ReferenceEquals(txt, Textbox1))
                            {
                                ShowZoomWindow() 
                            }
    
                        }
                        break;
               }
      }
    TextZoom (form with richtextbox):


    Code:
     Form1 frm;
    
     public TextZoom(Form1 F)
     {
            InitializeComponent();
            frm = F;
     }
    
     private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
     {
            if (e.KeyCode==Keys.Enter)
            {
                    frm.Textbox1.Text = richTextBox1.Text;
                    e.Handled = true;
                    this.Close();
            }
     }
    In short - I open TextZoom window wiht CTRL key and then text should pass between forms. Now I need a logic to make this working with a multiple forms.
    Last edited by LuckyLuke82; Dec 4th, 2017 at 04:45 AM.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Passing text between multiple forms

    Solved.

    Static class:
    Code:
       public static void ShowZoomWindow(Form frm,TextBox Txt)
       {
            
                Point New_Loc = Txt.PointToScreen(Point.Empty);
    
                TextZoom zoom_text = new TextZoom();
                zoom_text.Owner = frm;
    
                //find control to show window
                var text_ctl = frm.Controls.Find("Textbox1", true).FirstOrDefault() as Control;
                TextBox Txt1 = (TextBox)text_ctl;
    
                zoom_text.richTextBox1.Text = Txt1.Text;
                zoom_text.StartPosition = FormStartPosition.Manual;
                zoom_text.Location = New_Loc;
                zoom_text.ShowDialog();
    
        }

    TextZoom (form with richtextbox):



    Code:
        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
               
                if (e.KeyCode==Keys.Enter)
                {
                    Form parent = (Form)this.Owner;
    
                    var text_ctl = parent.Controls.Find("Textbox1", true).FirstOrDefault() as Control;
                    TextBox Txt = (TextBox)text_ctl;
    
                   Txt.Text = this.richTextBox1.Text;
                
                    e.Handled = true;
                    this.Close();
                }
        }
    Now I can use this window in every Form, I only need to set textbox1 names same on each form, and set Modifiers to Public.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Passing text between multiple forms

    A better alternative would be to add a property to TextZoom with a data type of Textbox, and pass the actual textbox to it, eg:
    Code:
       public static void ShowZoomWindow(Form frm,TextBox Txt)
       {
            
                Point New_Loc = Txt.PointToScreen(Point.Empty);
    
                TextZoom zoom_text = new TextZoom();
                zoom_text.Owner = frm;
                zoom_text.TheTextbox = txt;
    
                zoom_text.richTextBox1.Text = Txt.Text;
                zoom_text.StartPosition = FormStartPosition.Manual;
                zoom_text.Location = New_Loc;
                zoom_text.ShowDialog();
    
        }
    Code:
        public property TextBox TheTextbox; 
    
        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
               
                if (e.KeyCode==Keys.Enter)
                {
                    Form parent = (Form)this.Owner;
    
                    TheTextbox.Text = this.richTextBox1.Text;
                
                    e.Handled = true;
                    this.Close();
                }
        }
    You then don't need to worry about control names, and can also use it for multiple textboxes in the same window.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: [RESOLVED] Passing text between multiple forms

    thanks, however one modification was needed: public TextBox TheTextbox;, not public property TextBox TheTextbox; - that gives me errors.
    Last edited by LuckyLuke82; Dec 4th, 2017 at 06:02 AM.

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