Question Copy, Paste Problem ....

poprad

New member
Joined
Aug 2, 2017
Messages
2
Programming Experience
10+
Hello.
Please advise. I created three children's windows on the main window and I placed a richTextBox in each. In the main window, I created a menu system, including Edit - Copy, Paste, ...... And this problem:
So I have three children's windows that do not overlap each other. In one box I write text, mark something and use Copy from my menu. How can I ensure that using Paste (of course, from my menu) will save the selected text to a window I clicked to be active? Copy - Paste works smoothly in the window, but I want to transfer text using Paste to other child windows.
I've tried something like that, but without success ...

HTML:
// *** PASTE MENU ****************************************************
// Paste funkcia
// *******************************************************************
private void pasteMenu_Click(object sender, EventArgs e)
{
    if (Okno2.Visible)    Okno2.richTextBox2.Paste();
    if (Okno3.Visible)    Okno3.richTextBox3.Paste();
    if (Okno4.Visible)    Okno4.richTextBox4.Paste();
}
 
The RichTextBox's Cut(), Copy(), and Paste() methods all utilize the Windows Clipboard which can lead to unexpected results. Such as the user could click the Copy button in your app, it calls the RichTextBox.Copy() method (which calls Clipboard.SetText() or similar under the hood) then they copy a url or something from a friend or perhaps something they had highlighted in Notepad or somewhere else and then they come back to your app and click the Paste, which calls the RichTextBox.Paste() (which internally calls Clipboard.GetText() or similar) and BAM you have bad data being pasted into the other form(s), not what you want, that's the first issue and luckily you can get around this by setting a variable to the value when they click Copy, then retrieve that value when they click Paste.

As for your actual question, this one is more complicated to answer because you need to need to know about instantiate, if I had to guess your Okno2, Okno3, & Okno4 are the form names directly, if so this means you're trying to set the text of the RichTextBox by it's direct name and not through its instance when running.
When your app opens any of the new child windows you're probably creating a new form, showing it, and adding it to the MDIChildren list then the sub ends and that's the end of the variable of the form, the form still exists as it's kept alive in the MDI children list but you no longer have the direct variable to it. There's two ways to handle that, you can either A) keep the variable instance when you create it (put it up at the top of the form so it's visible throughout) then in the your Paste sub you simply check that the variable is instantiated & set the text if it is.
Or B) you could loop the MDIChildren collection and check and check for the forms in question, when you have one that is you simply set the text, this allows for more flexibility as you can have more than 1 copy of the form open and it will set the text on all of them.
 
Form2 Okno2 = new Form2();
Form3 Okno3 = new Form3();
Form4 Okno4 = new Form4();

This is that I want to transfer using Copy, Paste the text between the windows. This is how it works in Windows.
 
That looks like a local declaration of the forms, presumably in a different sub when the user clicks a menu item to have it open.

What you could do is declare it at the top of the form, have the "open" sub create a new instance of it and all you need to do in your paste sub is check if it's something and if so, set the text.

Something like:
public partial class Form1 : Form {

    Okno2 _Okno2 = null;

    public Form1() {
        InitializeComponent();
    }

    private void OpenOkno2() {
        if (_Okno2 != null) {
            _Okno2 = new Okno2();
            _Okno2.Show(this);
        }
    }

    private void Paste() {
        if (_Okno2 != null)
            _Okno2.richTextBox1.Text = "your text";
    }
}
 
Back
Top Bottom