How to add text in a multiline textbox? - c#

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;
}

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#, messages supposed to be among each other

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)

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.

new line at Text Box at winform

I want to pass a string into a method in form1 class every time an event occurs in another class and then I want to print some text as multi-lined in a TextBox in the Windows Form associated with form1 class.
The thing is I'm getting all the text in that TextBox in a single line and want to print it on a new line:
private void myEvt_valueChnaged(string s)
{
textBox1.Invoke(new Action(() => textBox1.Text = s ));
}
I tried to use += and then I got the text later in the line and not below the line .
I also tried to add +="/n/r" + s and it was printed as is in the Text Box
Then I tried using += Environment.NewLine() + s and got the error: Environment.NewLine cannot be used like a method .
"\n " is good enough for a new line in c#.
String newline = "\n "
textBox1.Invoke(new Action(() => textBox1.Text = newline + s ));
You should set the Multiline property of the textbox control to True and then just call this:
textBox1.Invoke(new Action(() => textBox1.Text = String.Concat(textBox1.Text, Environment.NewLine, s)));
if you want to append the text in a new line and maintain the already present text. Otherwise you can use this to put simply the text in the new line:
textBox1.Invoke(new Action(() => textBox1.Text = String.Concat(Environment.NewLine, s)));
Note that Environment.NewLine is a property not a method.

TextBlock + RichTextBox to clipboard

I'm trying to get the contents of dynamically created TextBlock and dynamically created RichTextboxes (they sit side by side) into the clipboard in c# + wpf, however, I'm not able to do so. I've search all over google to no avail, the latest code I came up with is
StringBuilder clipboard = new StringBuilder();
String rtb = scrlPanel.Children.OfType<RichTextBox>().ToString();
//List<RichTextBox> rtb = scrlPanel.Children.OfType<RichTextBox>().;
foreach(TextBlock txtb in scrlPanel.Children.OfType<TextBlock>())
{
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtb.ToString() + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
but it doesn't work, the codes copies the TextBlocks just fine but the RichTextBoxes content display" "System.Linq.Enumerable+d__aa`1[System.Windows.Controls.RichTextBox]"
Any help or pointers is greatly appreciated.
Thanks,
You are copying the from the ToString() - method, which by default shows the typename.
You should do it like this:
StringBuilder clipboard = new StringBuilder();
List<RichTextBox> rtbs = scrlPanel.Children.OfType<RichTextBox>().ToList();
List<TextBlock> texts = scrlPanel.Children.OfType<TextBlock>().ToList();
foreach(TextBlock txtb in texts)
{
RichTextBox rtb = rtbs[texts.indexOf(txtb)];
string rtbtext = new TextRange(rtb .Document.ContentStart, rtb .Document.ContentEnd).Text;
clipboard.Append(txtb.Text + " " + "::" + Environment.NewLine + rtbtext + Environment.NewLine);
}
Clipboard.SetText(clipboard.ToString());
That is, if you just as many TextBoxes as RichTextBoxes and they have the same order.
PD: There's probably better ways of doing this, but this would be a quick fix.

Categories