Multiple character casing in same TextBox - c#

How to change character casing in TextBox? I need that 1 line character been Upper and second line character benn Lower
isv.CharacterCasing = CharacterCasing.Upper;
isv.Text = "Upper"
isv.CharacterCasing = CharacterCasing.Lower;
isv.Text = "Lower"

As Mark said, it's difficult to understand exactly what you need, but I think it's something like
string[] lines = isv.Text.Split('\n');
string finalText = string.Empty;
for (int i = 0; i < lines.length; i++)
finalText += i%2==0 ? lines[i].ToUpper() : lines[i].ToLower() + + Environment.NewLine;
isv.Text = finalText;
Keep in mind I wrote the code without the compiler :)

You can use TextBox.Lines property I guess.
something like:
private void button1_Click(object sender, EventArgs e)
{
string result = string.Empty;
result += textBox1.Lines[0].ToUpper() + Environment.NewLine;
result += textBox1.Lines[1].ToLower();
textBox1.Text = result;
}

isv.Text = isv.Text.Split(Environment.NewLine)[0].ToUpper() + isv.Text.Split(Environment.NewLine)[1].ToLower();

Related

Insert data in RichTextBox C#

My code:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text = infos;
}
timer4.Stop();
}
I want to insert all of 10 src values in RichTextBox, while my code do it only once.
You can use AppendText
Replace
richTextBox1.Text = infos;
with
richTextBox1.AppendText(infos);
OR
richTextBox1.Text += infos + Environment.NewLine;
This line is wrong.
richTextBox1.Text = infos;
This is right.
richTextBox1.AppendText= infos;
What your code is doing is setting the text to equal each infos, 10 times over.
So I'm guessing your output would be the last infos variable? What you might want to do instead is this:
private void timer4_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
var infos = webBrowser1.Document.GetElementsByTagName("img")[a].GetAttribute("src");
richTextBox1.Text += infos; // the "+=" will add each infos to the textbox
}
timer4.Stop();
}
As you can see, if you use the += instead of just =, it will add each iteration to the whole, instead of just overriding the whole value each time.

Backslash n "\n" doesn't work?

Hello I am making an application to split the song text based on certain char. The application works when I type something such as "sampletext/ sampletext", then the result should be :
sampletext
sampletext
but, instead of above, the result is
sampletextsampletext
Below is the code :
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "";
string[] a = TextToSongFormatConverter(textBox1.Text, '/');
textBox2.Text += "\n"; textBox2.Text += "\n"; textBox2.Text += "\n";
foreach (var item in a)
{
textBox2.Text += item;
textBox2.Text += "\n";
}
}
public static string[] TextToSongFormatConverter(string text, char separator)
{
string[] result;
result = text.Split(separator);
for (int i = 0; i < result.Length; i++)
{
result[i] = result[i].Trim();
}
}
I am using visual studio 2012 and c# and windows form :
This is the screenshoot :
Any idea why? Thanks.
Use Environment.NewLine instead. It will give you proper new line characters for system your program is running on.
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
from Environment.NewLine Property
Try using Environment.NewLine instead of \n.
MSDN

How to display a converted string

I'm working on an assignment which wants me to have the user input a string with no spaces and to identify each word by having it start with a capital letter, so "IAmName" would convert to "I Am Name". I think I have that part down, my problem is the last step, which is displaying the new string in a label, here is my code so far:
private string ConvertText()
{
string str = inputTextBox.Text;
if (str.Contains(" "))
{
MessageBox.Show("No spaces allowed");
}
string newstring = outputLabel.Text;
for (int i = 0; i < str.Length; i++)
{
if (char.IsUpper(str[i]))
newstring += " ";
newstring += str[i].ToString();
}
return newstring;
}
Any help would be greatly appreciated.
Just do it the other way around:
outputLabel.Text = ConvertText(); // Or any other label that should display it
I'm not sure why you have this (string newstring = outputLabel.Text;) inside your code though. You're not using it, you just overwrite it.
You might also want to use a StringBuilder to concatenate in a loop, it's a lot more efficient.
And this code probably should add a return;
if (str.Contains(" "))
{
MessageBox.Show("No spaces allowed");
return; // Return so it stops executing this method
}
I edited you sample a little
I think it correct as below.
public static class MyClass
{
public static string ConvertText(string inputText)
{
var newstring = "";
for (var i = 0; i < inputText.Length; i++)
{
if (char.IsUpper(inputText[i]))
newstring += " ";
newstring += inputText[i].ToString(CultureInfo.InvariantCulture);
}
return newstring;
}
}
string output = MyClass.ConvertText("MyNameIsBlaBla");
Console.WriteLine(output);
" My Name Is Bla Bla"

Auto formatting a textbox text

