Insert text at cursor position - c#

I need to insert text from a variable at the cursor position no matter in which window/program it is located, so whenever the C# or VBA (preferably C#) code will run it will just write that text from the variable.
PD: im using VoiceBot to create custom scripts, C# default script looks like this:
using System;
using System.Drawing;
public static class VoiceBotScript
{
public static void Run(IntPtr windowHandle)
{
var myText = "This is a simple text";
//how to add this text variable to cursor position?
}
}
To clarify: VoiceBot can run C# or Visual Basic scripts on voice commands, after writing the script it will be triggered by voice. https://www.voicebot.net/ I need to run this script for example when playing a game and chat is selected, to warn player about X.

You can use SendKeys to simulate the keyboard and send keystrokes to the currently active application. For example:
SendKeys.Send("+This is a simple text");
Note that as you are simulating the keyboard, you need to explicitly invoke the Shift key (with the + character, as in the example) to get an uppercase character. There are some other caveats including other characters you have to escape, so do not simply feed this method a user-provided string without processing it first. For more information, see the documentation.

Related

Visual Studio Find and Replace change casing

I'm working in a group that's converting an old WinForms app to a WPF app, using the MVVM pattern. Thus, we're having to write a lot of large models. I thought I'd write a find-and-replace regex so we can write our private variables:
private string myString;
and do a find-and-replace across the large file and get:
private string myString;
public string MyString
{
get { return myString; }
set { SetProperty("MyString", ref myString, value); }
}
this isn't too bad, the only issue I have is converting myString to MyString.
Is there a way, using straight regex capture groups, to replace a character with it's uppercase version, within Visual Studio's find-and-replace? All my searches just turn up using C# code to do the conversion, which obviously isn't possible in this context.
When I come across this issue, the sometimes fastest way is to replace everything with something completely different at first like "MyNOTString", and then back to "MyString" to get the right capital letters. It does not seem that you can get what you are asking for in Visual Studio without using Macros and such.
Another solution would be to open all the project files in a different tool like Notepad++, which supports writing /U to turn something into uppercase. See this answer.

RichTextBox prints new line upon receiving a certain character

I'd like to know if it is possible for the richtextbox or textbox multi lines to print in a new line upon receiving a certain character like "Z"?
I'm having a big time problem in getting values from a richtextbox. My microcontroller is sending a string in a non-constant speed to the C# program so any way that I use to extract the numbers from the string in a richtextbox is useless I think.
If my circuit sends this kind of string:
Sensor1: 0.10 meter/s \nSensor2: 0.50 meter/s
and then the c# program receives it and detects the \n, the next characters/string will be printed in a new line until it detects the another \n.
Is this possible? if not, what way can I use?
Assume you have a RichTextBox Control named RichTextBox1. Just replace your \n with ControlChars.Lf
Code in Visual Basic
RichTextBox1.Text = RichTextBox1.Text.Replace(ControlChars.Lf, "\n")
Code in C#
RichTextBox1.Text == RichTextBox1.Text.Replace(ControlChars.Lf, "\n")
You need to reference Microsoft.VisualBasic, parent of ControlChars.Lf. That goes without saying.

Need help for displaying characters as a mirror at the same time as writing input

