Torna al Thread

private void Formatta69Battute(TextBox txtb1) { List<string> lst = new List<string>(); char[] split = { '\n' }; //carica la lista for (int i = 0; i < txtb1.Lines.Length; i++) { lst.Add(txtb1.Lines[i] + "\n"); } //inizia la procedura di controllo sulla 69° battuta for (int i = 0; i < lst.Count; i++) { if (i != lst.Count - 1) { //la lunghezza va "depurata" delle sequenze dei caratteri "\n" if (lst.ElementAt(i).TrimEnd(split).Length > 69) { int c = 69; //ciclo per individuare il primo spazio antecedente la parola che //comprende il 69° carattere while (lst.ElementAt(i).Substring(c).StartsWith(" ") == false) { c--; } //sequenza di "taglio e cucito"... :-) lst.Insert(i + 1, lst.ElementAt(i).Substring(c + 1) + lst.ElementAt(i + 1)); lst.RemoveAt(i + 2); lst.Insert(i, lst.ElementAt(i).Substring(0, c)); lst.RemoveAt(i + 1); //fa un check alla linea inserita (i+1) per rintracciare eventuali \n presenti (nel caso si sia incollato un messaggio //già preformattato......e, //qualora li individua, splitta la riga e manda a capo il pezzo finale.... if (lst.ElementAt(i + 1).Split(split, StringSplitOptions.RemoveEmptyEntries).Length > 1) { string[] _str = lst.ElementAt(i + 1).Split(split, StringSplitOptions.RemoveEmptyEntries); for (int a = 0; a < _str.Length; a++) { lst.Insert(a + i + 2, _str[a] + "\n"); } lst.RemoveAt(i + 1); } } } else//caso dell'ultima riga....come sopra... { if (lst.ElementAt(i).Length > 69) { int c = 69; while (lst.ElementAt(i).Substring(c).StartsWith(" ") == false) { c--; } lst.Add(lst.ElementAt(i).Substring(c + 1)); lst.Insert(i, lst.ElementAt(i).Substring(0, c)); lst.RemoveAt(i + 1); if (lst.ElementAt(i + 1).Split(split, StringSplitOptions.RemoveEmptyEntries).Length > 1) { string[] _str = lst.ElementAt(i + 1).Split(split, StringSplitOptions.RemoveEmptyEntries); for (int a = 0; a < _str.Length; a++) { lst.Insert(a + i + 2, _str[a]); } lst.RemoveAt(i + 1); } } } } //ELIMINA TUTTI I CARATTERI \N CHE MI GENERANO RIGHE VUOTE (dal momento che //la trasformazione in array mi inserisce automaticamente \n in fondo ad //ogni riga.... string[] _str2 = new string[lst.Count]; int n = 0; foreach (string str in lst) { _str2[n] = str; n++; } txtb1.Lines = _str2; for (int i = 0; i < _str2.Length; i++) { _str2[i] = _str2[i].TrimEnd(split); } txtb1.Lines = _str2; }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5