I want to auto format a text entered in a textbox like so:
If a user enters 2 characters, like 38, it automatically adds a space. so, if I type 384052
The end result will be: 38 30 52.
I tried doing that, but it's ofr some reason right to left and it's all screwed up.. what I'm doing wrong?
static int Count = 0;
private void packetTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
Count++;
if (Count % 2 == 0)
{
packetTextBox.Text += " ";
}
}
Thanks!
It's much nicer if you just let the user type and then modify the contents when the user leaves the TextBox.
You can do that by reacting not to the KeyPress event, but to the TextChanged event.
private void packetTextBox_TextChanged(object sender, EventArgs e)
{
string oldValue = (sender as TextBox).Text.Trim();
string newValue = "";
// IF there are more than 2 characters in oldValue:
// Move 2 chars from oldValue to newValue, and add a space to newValue
// Remove the first 2 chars from oldValue
// ELSE
// Just append oldValue to newValue
// Make oldValue empty
// REPEAT as long as oldValue is not empty
(sender as TextBox).Text = newValue;
}
On TextChanged event:
int space = 0;
string finalString ="";
for (i = 0; i < txtbox.lenght; i++)
{
finalString = finalString + string[i];
space++;
if (space = 3 )
{
finalString = finalString + " ";
space = 0;
}
}
I used
int amount;
private void textBox1_TextChanged(object sender, EventArgs e)
{
amount++;
if (amount == 2)
{
textBox1.Text += " ";
textBox1.Select(textBox1.Text.Length, 0);
amount = 0;
}
}
Try this..
on TextChanged event
textBoxX3.Text = Convert.ToInt64(textBoxX3.Text.Replace(",", "")).ToString("N0");
textBoxX3.SelectionStart = textBoxX3.Text.Length + 1;

Reversed for loop

I made this small nested for loop, and it shows no error in C# whatsoever,
but when I try to run my small program I get the following error in my TextBox:
System.Windows.Forms.TextBox, Text: System.Windows.Forms.TextBox,
Text: Syst...
Here is my code:
int number = textBox.Text..ToString();
for (int row = 0; row < number; row++)
{
for (int x = number - row; x > 0; x--)
{
textBox2.Text = textBox2.Text + "X";
}
textBox2.Text = textBox2 + Environment.NewLine;
}
My result should be something like this:
XXXX
XXX
XX
X
I can't figure out what may cause this error.
You can't assign a string to a number. You need to convert it:
// int number = textBox.Text..ToString();
int number;
if (!int.TryParse(textBox.Text, out number)
{
// Handle improper input...
}
// Use number now
In addition, when you add the newline, you need to actually append to the Text property, not the TextBox itself:
textBox2.Text = textBox2.Text + Environment.NewLine;
Instead of
textBox2.Text = textBox2 +
use
textBox2.Text = textBox2.Text +
in the last line.
That's it ;-)
textBox2.Text = textBox2 + Environment.NewLine;
Should be
textBox2.Text = textBox2.Text + Environment.NewLine;
System.Windows.Forms.TextBox is just class name
You're missing a .Text in the second to last line. It should be:
textBox2.Text = textBox2.Text + Environment.NewLine;
^^^^^
or just:
textBox2.Text += Environment.NewLine;
int number = textBox.Text..ToString();
Suppose that was a typo? Either way, check if the value is numeric first.
if (int.TryParse(textBox.Text, out number))
{
//run your loop here
}
Also,
textBox2.Text = textBox2 + Environment.NewLine;
should be:
textBox2.Text = textBox2.Text + Environment.NewLine;
You cannot assign string to an int , what you are doing as:
int number = textBox.Text..ToString();
Better option is to use int.TryParse(textBox.Text, out number)
AND
Change
textBox2.Text = textBox2 + Environment.NewLine;
to
textBox2.Text = textBox2.text + Environment.NewLine;
Edit:
Even if you change 2 dots to 1, it will give error for int number = textBox.Text.ToString(); - you can't assign string to int
You have two dots here.
textBox.Text..ToString();
This should throw a compilation error by the way. And you can't assign it to a variable of type integer.
textBox2.Text = textBox2 + Environment.NewLine;
You have to call a method of the textbox here, presumably textBox2.Text.
This might inspire you to think about this problem differently:
// I created a simple textbox class so I could do this in a console app
var textBox = new TextBox();
var textBox2 = new TextBox();
textBox.Text = "4";
var number = Convert.ToInt32(textBox.Text);
var descendingXStrings = Enumerable.Range(1, number)
.Select(n => new string('X', n))
.Reverse();
textBox2.Text = string.Join(Environment.NewLine, descendingXStrings);
Console.WriteLine(textBox2.Text);
CW as this does not answer the question directly.
Try this
int number = int.Parse(textBox1.Text);
for (int row = 0; row < number; row++)
{
for (int x = number - row; x > 0; x--)
{
textBox2.Text = textBox2.Text + "X";
}
textBox2.Text = textBox2.Text + Environment.NewLine;
}
Convert TextBox Value in integer
Change textBox2 to textBox2.Text in second last line
Use textBox as multiline textbox

Categories