I'm currently working on a Program, that presses buttons for me. I'm working on WPF but I already finished my design in XAML, now I need some C# code.
I have a TextBox that should handle all the SendKeys input. I want to extend it's functionality by providing some CMD-like arguments. The problem is, I don't know how. ;A; For example:
W{hold:500}{wait:100}{ENTER}
This is a example line that I'd enter in the textbox. I need 2 new functions, hold and wait.
The hold function presses and holds the previous key for the specified time (500 ms) and then releases the button.
The wait function waits the specified time (100ms).
I know I could somehow manage to create this function but it would end up being not user editable. That's why I need these arguments.
You're trying to 'parse' the text in the text box. The simplest way is to read each character in the text, one by one, and look for '{'. Once found, everything after that up until the '}' is the command. You can then do the same for that extracted command, splitting it at the ':' to get the parameters to the command. Everything not within a '{}' is then a literal key you send.
There are far more sophisticated ways of writing parsers, but for what it sounds like you are doing, the above would be a good first step to get you familiar with processing text.
Related
I am trying to figure out a solution in C# to perform exception handling for multiple textboxes using windows forms.
The user can only enter one or two positive integers in these textboxes and if the user tries to enter more numbers or letters, a tooltip should appear with a warning message?
Thanks for the assistance!
For a case like this, I like to do what I call "passive Error Reporting". Rather then throwing exceptions, you take every value (usually strings). But display a message if it does not fit some criteria.
The simple approach is INotifyDataError. It allows you to show one error for each property (it is advantagenous to use a property or a string key in the backend storage).
I know there is a more complex brother that allows Multiple errors per Property/Key. But it is too long since I read it's name, so I can not remember it.
Okay, so I am making a game that reads data from a text file to create events. What happens is as each year advances if something exciting happens a popup box with three options appears, clicking them affects the game and so on.
I have created a function which can take various arguments and make the form automatically - so far so good- but writing large event descriptions in the code is messy and disorganized. Instead I decided to create another function which takes values from a text file, organizes them and then calls the second function to create the 'event'.
Now, as I said each event comes with three choices (See below) and each choice has a consequence on one of three factors (approval, prestige, power), I haven't quite worked out the mechanics properly but all in good time, everything runs wonderfully until I need to load this integers from the text file.
It keeps having trouble converting the string to an integer, why is this and how can I fix it?
Line 11 of the text file: 10 (yes I checked and it is the right line to read)
The code:
List<int> affecta = new List<int>();
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Take(11).First()))
I can load the other things such as the picture file's location perfectly, so 'filename' points to the correct .txt
It might be something really obvious to someone with more experience, but I just can't see why.
I don't think Take does what you think - It grabs the first 11 items and returns all of them, so you get an IEnumerable of 11 items. When you do First on that, you get item at position 0, not position 10. I think you want Skip and then First, which will skip the first 10 items and return the next (11th) item:
affecta.Add(Int31.Parse(System.IO.File.ReadLines(filename).Skip(10).First()))
If you use Take(11) this means "get 11 rows from the source". After that you have First(), so you'll get first of them. You're essentially trying to convert the first line into integer.
I assume you want to use Skip(10) since you want the 11th line.
Take(11).First() returns the First element from the IEnumerable containing the 11 elements.
Instead, Skip the first 10 and select the First from the IEnumerable.
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Skip(10).First()))
Alternatively, Take the first 11 and select the Last from the IEnumerable.
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Take(11).Last()))
I have a windows form program that I would like to 'run' a certain number of times based on a number that the user specifies.
I am using Visual Studio 2013 and C#.
I have a program that has 16 picture boxes and randomly assigns pictures to 5 of these boxes. The person has to select the correct order of pictures. If they do, they are rewarded, if they choose wrong, they are punished by an annoying sound.
My first form has a TextBox that the user can specify the 'numberOfTrials'
I have a second form that takes the value of the TextBox and converts it into an int.
I want to have the main program on my second form run the number of times that the user specifies.
My program does work if I run it once, without using this variable.
I have tried using a for loop inside the method that starts the program, but this did not work. It just made all of the picture boxes white.
I then tried to use the for loop around the InitializeComponent() method but, again, this just made all of the picture boxes white.
My for loop uses the textbox variable as such:
for (int cycles = 0; cycles < numberOfTimesThrough; cycles++)
I create the numberOfTimesThrough variable by parsing the textbox variable.
Perhaps I am doing this wrong?
On the first form:
at the top of the class:
public static string trialNumberString;
inside a method that is called when a confirm button is pressed:
trialNumberString = tbTrialNumber.Text.ToString();
On the second form:
at the top of the class:
//Integer value for the string of trials
public static int numberOfTimesThrough;
bool canConvert = Int32.TryParse(Settings.trialNumberString, out numberOfTimesThrough);
Is this the correct way to get the string value of the textbox on the first form?
I am sure that adding a for loop should make the program repeat itself, so there must be something wrong with the way I am parsing the textbox string to an int.
The people using this program are not wanting to break it so the data entered into the textbox on the first form will always be between 1 and, say, 25. Do I still have to use a try catch around the string conversion?
Any help with this will be greatly appreciated.
You don't need to re-run the entire program just to show a form multiple times: instead, just instantiate multiple instances of the second form and show them in sequence.
To safely parse text as a number, you can use int.TryParse()
You can get the value of the textBox by accessing it's Text property and from there parse the string it returns to an int.
string s = textBox.Text;
int i = int.Parse(s);
Use whatever validation logic is necessary as well.
When application closes, static variables are deleted from memory too. In order to save setting, you need to save it to separate file and then read that. Simple way of read/write settings is using build-in options of ApplicationSettings: http://msdn.microsoft.com/en-us/library/vstudio/a65txexh(v=vs.100).aspx
I have an error provider providing error for 4 controls..
when I set all the four errors, only two of them blink together at a time and all four settle down after certain time..
even if I set two errors, both blink alternatively..
but I want all of them blink together...How can I do this? (I don't prefer using more than one errorProvider)
You've hinted to an (IMO) acceptable solution, with your last statement: use 2 error providers, one dedicated to blinking exactly one control at a time (the latest one with an invalid input, or the one you'd expect to be corrected ASAP, or whatever criteria you deem most important), and a second one that "silently" displays the icon on all controls with invalid input.
I'm starting to play around with the .NET speech recognition in System.Speech.Recognition. I've been able to get some very basic phrases recognized, but in the event handler, I'd like to get at the certain pieces of information as shown in the pizza ordering example.
I could parse values from e.Result.Text using regex, but the pizza ordering example made use of a really handy method called AppendChoices. The beauty of this method is that you essentially associate a list of possible words with a key, and when the event handler is called (after a phrase is recognized), you can access the value by looking at Semantics[<your key string here>]. However, while Semantics is still available, I don't know how to make use of it since it seems that AppendChoices has been deprecated.
Is my only recourse to use regex in the event handler to figure out what the spoken command arguments were?