I am new to this forum and working with Visual Studio as a beginner.
My teacher gave me an assignment to perform in Visual Studio but I don't have idea about it to implement
The assignment is to accept any input and simultaneously get it's output in mirrored form without the need to press Enter.
For example,
If I am writing MESSI then at the the same time output should be ISSEM without pressing ENTER.
I tried my level best but without pressing ENTER I didn't get results. Here is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test11
{
class Class1
{
static void Main(string[] args)
{
string s;
Console.WriteLine("Enter characters:");
s = Console.ReadLine();
char[] arr = s.ToCharArray();
Array.Reverse(arr);
string r = new string (arr);
Console.WriteLine( s + " " + r);
}
}
}
So kindly help me for this problem
Regards,
OSCAR
The reason you need to press enter with your code is that you are using Console.ReadLine, which doesn't return until you press enter. You will want to look at using Console.ReadKey, which will return each key as it is entered, and pass in true for the intercept parameter so that the input is not echoed to the console. Look at the docs for ConsoleKeyInfo so you know how to interpret the input.
Since it's homework, I don't want to just give you the code, but here's some pseudo code to get you started. If you run into specific issues, ask for further help.
initialize input string // use a StringBuilder
loop forever
{
use Console.ReadKey(true) to get the next character
if (user pressed enter)
exit the loop // need some way to escape
if (user entered printable character) // ignore control characters
{
add character to input string
reverse string
print reversed string
move cursor to beginning of line // Console.Write('\r') will work here
}
}
If you want to allow the backspace key to delete characters, you can check for that key, but you will also need to erase the previous output (just write spaces over it) before printing the new string, since it will be shorter.
You've already got most of the code, so hopefully this points you in the right direction to work out the rest.

C# replace certain string when typing in any application

I want to make an application that will replace a certain string when I type in any textfield of any application (online textboxes, notepad, word, email, etc..)
For example if I'm writing in notepad++ and I type [for] and press space or enter I want my C# application to work in background, access the field and replace that string with a predefined string in my C# code.
The result would for example be:
for($i = 0; $i < X; $i++)
{
// ....
}
For example if I'm writing a word document and I input [FIRSTPAGE] I would want that to be replaced with a random string I setup early.
Later on I will setup an app that will let me change these on the fly.
I tried searching google but I found no information on anything similar.
I just need to find a way to replace a string in any textfield.
Hope this makes sense. Thanks for your help.
Create a global keyboard hook, C# : Keyboard Hook shows how you can do it.
Once hook is created monitor handle the keylog for typed word. Once the typed word is found use SendKeys to send keystrokes virtually.
There's a really cool library called Scintilla.NET - http://scintillanet.codeplex.com/
It's usually used for building your own code editor with syntax highlighting support.
But it has the auto-suggest feature you are after, i.e:

How do I read a tab-delimited file into a List array?

I am firing up Visual C# (2010 Express) for the first time, and I've created a new project for a WindowsFormsApplication. No code has been written yet, but I created a button and placed it on Form1. Then, I double-clicked the button and am taken to the part of the code where you write what happens when the button is push/clicked.
The first thing I would like to do is read data from a LARGE tab-delimited text file (30MB). The text file contains 7 structured columns of data like names, age, favorite color, animal, etc. Nothing tricky or fancy in the text formatting. I'm using the code below:
File.ReadLines(sourceFilePath)
.Select(line => line.Split('\t'))
.ToArray();
But my more basic question is how do I establish and define File and sourceFilePath? With the code above I get "The name 'File' does not exist in the current context.
You need to add the following line to the top of your C# file:
using System.IO;
This will allow the use of the File class, which is in the System.IO namespace.
As for defining sourceFilePath, that's just a variable, which you can declare and set to whatever file path you need, e.g.
string sourceFilePath = #"c:\data\file.csv";
Note the use of # before the string literal; this prevents the backslashes from being treated as the start of escape sequences. You can instead just escape the bakslashes, e.g.
string sourceFilePath = "c:\\data\\file.csv";
If you want to split by tab key then you can try using ReadAllText method, and then a Split method, where you define delimiter (by tab):
string[] delimitedByTab = File.ReadAllText(#"file").Split('\t').ToArray();
And use System.IO; namespace for File class
The File class resides the System.IO namespace. You leverage it in your application with the following:
using System.IO;
As for defining the contents of sourceFilePath, you are going to need either to hard-code the value to a fixed location, which is not always advisable, or devise a mechanism for the user to specify that path, possibly through one of the various CommonDialogs that are available. Some research on the CommonDialogs should help push you a bit further in developing your project.

Categories

Resources