C#, messages supposed to be among each other - c#

i created a windows forms-app with visual studio and im programming in c#.
I have one textbox, one richtextbox and one button. If im writing something in the textbox and press the button, the text from the textbox is shown in the richtextbox (just like a simple chat). My problem is, that if i already wrote something and add something new, the old text in the richtextbox gets replaced by the new one. I want that everything i wrote is among each other.
My source code:
string message = textbox1.Text;
richTextBox1.Text = "Name:" + message;

Of course; you set the Text of the richtextbox to a new text each time you access it
Consider using richTextBox1.AppendText(message) instead

Using the equals operator replaces the value in the .Text property. If you'd like to append text, use the += operator:
string message = textbox1.Text;
richTextBox1.Text += "Name:" + message;
Alternatively, you can skip the shorthand and say:
string message = textbox1.Text;
richTextBox1.Text = richTextBox1.Text + "Name:" + message;
Or, use AppendText():
richTextBox1.AppendText(message)

Related

Add extra text while databinding

I'm trying to add some extra text to the following code but it doesn't seem to work whatever I do. The information I want to show in a label comes from a DataBinding. But I can't manipulate it like a usual string
lblPower.DataBindings.Add("Text", BindingSourceMachineProfiles, nameof(MachineProfile.NominalPower));
At the moment the label only displays a number but I would like to add before the description and at the back the units.
So I would like to have e.g. "Speed" + code from above + "km/h"
How could I do this using the current code?
I have found a solution by using Binding.Format Below I have made an example of how I use it in my app.
var SpeedBinding = new Binding("Text", BindingSource);
SpeedBinding.Format += delegate (object sentFrom, ConvertEventArgs convertEventArgs)
{
convertEventArgs.Value = "Speed: " + convertEventArgs.Value + " km/h";
};
lblPower.DataBindings.Add(SpeedBinding);
This gives me nice results

C# Auto Move to Next Line of Multiline Textbox

I've been tasked with creating a feedback dialogue box to work in conjunction which offers the user simple text based feedback on what operations are occurring. E.g. master switch turned on, hob 1 turned on. I got the text response to display in a multiline textbox, but can only get it to write on the first line therefore overwriting whatever was previously there before. Is there a way to get new text responses to appear on the line below any previous inputs? This is where I'm up to:
if(MasterOnOff.Checked == true)
{
FeedbackTextBox.Text = "Master Switch On";
}
if(MasterOnOff.Checked == false)
{
FeedbackTextBox.Text = "Master Switch Off";
}
Working in C# Windows Forms, any help massively appreciated!
Try
multilineTextBox.Text += "Your message" + Environment.NewLine;
it´s equal to
multilineTextBox.Text = multilineTextBox.Text + "Your message" + Environment.NewLine;
Instead of FeedbackTextBox.Text = "..." which replaces the text in your textbox, try to append to the end of it by using += instead:
FeedbackTextBox.Text += "Master Switch Off\n".
which is the same as
FeedbackTextBox.Text = FeedbackTextBox.Text + "Master Switch Off\n"
The \n part is just the character for new line so that it doesn't add everything as a single line.

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
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)
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.
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.

C# Text box remaining empty

I'm calling a public method from another class. It takes in a List as a parameter, and goes through the list printing out each item into a text field. The problem is the text field is remaining empty!. I've checked that the list is populated by outputing the item to the console before I put it into the text box, and the text is coming up fine there.
The list contains strings, and should output each string to the textfield followed by a semi colon.
This is the method which is being called:
public void fillAttachment(List<string> attachList)
{
for (int i = 0; i < attachList.Count; i++)
{
Console.WriteLine("List: " + attachList[i]);
txtAttach.Text += attachList[i] + ";";
}
}
I would solve it in this way:
foreach(var attach in attachList)
{
Console.WriteLine(attach);
txtAttach.AppendText(string.Format("{0};", attach));
}
Setting the text property on a text box and it not displaying could be one of the following:
You are not looking at the same control as you are setting the text in
Could you have instantiated a second copy of the form object and it is this form that you are setting the txtAttach text property in?
Could the control that you are expecting to be populated be a different one? Right click the text box that you want the text to appear in click properties and check the name.
Something else is clearing the textbox after you set it
Right click the txtAttach.Text and click Find All References, this will show you all the places that the Text property is referenced - written and read - in your project. This is a very useful way to locate other interaction with this control.
Fomatting is making the text box appear empty
Is the Font too small, or in the same colour as the background. Can you select the text in the text box?
The easiest way to test all of the above is to create a new text control on your form with a different name, change your code to populate it, check that it is indeed populated, then replace the old one.
As an aside, you could also reduce the code with a single line as follows:
public void fillAttachment(List<string> attachList)
{
txtAttach.Text = String.Join(";", attachList.ToArray());
}
Although this obviously skips out the console write line function.
Not sure why yours doesn't work but I would have done it like this...
public void fillAttachment(List<string> attachList)
{
string result = "";
//OR (if you want to append to existing textbox data)
//string result = txtAttach.Text;
for (int i = 0; i < attachList.Count; i++)
{
Console.WriteLine("List: " + attachList[i]);
result += attachList[i] + ";";
}
txtAttach.Text = result;
}
Does that work for you? If not then there is something else very wrong that is not obvious from your code

How to add text in a multiline textbox?

I have to add details of my file into a multiline textbox. But all the details are getting added in a single line in the text box and not in a vertical sequence. I used Environment.NewLine and also used "\r\n", but it's not of any help. I have ticked the multiline text box in a Windows Forms form and also set it to true but to no avail.
My line of code is like this:
m_Txt.Multiline = true;
m_Txt.Text = fileInfo.m_Title + "\r\n" +
fileInfo.m_Identifier + Environment.NewLine +
fileInfo.m_TotalTime;
A cleaner answer is:
Assuming txtStatus is a textbox:
txtStatus.Multiline = True;
txtStatus.Clear();
txtStatus.Text += "Line 1" + Environment.NewLine;
txtStatus.Text += "Line 2" + Environment.NewLine;
Using the built in enumeration means cleaner code.
Shift+Enter
In the Visual Studio resource editor, you can hit "Shift + Enter"
to create a new line, as doing something like "\r\n" will get escaped
out. You will also need to increase the cell height to see both
lines as it does not auto-size.
If you're doing it programatically, append the new line to m_Txt.Lines, which is a string[].
m_Txt.Lines = new string[]{ fileInfo.m_Title, fileInfo.m_Identifier, fileInfo.m_TotalTime};
Not sure why your code would not work unless something else is going on.
I just created a WinForms project using C#, added a textbox, set it multiline and added the following code - works a charm.
textBox1.Text = "a\r\nb";
I just wrote this code, seems to be working fine.
public void setComments(String comments)
{
String[] aux;
if(comments.Contains('\n')) //Multiple lines comments
{
aux = comments.Split('\n');
for (int i = 0; i < aux.Length; i++)
this.textBoxComments.Text += aux[i] + Environment.NewLine;
}
else //One line comments
this.textBoxComments.Text = comments;
}

Categories