Hello. I do not know how to implement such a thing. I need that if in textbox1 the first character is minus, then it would automatically copy it into textbox2. How to implement it?
- 2if (MyTextBox.Text [0] == '-') MyTextBox2.Text = MyTextBox.Text; - EvgeniyZ 5:46 pm
- This sink completely copies textbox1, but I need to copy the first character exactly - Vlad
- one@ Vlad will show your broken (for now) solution? :) - Alias
- Here, in fact, there are three questions in one: (1) how to check the first character in a line (2) how to add a character to a line (3) how to change the contents of TextBox. Without an example, it is unclear exactly what part of the problem arose. - default locale
1 answer
Look, I'll just explain what's what.
To begin with, any string value can be divided into letters ( char ). That is, we have for example the string string hello = "World!"; , if we go through it cycle, what will happen? Let's check...
string hello = "World!"; foreach (char c in hello) { Console.WriteLine(c); } Hmm, our output will be the following:
W o r l d ! That is, we can conclude that string itself implements a certain interface, which allows us to use it as an array of characters without unnecessary actions. Then why don't we just refer to the desired character (if this is an array)? Let's try:
string hello = "World!"; Console.WriteLine(hello[0]); The result will be:
W Well, in the end we figured out how to get the necessary letter from the string. By the way, I unfortunately do not know which version of the language this all came from, but as an option, it is to break the string into characters myself, for this there is the ToCharArray() method. We break the line into characters, and we work as before:
string hello = "World!"; char[] myChars = hello.ToCharArray(); Console.WriteLine(myChars[0]); Ok, we figured it out. What do we need next?
I need that if in textbox1 the first character is minus, then it would automatically copy it into textbox2.
Knowing how we can “pull out” the required letter from a string , it now becomes a rather simple task. First, textbox1 check what the textbox1 value begins with. For this we need if , previous knowledge, well, or the StartsWith() method.
We will write using the knowledge obtained above:
if (textBox1.Text[0] == '-') { Console.WriteLine(true); } After this code, our program will check the written text to match the first character. If it coincided with the one given to us (in this case, it - ), then we output True .
StartsWith() same and use StartsWith() . What is it all about? This is one of the good helpers that ( StartWith() and EndsWith() ), its task is to compare the first (or last) character / characters. At the input, it takes a string value, and the output will bool a bool result. Let's try:
if (textBox1.Text.StartsWith("-")) { Console.WriteLine(true); } The result will be the same as in the example above.
Well, with checking the character seems to have figured out. Now we solve this:
I need to copy the first character
Having collected all that we have learned above, we can now do the following without difficulty:
if (textBox1.Text[0] == '-') { textBox2.Text = textBox1.Text[0].ToString(); } What's going on here? Hmm, we checked the value in textBox1 on the starting character, if it matches, then we transfer this character to textBox2 . So, as textBox2.Text is a string value, and a character is a char , then we need to translate from char to string (as .ToString() does).
Also a moment! If we need not the first character, but something after - , then it is not difficult to guess, we can indicate it, replacing [0] with the desired value !.
And another moment !! Before getting a character, I advise you to check for length, yes, or at least for a null value. Otherwise, we will get an error. You can do before all actions something like:
if (string.IsNullOrEmpty(textBox1.Text)) return; Here we check if the value of textBox1.Text . If yes, then just stop the execution of this method.
Well, now everything seems to be. I hope helped. Good luck!