Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Cut,Copy ,Paste Problem...

Cut,Copy ,Paste Problem...

Scheduled Pinned Locked Moved C#
dockerhelpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rahul DSG
    wrote on last edited by
    #1

    Private StringBuilder sb=null private void CutCopyPaste(bool Cut, bool Copy, bool Paste) { foreach (Form f in this.MdiChildren) { if (f.ContainsFocus) { foreach (Control c in f.Controls) { if (c.Focused) { if (c.GetType() != typeof(Label)) { if (Cut) { sb.Append(c.Text); c.Text = ""; } else if (Copy) sb.Append(c.Text); else if (Paste) c.Text = sb.ToString(); break; } } } } } } private void copyToolStripMenuItem_Click(object sender, EventArgs e) { try { sb = null; sb = new StringBuilder(); CutCopyPaste(false, true, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { try { if (sb != null) CutCopyPaste(false, false, true); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void cutToolStripMenuItem_Click(object sender, EventArgs e) { try { sb = null; sb = new StringBuilder(); CutCopyPaste(true, false, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } It is the complete code for cut copy and paste but if we take textbox inside the container like groupbox,panel etc. its not working ...and i want also include the feature Undo and SelectAll...how can i do this...

    R A 2 Replies Last reply
    0
    • R Rahul DSG

      Private StringBuilder sb=null private void CutCopyPaste(bool Cut, bool Copy, bool Paste) { foreach (Form f in this.MdiChildren) { if (f.ContainsFocus) { foreach (Control c in f.Controls) { if (c.Focused) { if (c.GetType() != typeof(Label)) { if (Cut) { sb.Append(c.Text); c.Text = ""; } else if (Copy) sb.Append(c.Text); else if (Paste) c.Text = sb.ToString(); break; } } } } } } private void copyToolStripMenuItem_Click(object sender, EventArgs e) { try { sb = null; sb = new StringBuilder(); CutCopyPaste(false, true, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { try { if (sb != null) CutCopyPaste(false, false, true); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void cutToolStripMenuItem_Click(object sender, EventArgs e) { try { sb = null; sb = new StringBuilder(); CutCopyPaste(true, false, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } It is the complete code for cut copy and paste but if we take textbox inside the container like groupbox,panel etc. its not working ...and i want also include the feature Undo and SelectAll...how can i do this...

      R Offline
      R Offline
      ricmil42
      wrote on last edited by
      #2

      Group and Panel controls have their own Collection of controls. So once you find one of these controls you have to do a foreach() to access the controls that it holds. You can use a recursive method to access the control you are looking for, or write code to access these special cases. My choice would be recursion since it will handle the nesting of these special cases.

      R 1 Reply Last reply
      0
      • R Rahul DSG

        Private StringBuilder sb=null private void CutCopyPaste(bool Cut, bool Copy, bool Paste) { foreach (Form f in this.MdiChildren) { if (f.ContainsFocus) { foreach (Control c in f.Controls) { if (c.Focused) { if (c.GetType() != typeof(Label)) { if (Cut) { sb.Append(c.Text); c.Text = ""; } else if (Copy) sb.Append(c.Text); else if (Paste) c.Text = sb.ToString(); break; } } } } } } private void copyToolStripMenuItem_Click(object sender, EventArgs e) { try { sb = null; sb = new StringBuilder(); CutCopyPaste(false, true, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { try { if (sb != null) CutCopyPaste(false, false, true); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void cutToolStripMenuItem_Click(object sender, EventArgs e) { try { sb = null; sb = new StringBuilder(); CutCopyPaste(true, false, false); } catch (Exception ex) { MessageBox.Show(ex.Message); } } It is the complete code for cut copy and paste but if we take textbox inside the container like groupbox,panel etc. its not working ...and i want also include the feature Undo and SelectAll...how can i do this...

        A Offline
        A Offline
        Andrew Rissing
        wrote on last edited by
        #3

        The reason it fails is because you're not doing a recursive search through the child controls of the Form. You're only looking at the top level of the controls - controls are hierarchal in nature. I would definitely recommend reworking your code to be more modular or atleast rely on a different method of finding the focused control. One method might be hooking up a listener for the focused event on every control (assuming you don't have 1000+ controls) - see this[^]. You might also consider using the clipboard to maintain the text to be copied rather than the stringbuilder you've got - see this[^].

        1 Reply Last reply
        0
        • R ricmil42

          Group and Panel controls have their own Collection of controls. So once you find one of these controls you have to do a foreach() to access the controls that it holds. You can use a recursive method to access the control you are looking for, or write code to access these special cases. My choice would be recursion since it will handle the nesting of these special cases.

          R Offline
          R Offline
          Rahul DSG
          wrote on last edited by
          #4

          yes u are right...how can i acess those controls that it holds... guide me...

          R 1 Reply Last reply
          0
          • R Rahul DSG

            yes u are right...how can i acess those controls that it holds... guide me...

            R Offline
            R Offline
            ricmil42
            wrote on last edited by
            #5

            Try something like this (not tested).

            public void ProcessLabel ( Control.ControlCollection c )
            {
            foreach ( Control control in c )
            {
            if ( control.Controls.Count != 0 )
            ProcessLabel ( control.Controls ) ;

                if ( control is Label )
                    {
                        // Do something with the label.
                    }
            }
            

            }

            Call it like

            ProcessLabel ( Controls ) // This is the Control array from your main form or Panel or Group control.

            If you need to send more parameters, modify the method but be sure you also add them to the recursive call as well.

            public void ProcessLabel ( Control.ControlCollection c, bool cut, bool copy, bool paste )
            {
            foreach ( Control control in c )
            {
            if ( control.Controls.Count != 0 )
            ProcessLabel ( control.Controls, cut, copy, paste ) ;

                if ( control is Label )
                    {
                        // Do something with the label.
                    }
            }
            

            }

            R 1 Reply Last reply
            0
            • R ricmil42

              Try something like this (not tested).

              public void ProcessLabel ( Control.ControlCollection c )
              {
              foreach ( Control control in c )
              {
              if ( control.Controls.Count != 0 )
              ProcessLabel ( control.Controls ) ;

                  if ( control is Label )
                      {
                          // Do something with the label.
                      }
              }
              

              }

              Call it like

              ProcessLabel ( Controls ) // This is the Control array from your main form or Panel or Group control.

              If you need to send more parameters, modify the method but be sure you also add them to the recursive call as well.

              public void ProcessLabel ( Control.ControlCollection c, bool cut, bool copy, bool paste )
              {
              foreach ( Control control in c )
              {
              if ( control.Controls.Count != 0 )
              ProcessLabel ( control.Controls, cut, copy, paste ) ;

                  if ( control is Label )
                      {
                          // Do something with the label.
                      }
              }
              

              }

              R Offline
              R Offline
              Rahul DSG
              wrote on last edited by
              #6

              how to call this method... processlabel(whichcontrol);

              R 1 Reply Last reply
              0
              • R Rahul DSG

                how to call this method... processlabel(whichcontrol);

                R Offline
                R Offline
                ricmil42
                wrote on last edited by
                #7

                You can change the signature like this to add the specific label control you are interested in processing.

                public void ProcessLabel ( Control.ControlCollection c, Label label )
                { foreach ( Control control in c )
                {
                if ( control.Controls.Count != 0 )
                ProcessLabel ( control.Controls ) ;

                    if ( control is Label && control.Name = label.Name )
                    {
                        // Do something with the label.
                        label.ForeColor = Color.Red ; // Or whatever.
                    }
                }
                

                }

                How to call this method

                // "Controls" is the Control array from your main form or Panel or Group control.
                // The container that holds the label control.
                ProcessLabel ( Controls, myLabel )

                If you need to send more parameters, modify the method but be sure you also add them to the recursive call as well.

                public void ProcessLabel ( Control.ControlCollection c,
                Label label,
                bool cut,
                bool copy,
                bool paste )
                {
                foreach ( Control control in c )
                {
                if ( control.Controls.Count != 0 )
                ProcessLabel ( control.Controls, label, cut, copy, paste ) ;

                    if ( control is Label && control.Name = label.Name )
                    {
                        // Do something with the label.
                        label.ForeColor = Color.Red ; // Or whatever.
                    }
                }
                

                }

                R 1 Reply Last reply
                0
                • R ricmil42

                  You can change the signature like this to add the specific label control you are interested in processing.

                  public void ProcessLabel ( Control.ControlCollection c, Label label )
                  { foreach ( Control control in c )
                  {
                  if ( control.Controls.Count != 0 )
                  ProcessLabel ( control.Controls ) ;

                      if ( control is Label && control.Name = label.Name )
                      {
                          // Do something with the label.
                          label.ForeColor = Color.Red ; // Or whatever.
                      }
                  }
                  

                  }

                  How to call this method

                  // "Controls" is the Control array from your main form or Panel or Group control.
                  // The container that holds the label control.
                  ProcessLabel ( Controls, myLabel )

                  If you need to send more parameters, modify the method but be sure you also add them to the recursive call as well.

                  public void ProcessLabel ( Control.ControlCollection c,
                  Label label,
                  bool cut,
                  bool copy,
                  bool paste )
                  {
                  foreach ( Control control in c )
                  {
                  if ( control.Controls.Count != 0 )
                  ProcessLabel ( control.Controls, label, cut, copy, paste ) ;

                      if ( control is Label && control.Name = label.Name )
                      {
                          // Do something with the label.
                          label.ForeColor = Color.Red ; // Or whatever.
                      }
                  }
                  

                  }

                  R Offline
                  R Offline
                  Rahul DSG
                  wrote on last edited by
                  #8

                  I am trying But its not working...have u run this..

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups