Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

SendKeys Problems

I am trying to use the SendKeys.Send function to pass the contents of a richtextbox to another window.  I have 2 problems, first is that since SendKeys.Send has special characters such as (+,^,%,~,(,),{,}) that have to be enclosed in {} to be passed as text to another window.  I have been able to accomplish this with the following method:

      private string FormatText(string p)
        {
            string[] ch = new string[] { "+", "^", "%", "~", "(", ")" };
            string retval = p;

            foreach (string c in ch)
            {
                retval = retval.Replace(c, "{" + c + "}");
            }

            return retval;
        }

But if I add the {} to the beginning of the string array to be enclosed in braces,  the first one({) is successful, but after that, since I have enclosed the first { in {}, now there are a new instances of the }.  It is like I need to be able to replace both {} at the same time so that I will not be trying to enclose new {}.

2nd problem is that I would like to know if there is someway I can send the text to another window AND retain the rich text formatting???????

Thanks,
Kendal
0
gvector1
Asked:
gvector1
  • 5
  • 4
1 Solution
 
ColemssCommented:
DO you contorl the other window?
If you do pass the entire contorl.

Can you past what you are trying to do with the {}.  The erroring code

This may help
http://msdn2.microsoft.com/en-us/library/ms750582.aspx
0
 
gvector1Author Commented:
No I do not control the other window.  It is another word processing application that is running.  

Here is all the code I use to do a sendkeys.send:

        private void sendToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendTo st = new SendTo();
            if (st.ShowDialog() == DialogResult.OK)
            {
                NativeWin32.SetForegroundWindow(st.SelectedHandle);
                string val = FormatText(rtbTranscribe.RichTextBox.Text);
                SendKeys.Send(val);
            }
        }

        private string FormatText(string p)
        {
            string[] ch = new string[] { "+", "^", "%", "~", "(", ")" };
            string retval = p;

            foreach (string c in ch)
            {
                retval = retval.Replace(c, "{" + c + "}");
            }

            return retval;
        }

SendTo is a form that will show a list of open windows on the machine.  Once selected, control is sent back to the click method and the window handle is used to set as the Foreground Window.  If you look in MSDN under the SendKeys.Send method, it states that if you want to send a { or } in the sendkeys.send method, they have to be enclosed with {} themselves.  So if I find a { in my text and enclose it in {}, then there are new } that will also get enclosed in {} when I do the search for }.

Thanks,
Kendal
0
 
ColemssCommented:
for your } problem

find the locations in the string of each { and }
save each of them in an array
2 int arrays i would use arraylist to make it easier
loop through the first arrays using the int of the array to replace the { in the string with {}}
but you need to use a new string

sudo code
foreach int i in arraylist{
int cnt = 0
foreach char c in oldstring{
if (cnt = i)
{
{newstring += "{}}"
}
else
{
newstring = c
}
cnt++;
}

}

loop through them and do you replace
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gvector1Author Commented:
After making replacements of the first array, my indexes will be wrong for the replacements of my second array.
0
 
gvector1Author Commented:
Does anyone have any suggestions to my problem??????
0
 
ColemssCommented:
I wrote my sudo code wrong you will need to keep track of both arrays and make the replacements in the order they are found. Using something like a dictionary by key. Or two array but more complicated

You find in this order a{  b{  c}  }  {
add to a dictionary using the numerical location of the { or } as the key
key value
2    { {}
5     {{}
8     {}}
10     {}}
12     {{}
then as you are looping through the old string
Add the value based on the looping index
0
 
gvector1Author Commented:
So basically I would have to create a dictionary of the indexes for { and }.  Then I would have to loop through the indexes adding the necessary text.  Since I would be adding a { and } to enclose the existing brackets, I would multiply the loopcounter by 2 in order to find the new index of each bracket that needs to be enclosed.  That is the logic I am basically thinking of.  Would that seem correct.

What about being able to use the Sendkeys.Send to send the contents of a richtextbox while retaining the rich text formats???????  Sort of like if I copy the contents to the clipboard and paste them into the new window it will retain the rich text formatting.  Can this be done using Sendkeys.Send()??????????
0
 
ColemssCommented:
Yeah that would work.
I was actually thinking more looping the old string - btw this would work with any number of characters to replace.  Yours would mean it has to be + 2 chars
loop old string
if(I++ in dict(key))
replace text in new string with dict(i)
else
add current value to new string
loop

I am fairly sure send keys will not work. As it is only keys.  Ive used it before and it is very limited.
Look at send message.

http://www.thescripts.com/forum/thread278799.html
http://www.c-sharpcorner.com/UploadFile/thmok/SendingWindowsMessageinCSharp11262005042819AM/SendingWindowsMessageinCSharp.aspx
0
 
gvector1Author Commented:
As far as passing the richtext, I used the clipboard.  I copied the richtext from the richtextbox to the clipboard, made the other window have focus, then sent a ctrl-v key combination to the window and that worked.  Thanks for the recommendation for parsing the special characters.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now