Rick
Rick

Reputation: 23

display a string in textbox

I want to get the data from my serial port and display this data in a textbox. but when i run mu code it displays just one line in the textbox and it gets replaced by the next. but I want the each part of the string under the next one.

private void button1_Click(object sender, EventArgs e)
{           
    SerialPort serP = new System.IO.Ports.SerialPort("COM3", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);

    while (true)
    {
        serP.Open();
        serP.WriteLine("test");
        string dataIn = serP.ReadLine();
        textBox1.Text = dataIn;             
        serP.Close();
    }
}

this is my code, I hope some one can help me out with this. Rick

Upvotes: 0

Views: 101

Answers (3)

Maxi Krone
Maxi Krone

Reputation: 1

I agree with Pavan. If you want to use more than one line i would prefer RichtTextBox, too.

Also if you only want to display something, you could use a simple label, too.

I think thats much nicer for a user than to have it in a TextBox, but only if you want to display something without using it in other things.

Upvotes: 0

CodeTantric
CodeTantric

Reputation: 140

I would go with Andy Korneyev's answer. But if you want each part in a new line you could alter his code like below,

textBox1.Text += dataIn + Environment.NewLine;

Also if you want to set the textbox as MultiLine in code behind,

textBox1.TextMode = TextBoxMode.MultiLine;

Note: I assume you are working with WinForms. If so, you can also use RichTextBox control. It doesn't need to be set has MultiLine.

Upvotes: 0

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

Just concatenate your text:

textBox1.Text += dataIn + Environment.NewLine;             

And make sure your textbox is multiline (textBox1.Multiline = true for standart Windows.Forms.TextBox or somethimg similar if it's textbox from some controls library)

Upvotes: 3

Related Questions