-
Dec 4th, 2017, 04:41 AM
#1
Thread Starter
Fanatic Member
[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.
-
Dec 4th, 2017, 05:28 AM
#2
Thread Starter
Fanatic Member
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.
-
Dec 4th, 2017, 05:37 AM
#3
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.
-
Dec 4th, 2017, 05:57 AM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|