So my current project is comming to an end.
But I have an issue, i need to get a selection from any window and insert that into my current method, and then paste a new string into the selection.
This means that if i mark the following "This is a simple line", and i press my shortcut, i want "This is a simple line" to go into my method, and transform the text to "This line is more advanced", when i press my global hotkey.
Currently the method takes a string and returns a string (So the method works fine), i just need for it to copy the selection, do the method and then paste the new text, when i use my shortcut.
Any ideas?
I've solved it, the "fix" was just to simulate/emulate the "ctrt + c" "ctrl + v" shortcut in the project. I've gotten my inspiration from: This youtube video. Big thanks to "Matthew Watson" for the suggestion
Clipboard.SetText(textBox1.Text); //To copy your text to your clipboard
Clipboard.GetText(); //To get your text from your clipboard
Credit: https://stackoverflow.com/a/10140582/12345062
Related
I have a basic text editor app, and I aim to add a feature where the user can click a button and add premade text after where their cursor is.
I currently have this (using some code I found online)
richTextBox1.CaretPosition.InsertTextInRun(s);
I intend for the string s to be the string to be added.
However, the RichTextBox in System.Windows.Forms does NOT contain a CaretPosition. I found one post from 2010 suggesting you use System.Windows.Control, however, that is no longer accessible in .Net Core, it depends on presentation framework.
So, is there any way I could get my goal (inserting a string after the mouse cursor, in a rich text box), in .net core?
Updated2:
Use the following code to fully satisfy the richtextbox of the text text environment.:
richTextBox1.Select(richTextBox1.SelectionStart, richTextBox1.TextLength);//Select everything after the cursor
string tmp = richTextBox1.SelectedText;//Copy them
richTextBox1.SelectedText = "";//Set to null
richTextBox1.AppendText("Hello world"+tmp);//Add the target string and add the original text
Updated1:
// Determine if there is any text in the Clipboard to paste into the text box.
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
IDataObject dataObject = Clipboard.GetDataObject();
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
Clipboard.SetDataObject(dataObject);
}
else
{
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
}
This action preserves the contents of the original pasteboard.
Only works if the clipboard is data. I will continue to update after I think about it.
Original:
You only need to use the clipboard and paste method to insert the specified string after the specified cursor.
Clipboard.SetDataObject("Hello World");
richTextBox1.Paste();
Clipboard.Clear();
Change it yourself according to your needs.
On my website, I can click on a button, which does copy text. In the screenshot below this is how it looks like:
After clicking on the copy text, it should copy the text Bitte einschalten und ausschalten äüöß 876543212345678 which can be seen on the bottom of the picture.
Now I would like to check, if the copy function worked and if the text on the webpage, is the same as the one which has been copied.
I was already searching for answers, but nothing worked for me.
My idea was to paste the text into the browser's URL field and then compare it, but it seems that selenium is not able to access this URL field.
There is also no other text field where I could paste the text inside.
Would appreciate any help!
EDIT: There was a method in C# which doesnt work in .NET Core anymore.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.clipboard?view=windowsdesktop-6.0
Looking for a other solution like this.
With a bit of JavaScript, you might be able to do this:
Create a text field using JavaScript.
Get the value of the text field.
var js = (IJavaScriptExecutor)driver;
var textareaId = DateTime.Now.Ticks.ToString();
var script = $#"
var textarea = document.createElement('textarea');
textarea.id = '{textareaId}';
document.body.appendChild(textarea);";
js.ExecuteScript(script);
var textarea = driver.FindElement(By.Id(textareaId));
// Paste clipboard into textarea
textarea.SendKeys(Keys.CONTROL + "v");
var clipboardText = textarea.GetProperty("value");
Just be aware of where this text area gets appends to the page. If it is hidden or partially obstructed by another element, you will get an ElementNotInteractableException. If this happens, pick a different element on the page when appending the textarea. Just replace document.body.appendChild(textarea) with a reference to a different element.
For example: document.getElementById('someOtherElementId').appendChild(textarea)
I want to send the string ABC to the input field of a windows file dialog. With this code line I can set the focus to the correct element. I see a blinken cursor.
var filedialogOverlay = drv.SwitchTo().ActiveElement();
But the following code doesn't write the string into the element.
Thread.Sleep(1000);
filedialogOverlay.SendKeys("ABC");
EDIT:
The file upload prompt is shown by a website which I want to test. Because of black box testing I can't see the source code. Is there a tool to analyse the GUI?
When I right click the input element I get the following choices.
You can use the SendKeys.SendWait of Windows Form
//Input the file path into the filename field:
SendKeys.SendWait(longfilepath);
//Input "Enter" key
SendKeys.SendWait(#"{Enter}");
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx
If you need to upload file, try to send path to file directly to appropriate input field:
drv.FindElement(By.XPath("//input[#type='file']")).SendKeys("ABC");
P.S. If there are more than one input fields for file upload located on page, you might need more specific XPath, like "//input[#id='some_specific_file_upload']"...
I'm using ToolTip and when i'm writing the text that will show it's getting too long to the right so i clicked enter to keep write the text in a new line it dosent mean when i'm running the program it will be in a new line this is only for me to see the text easier.
toolTip1.SetToolTip(this.checkBox2, "Create automatic animated gif after rain event
this text is a new line");
Instead doing:
toolTip1.SetToolTip(this.checkBox2, "Create automatic animated gif after rain event this text is a new line");
I just want to see what i'm writing more easy.
For example what i mean is:
toolTip1.SetToolTip(this.checkBox2, "Create automatic animated gif after rain event
this is text to keep writing
and this is also the same line one single line to show");
So when i'm running the program i will see all the text ine long line but here in the visual studio i want to see the text in lines.
When doing:
toolTip1.SetToolTip(this.checkBox2, "Create automatic animated gif after rain event
text to add");
I'm getting 9 errors it's like the part: text to add is not inside the ""
If I understand correctly, you want to do this:
toolTip1.SetToolTip(this.checkBox2, "Create automatic animated gif " +
"after rain event " +
"text to add");
String literals in C# cannot span multiple lines. If you want to break your strings across multiple lines, then each line needs to be individually quoted and then you can concatenate them:
string str = "Create automatic animated gif after rain event " +
"text to add";
For better visual presentation you can also use verbatim literals with # prefix:
var s = #"Create automatic animated gif after rain event
this is text to keep writing
and this is also the same line one single line to show";
Note that it will also create 3 lines in the s variable, not just visually.
If you have long texts, it might be easier to store them as string resource.
Right click the project in the solution explorer and select Properties > Resources. If you are adding resources for the first time to this project, click "... Click here to create one". Then enter a name for the string resource (e.g. "AnimatedGifToolTip") and a string value.
In the code you can use this string resource like this:
toolTip1.SetToolTip(this.checkBox2, Properties.Resources.AnimatedGifToolTip);
I would like to know how can I get highlighted text from any window for example: (excel, ie, firefox,…).
please note that the following message not work in the above application
WM_GETTEXT,WM_COPY,EM_GETSELTEXT.
I have also tried control C (copy) and get selected text from clipboard but it is not a good idea.
Language used: C#
I haven't tried it myself, but the Microsoft UI Automation API should have the functionality that you need.
The UI Automation API is what you would use if you were building a screen reader to assist blind people. So it should definitely be able to access the selected text in an arbitrary application.
A good place to start would be with the "Text Pattern Overview" at http://msdn.microsoft.com/en-us/library/ms745158.aspx
Also keep your eye on question 517694. I think you'll find that answers to that question will solve your problem.
No answers huh? Well, I know you can get it from Excel, Word etc using interop. Look into that. It might give you som ideas on how to proceed with ie and ff. But basically the recieving application must have some sort of fascility for letting you do this and I don't think there's any general way which works all the time.
There is no general purpose answer to this question. Each window class will have a different solution.
For instance, if the hilighted text is in an edit window, then you can use EM_GETSEL to get the range of the selection, then WM_GETTEXT to get the text (and then throw the unselected part a way) or EM_LINEFROMCHAR to turn that range into line indexes, and then EM_GETLINE to get the selected text one line at a time.
But this won't work for any other window class.
No need to write this in C# from scratch. What's wrong with using the clipboard? This script ensures that it restores what was on the clipboard when it has finished.
Autohotkey makes this much simpler.
; Hotkey: Ctrl Shift t
^!t::
; Remember what was in the clipboard
clipboardPrev = %clipboard%
; Clear the clipboard
clipboard:=
Sleep,200
; Send a Ctrl C to copy the current selection
SendInput, {Ctrl down}c{Ctrl up}
Sleep,200
; Get the current selection from the clipboard
selectedText=%Clipboard%
if SelectedText =
{
; If the first attempt didn't get any test, try again
Sleep,200
; Send a Ctrl C to copy the current selection
SendInput, {Ctrl down}c{Ctrl up}
; Get the current selection from the clipboard
selectedText=%Clipboard%
}
; Restore the clipboard
clipboard=%clipboardPrev%
MsgBox, %selectedText%
return