I am unable to Change the specific string of a multiline TextBox.
suppose first line of multiline textbox is "Hello" & second line is "Bye".But when i trying to change the value of second line like below.
textBox1.Lines[1] = "Good bye";
When I saw the result using Debug mode it was not "Good bye".
I also read this MSDN article & this stackoverflow question but can't get the desired answer.
As MSDN states (the link you provided):
By default, the collection of lines is a read-only copy of the lines in the TextBox.
To get a writable collection of lines, use code
similar to the following: textBox1.Lines = new string[] { "abcd" };
So, you have to "take" Lines collection, change it, and then return to TextBox. That can be achieved like this:
var lines = TextBox1.Lines;
lines[1] = "GoodBye";
TextBox1.Lines = lines;
Alternatively, you can replace text, like Wolle suggested
First you need assign textBox1.Lines array in variable
string[] lines = textBox1.Lines;
Change Array Value
lines[1] = "Good bye";
Reassign array to text box
textBox1.Lines=lines;
According to MSDN
By default, the collection of lines is a read-only copy of the lines
in the TextBox. To get a writable collection of lines need to assign
new string array
Working with TextBox lines via Lines property are extremely ineffective. Working with lines via Text property is a little better, but ineffective too.
Here the snippet, that allows you to replace one line inside TextBox without rewriting entire content:
public static bool ReplaceLine(TextBox box, int lineNumber, string text)
{
int first = box.GetFirstCharIndexFromLine(lineNumber);
if (first < 0)
return false;
int last = box.GetFirstCharIndexFromLine(lineNumber + 1);
box.Select(first,
last < 0 ? int.MaxValue : last - first - Environment.NewLine.Length);
box.SelectedText = text;
return true;
}
You could try to replace the text of second line like this:
var lines = textBox.Text.Split(new[] { '\r', '\n' }).Where(x => x.Length > 0);
textBox.Text = textBox.Text.Replace(lines.ElementAt(1), "Good bye");
Related
So, I've been learning C# and I need to remove everything after the
":" character.
I've used a StreamReader to read the text file, but then I can't use the Split function, then I tried it by using an int function to import it, but then again I can't use the Split function?
What I want this to do is import a text file that's written like;
name:lastname
name2:lastname2
And so that it only shows name and name2.
I've been searching this for a couple of days but I can't seem to figure it out!
I don't know what I'm doing wrong and how to import the text file without using StreamReader or anything else.
Edit:
I'm trying to post something to a website that goes like;
example.com/q=(name without ":")
Edit 2:
StreamReader list = new StreamReader(#"list.txt");
string reader = list.ReadToEnd();
string[] split = reader.Split(":".ToCharArray());
Console.WriteLine(split);
gives output as;
System.String[]
You've got a few issues here. First, use File.ReadLines() instead of a StreamReader, its much simpler and easier:
IEnumerable<string> lines = File.ReadLines("path/to/file");
Next, your lines variable needs to be iterated so you can get to each line of the collection:
foreach (string line in lines)
{
//TODO: write split logic here
}
Then you have to split each line on the ':' character:
string[] split = line.Split(":");
Your split variable is an array of string (i.e string[]) which means you have to access a specific index of the array if you want to see its value. This is your second issue, if you pass split to Console.WriteLine() under the hood it just calls .ToString() on the object you have passed it, and with a string[] it won't automatically give you all the values, you have to write that yourself.
So if your line variable was: "name:Steve", the split variable would have two indexes and look like this:
//split[0] = "name"
//split[1] = "Steve"
I made a fiddle here that demonstrates.
I your file size small and your name:lastname in one line use:
var lines = File.ReadAllLines("filaPath");
foreach (var line in lines)
{
var array = line.Split(':');
if (array.Length > 0)
{
var name = array[0];
}
}
if name:lastname isn't in new line tell me how it's seprated
Hello I am new to C Sharp & Windows Forms. I am unable to set the specific string of a multiline TextBox. I have tried below things so far.
textBox1.Lines[1] = "welcome to stackOverflow";
The above code does not give a compile time error but when I saw the result using Debug mode it was not expected.
Then i was also reading this MSDN article but in this there is a new collection created by using stream[] constructor but still the same problem arises.
It should give compiler error because you are trying to assign a string to char here:
textBox1.Text[1] = "welcome to stackOverflow";
Text property is of type string, when you use indexer on a string it gives you the char at that position. And also string is immutable so you can't really change a character at specific position without creating a new string.
You should set the Text directly like this:
textBox1.Text = "welcome to stackOverflow";
Or if you have more than one line in an array of string you should set the Lines property:
var lines = new [] { "foo", "bar" };
textBox1.Lines = lines;
Any value that you set directly to textBox1.Lines will be effected to textBox1.
There is a solution to resolve your problem. I think it's best way.
You have to clone the current value of your textbox. Then you set new value on it. Finally, you set back to textbox.
var curValue = (string[])textBox1.Lines.Clone();
curValue[1] = "welcome to stackOverflow";
//Set back to textBox1
textBox1.Lines = curValue;
I have a list of strings stored in an ArrayList. I want to split them by every occurrence of ';'. The problem is, whenever I try to display them using MessageBox, there's an excess space or unnecessary value that gets displayed.
Sample input (variable = a):
Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;
Below is a line of code I used to split them:
string[] display_document = (a[0] + "").Split(';');
Code to display:
foreach (object doc_properties in display_document)
{
TextBox aa = new TextBox();
aa.Font = new Font(aa.Font.FontFamily, 9);
aa.Text = doc_properties.ToString();
aa.Location = new Point(pointX, pointY);
aa.Size = new System.Drawing.Size(80, 25);
aa.ReadOnly = true;
doc_panel.Controls.Add(aa);
doc_panel.Show();
pointY += 30;
}
The output that displays are the following:
How do I remove the last occurrence of that semicolon? I really need help fixing this. Thank you so much for all of your help.
Wouldnt It be easiest to check if the input ends with a ";" before splitting it, and if so remove the last character? Sample code:
string a = "Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
if (a.EndsWith(";"))
{
a = a.Remove(a.LastIndexOf(";"));
}
//Proceed with split
Split will not print last semicolon if no space character is added and your input is a string.
I don't know why you prefer an array list (which probably is the reason of this strange behaviour) but if you could use your input as a string you could try that
string a = "Arial;16pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
string[] display_document = a.Split(';');
foreach (object doc_properties in display_document)
{
//The rest of your code
}
I have a program which needs to determine the number of lines in a multiline textbox to know how to process it. I am calling the TextBox.Lines.Length property, which was working. Now however, no matter how many lines of text are visible in the GUI, this value is 1, and all of the "\r\n" strings have disappeared from the TextBox.Text string. Any Ideas? My code is as following :
TextBox.MultiLine = true;
TextBox.WordWrap = true;
for (int i = 0; i < TextBox.Lines.Length - 1; i++)
//Some Code
As I said in the comment, with Multiline=True and WordWrap=True, your textbox will display a long line as multilines (Wrapped)... but actually it is one single line, and that's why your Lines.Length=1, try type in some line break yourself, and test it again. Or you can set WordWrap=False, and you will see there is only one line...
It will need to be marked as multiline, check that, you can parse like so:
string txt = TextBox1.Text;
string[] lst = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
I have a long text file ... I read the text file and store the content in a string...
Now I want this text to split. The below is an image which shows what I want.
In the image "This is common text" means this string is common in every paragraph.
Green squares shows that I want that part in string array.
but how o do that... I have tried Regular expression for this... but isn't working....
please help
Try using RegEx.Split() using this pattern:
(.*This is common text.*)
Well, giving priority to RegEx over the string functions is always leads to a performance overhead.
It would be great if you use: (UnTested but it will give you an idea)
string[] lines = IO.File.ReadAllLines("FilePath")
List<string> lst = new List<string>();
List<string> lstgroup = new List<string>();
int i=0;
foreach(string line in lines)
{
if(line.Tolower().contains("this is common text"))
{
if(i > 0)
{
lst.AddRange(lstgroup.ToArray());
// Print elements here
lstgroup.Clear();
}
else { i++; }
continue;
}
else
{
lstgroup.Add(line)
}
}
i = 0;
// Print elements here too
I am not sure what you want to split on but you could use
string[] stringArray = Regex.Split(yourString, regex);
If you want a more concrete example you will have to (as others mentioned) give us more information regardning what the text looks like rather than just "common text".