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.
Related
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
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)
I am having an issue where when I write text to a second file, it display fine in wordpad where it includes the linebreaks, but in notepad all the text for copyText and richTextBox1.Text appear in one really long line.
How can I fix this? If somebody can include a code snippet with my code then it will be much appreciated as I can see what you have done and changed and I can keep it for reference for futr use.
My code is below where I take text from a text file (Testfile.txt) and insert it into richTextBox1, then when I click on button, text is copied to richText Box2 and is written in second file (_Parsed.txt).
I did a message box on text line and it displays text all without line break. I'm not sure if that is issue but I do need help as deadline is tomorrow and this is only thing I need to sort out and I'm done.
string Chosen_File = "C:\\_Testfile.txt";
string Second_File = "C:\\_Parsed.txt";
string wholeText = "";
private void mnuOpen_Click(object sender, EventArgs e) {
//Add data from text file to rich text box
richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
//Read lines of text in text file
string textLine = "";
StreamReader txtReader;
txtReader = new StreamReader(Chosen_File);
do {
textLine = textLine + txtReader.ReadLine() + " ";
}
//Read line until there is no more characters
while (txtReader.Peek() != -1);
richTextBox1.Text = textLine;
txtReader.Close();
}
}
private void Write(string file, string text) {
//Check to see if _Parsed File exists
//Write to _Parsed text file
using(StreamWriter objWriter = new StreamWriter(file)) {
objWriter.Write(text);
objWriter.Close();
}
}
private void newSummaryMethod(string copyText) {
//Write into richTextBox2 all relevant text
copyText = richTextBox1.Text;
wholeText = richTextBox1.Text + copyText
Write(Second_File, wholeText);
}
private void btn1_Click(object sender, EventArgs e) {
newSummaryMethod(copyText);
}
The problem is probably with the line breaks in your text file(s).
TextBoxes expect \r\n sequence as an indication of the end-of-line, and your text files may contain only the \n.
Try to make this change to your code:
do {
textLine = textLine + txtReader.ReadLine() + "\r\n";
}
//Read line until there is no more characters
while (txtReader.Peek() != -1);
The problem is that you are never adding a new line to the file in your code. you are using objWriter.Write(text); which does not generate a new line.
you can use objWriter.WriteLine(text); instead and it will automatically add new line character at the end.
I think the difference in viewing the file is because the "word wrap" option is turned on in "wordpad" and it causes to break a long line into multiple line.
Still new to C# and have been working on this program for sometime off and on added different features and what not. The program is for my job and it logs notes. Essentiall you type all the info into several different textboxes and then click a button and it combines the info into a large output richtextbox and adds some formatting to it and saves to a file and copies the info to the clipboard.
I'm wanting to set it up so that if i have a specific checkbox checked when i hit the button to process everything it will omit some of the formatting. I am using string builder to move the info into the output box.
Here is a sample of some of the code of how i have it working. Please let me know if anymore info is needed i will be happy to submit it.
private void save_button_Click(object sender, EventArgs e)
{
//Starts the Stringbuilder
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
{
string cbrSame = custBtnText.Text;
if (cbrSameCbx.Checked)
{
custCbrText.Text = cbrSame;
}
//Writes textboxes to the stringbuilder
strBuilder.AppendLine("\u2022 CX NAME: " + custNameText.Text);
strBuilder.AppendLine("\u2022 BTN: " + custBtnText.Text);
strBuilder.AppendLine("\u2022 CBR: " + custCbrText.Text);
strBuilder.AppendLine("\u2022 MODEM: " + custModemText.Text);
//Statements to write checkboxes to stringbuilder
string checkBoxesLine = "\u2022 LIGHTS: ";
foreach (Control control in pnlCheckBoxes.Controls)
{
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
if (checkBox.Checked && checkBox.Tag is string)
{
string checkBoxId = (string)checkBox.Tag;
checkBoxesLine += string.Format("{0}, ", checkBoxId);
checkBoxes = checkBoxesLine;
}
}
}
//Newline for checkboxes
strBuilder.AppendLine(checkBoxesLine);
strBuilder.AppendLine();
//Continues textboxes to stringbuilder
strBuilder.AppendLine("\u2022 TROUBLESHOOTING: " + tShootText.Text);
strBuilder.AppendLine();
strBuilder.AppendLine("\u2022 SERVICES OFFERED: " + svcsOfferedText.Text);
strBuilder.AppendLine();
strBuilder.AppendLine("\u2022 OTHER NOTES: " + otherNotesText.Text);
notesViewText.Text = strBuilder.ToString();
Clipboard.SetText(notesViewText.Text);
//....
Essentially i want to add a feature if checkbox1 is checked on that button press it omits the custModemText.text along with the \u2022 MODEM: and also the same for the \u2022 LIGHTS: and the string checkBoxesLine.
I have searched for something to do this but i am honestly at a loss about HOW to go about it which is limiting my ability to search for solutions. I have a sinking feeling i am going to have to redo the way i am processing my data but am unsure so any help would be appreciated.
Also this is a Win Form
What you need to do it you first check that the checkbox is checked or not. If it is checked then modify your text.
private void save_button_Click(object sender, EventArgs e)
{
// Add mandatory lines to a string.
if (checkBox1.Checked)
{
// Append optional lines to the string.
}
else
{
// Do something.
}
}
You don't need to do a foreach loop. Just check that checkbox on which you want to put condition.
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;
}