Cut,Copy ,Paste Problem...
-
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...
-
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...
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.
-
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...
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[^].
-
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.
-
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. } }
}
-
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. } }
}
-
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. } }
}
-
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. } }
}