Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have 5 textboxes and a richtextbx of 5 line. how can i get 1st line text of richtextbox in 1st textbox, 2nd line text of richtextbox in 2nd textbox, and so on.

C#
private void myRichTextBox_Click(object sender, EventArgs e)
        {
            string[] lines = richTextBox1.Lines;
            if (lines.Length >= 5)
            {
                myTextBoxForLineOne.Text = lines[0];
                myTextBoxForLineTwo.Text = lines[1];
                myTextBoxForLineThree.Text = lines[2];
                myTextBoxForLineFour.Text = lines[3];
                myTextBoxForLineFive.Text = lines[4];
            }
        }






suppose if we left myTextBoxForLineTwo blank then it shows blank space in myRichTextBox.
how to remove this blank space from myRichTextBox.
Posted

There are quite a lot of ways to do that - the most obvious is:
C#
List<string> nonBlankLines = new List<string>();
foreach (string s in lines)
   {
   if (!string.IsNullOrWhiteSpace(s)) nonBlankLines.Add(s);
   }
But you could use any of half a dozen other methods.
 
Share this answer
 
hi,

try this code...,


create control array in coding for your 5 textboxes.

declare textbox control array in class.
private TextBox[] textBoxes;


and follow the below code..

C#
string[] lines = richTextBox1.Lines;
textBoxes = new TextBox[] { myTextBoxForLineOne, myTextBoxForLineTwo, myTextBoxForLineThree, myTextBoxForLineFour, myTextBoxForLineFive};
int txtboxCnt = 0;

if (lines.Length >= 5)
{
    for  (int i=0; i< lines.Length; i++)
    {
        if (lines[i].Trim() != "")
        {
            textBoxes[txtboxCnt].Text = lines[i];
            txtboxCnt++;
        }
    }
}




regards,

Prakash.T
 
Share this answer
 
v2
Comments
arvindnitin7 27-Feb-13 23:26pm    
Its working opposite. I want to remove whitespace only of richtextbox but its removing white space of textboxes. thanx for this solution its 80% working

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private TextBox[] textBoxes;
public Form1()
{
InitializeComponent();

}

private void richTextBox1_Click(object sender, EventArgs e)
{
}
}



private void button1_Click(object sender, EventArgs e)
{

richTextBox1.Text = myTextBoxForLineOne.Text + "\n" + myTextBoxForLineTwo.Text + "\n" + myTextBoxForLineThree.Text + "\n" + myTextBoxForLineFour.Text + "\n" + myTextBoxForLineFive.Text;
myTextBoxForLineOne.Text = ""; myTextBoxForLineTwo.Text = ""; myTextBoxForLineThree.Text = ""; myTextBoxForLineFour.Text = ""; myTextBoxForLineFive.Text = "";
label2.Visible = true;


string[] lines = richTextBox1.Lines;
textBoxes = new TextBox[] { myTextBoxForLineOne, myTextBoxForLineTwo, myTextBoxForLineThree, myTextBoxForLineFour, myTextBoxForLineFive };
int txtboxCnt = 0;

if (lines.Length >= 5)
{
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Trim() != "")
{
textBoxes[txtboxCnt].Text = lines[i];
txtboxCnt++;
}
}
}
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900