Skip to content

Instantly share code, notes, and snippets.

@mam4dali
Created May 22, 2019 13:15
Show Gist options
  • Save mam4dali/aeb979852743fc4077e05f272fd26c30 to your computer and use it in GitHub Desktop.
Save mam4dali/aeb979852743fc4077e05f272fd26c30 to your computer and use it in GitHub Desktop.
using System;
using System.Text.RegularExpressions;
public class Test
{
public bool AddToTexBoxOfLine(TextBox box, int lineNumber, string text)
{
if (lineNumber <= 0)
throw new Exception("The number must be 1 or larger\nNumbers smaller than 1 are not acceptable");
string textbox_text = box.Text;
//var split = textbox_text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var split = Regex.Split(textbox_text, "\r\n|\n\r|\r|\n|"+ Environment.NewLine);
int countElement = (lineNumber > split.Length ? lineNumber : split.Length);
string[] newColeccion = new string[countElement];
//no move location
lineNumber = lineNumber - 1;
for (int i = 0; i < lineNumber; i++) newColeccion[i] = "";
for (int i = 0; i < split.Length; i++) newColeccion[i] = split[i];
for (int i = 0; i <= newColeccion.Length; i++)
{
if(i == lineNumber)
{
newColeccion[i] = text;
break;
}
}
box.Text = string.Join(Environment.NewLine, newColeccion);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment