Basic PropertyGrid Cut, Copy, & Paste Question

G

Guest

We have a Windows Form developed through .NET C# with a variety of
components, including objects of type TextBox, PropertyGrid, RichTextBox, and
DataGrid. Our Form includes menu items for cut, copy, and paste. Our
intention is to use these menu items to move text-type information between
the clipboard and certain components. We would like those menu items to
perform for the current active component, if appropriate. For instance, if
text is selected within one of the text boxes and the cut menu item is
invoked, we would like that text to be cut out of the text box and placed
into the clipboard. The TextBoxBase class makes this easy for objects of
type TextBox. However, we’re not sure how to do it when text is selected
within other types of components. For instance, we’re not sure how to apply
cut, copy, or paste when a string-type property value is selected within a
PropertyGrid. Is there a way to do this – specifically for string values
presented through a PropertyGrid? And, more generically, is there a way we
can apply cut, copy, and paste operations to the current selected component,
when those operations are appropriate for that component?
 
G

Guest

A brief status update regarding the Cut/Copy/Paste issue. I tried applying
Jeffrey Tan’s simple “SendKeys†approach set forth in his response to our
inquiry. At first it seemed great. I booted the Application and found I
could Cut, Copy, and Paste just fine to and from a text box which I was using
as a guinea pig. This was before I created a "job". "Jobs" are the main
target of our application. When I created a job, a strange thing happened.
When I again invoked the Edit-Copy menu item to copy text in the sample text
box, the Job-Copy menu item was mysteriously invoked. Before creating a job,
the Job-Copy menu item was disabled. After creating a job, the Job-Copy menu
was enabled. So the problem showed up only when the Job-Copy menu item was
enabled. Now, interestingly, Edit-Copy has a shortcut key sequence of
Ctrl+C. Job-Copy has a shortcut key sequence of Ctrl+Shift+C. Implementing
the Edit-Copy handler with either SendKeys.Send("^(C)") or
SendKeys.Send("^C") caused the Job-Copy menu item to get invoked when it was
enabled. I poked around in Google to see if this was a known bug regarding
the SendKeys class. I didn’t find a description of that bug, per se, but
found some reporting of other SendKeys bugs. I also found this statement at
the site, http://www.hammerdata.com/Newsgrp/api/api0046.htm :


“Under most circumstances, SendKeys is not recommended in a production
environment. This is because the keystrokes are processed by whichever
window is currently active on the desktop. Obviously this will cause
unpredictable behavior (to say the least) in case another app receives the
focus while your code is processing the Sendkeys statement. If you're
unlucky, the keystrokes when sent to application "y" may cause all documents
to be deleted or the hard drive to be formatted. So, simply put, try to
avoid Sendkeys at all cost.â€


So I think I’ll steer clear of Tan’s simple SendKeys suggestion and see what
I can do with the slightly more difficult second approach he provided. For
the record, this problem also existed in the Designer for Edit-Cut (shortcut
key sequence Ctrl+X) and File-Delete Project (shortcut key sequence
Ctrl+Shift+X). In other words, attempting to cut text when File-Delete
Project is enabled will cause the current project to be deleted instead. Not
good. Is there a better approach?
 
G

Guest

I adapted the handling of our Edit-Copy handler in the spirit of Jeffrey
Tan’s second suggestion to see if it would allow us to copy text from a
string property presented in a PropertyGrid and place that text into the
clipboard. Here’s my resulting implementation of that Edit-Copy handler:

private void OnClick_EditCopy_MenuItem(object sender, System.EventArgs e)
{
if (!ActiveControl.Equals(null))
{
Type ctrlType = ActiveControl.GetType();

if (ctrlType.IsSubclassOf(typeof(TextBoxBase)))
{
TextBoxBase textBox = (TextBoxBase)ActiveControl;

if (textBox.SelectionLength > 0)
{
textBox.Copy();
}
}
else if ((ctrlType.Equals(typeof(PropertyGrid))) ||
(ctrlType.IsSubclassOf(typeof(PropertyGrid))))
{
PropertyGrid pg = (PropertyGrid)ActiveControl;

if (pg.SelectedGridItem != null)
{
PropertyDescriptor pd = pg.SelectedGridItem.PropertyDescriptor;

if (pd.Converter.CanConvertTo(typeof(string)))
{
string propVal =
(string)pd.Converter.ConvertTo(pd.GetValue(pg.SelectedObject),
typeof(string));
Clipboard.SetDataObject(propVal, true);
}
}
}
}
}

I then selected a portion of the text in a string field presented in a
PropertyGrid and invoked Edit-Copy. The string - “My Rule Name†-
represented the entire value of the targeted property. However, with my
mouse I only selected a portion of this text – specifically, “My Rule†–
before invoking Edit-Copy. The algorithm placed the entire value of the
property – “My Rule Name†– into the clipboard and not simply the selected
text, “My Ruleâ€. At this point, I’m not sure how to dig out only the
selected portion of the string property, which is really what we want to do.
 
J

Jeffrey Tan[MSFT]

I will spend some time to do some research on this issue, I will reply to
you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi,

Currently we are finding some experience people to handle this issue, we
will reply to you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top