PriceCheaperton
PriceCheaperton

Reputation: 5349

wrapping strings around each new line in a rich text box using C#

I have 2 rich text boxes in my C# Winforms applications called, richtextbox1 and richtextbox2 I also have a button called button1. The idea is that when the end user pastes in a list of values into richtextbox1 eg,

C1
C2
C3
C4

The result of richtextbox2 should be: (this is what i want help with)

IN ('C1','C2','C3', 'C4')

This is what I have so far:

private void button1_Click(object sender, EventArgs e)
{
    string strValues;
    strValues = richTextBox1.Text;
    //MessageBox.Show(strValues);

    string strInStatement;
    strInStatement = richTextBox2.Text;

    List<string> idsList = new List<string>() { strValues };
    string whereClause = string.Join(",", idsList).ToString();

    richTextBox1.Lines = idsList.ToArray();

    foreach (string value in idsList)
    {
        MessageBox.Show(value);
    }

}

Upvotes: 0

Views: 613

Answers (3)

Sathish
Sathish

Reputation: 4487

Try This Code

 private void button1_Click(object sender, EventArgs e)
        {


    string whereClause = String.Join("','", richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.None));


        MessageBox.Show(" IN ( '" + whereClause + "')");

        }

Upvotes: 0

Sameer
Sameer

Reputation: 2171

Try This :

private void button1_Click(object sender, EventArgs e)
{
     string whereClause = String.Join("','", richTextBox1.Text.Split(new string[] { "\n" }, StringSplitOptions.None));
     richtextbox2.Text = (" IN ( '" + whereClause + "' )");
}

Upvotes: 1

har07
har07

Reputation: 89285

You can try this :

private void button1_Click(object sender, EventArgs e)
{
    var textInEachLine = richTextBox1.Text.Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
    string whereClause = string.Join("', '", textInEachLine).ToString();
    MessageBox.Show(" IN ( '" + whereClause + "')");
}

This code will remove empty lines if any, and wrap text in each line with single quotes.

Upvotes: 1

Related Questions