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.
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 using a trackbar to adjust the value accordingly and the output is shown in a text box. I want to show the value with some text after it. The code I have used shows the text I want displayed however also shows this; i.e. slider moved to 200.
i.e. windowsforms.textbox.text: 200 °C
private void trackBar_Temp_Scroll(object sender, EventArgs e)
{
Oven_Temp.Text = trackBar_Temp.Value.ToString();
Oven_Temp.AppendText(Oven_Temp + "°C");
}
How comes the code shows the windows form section in the output with this code?
Your call to append the text should just be: Oven_Temp.AppendText("°C");
Better yet, use string concatenation to just do it all in one line:
Oven_Temp.Text = trackBar_Temp.Value.ToString() + "°C";
The problem is that you are including Oven_Temp inside your call to AppendText, which is the textbox object itself. So the type name of the textbox then shows up in your output.
EDIT - Sorry folks, i guess i wanted to "obscure" my work code too much... i don't know why it got so many downvotes but anyway. see below for update/edit with actual code.
I am trying to insert a piece of text into an existing section of a line (<data) which resides at the beginning of a line in my RichTextBox control. However, whenever i do that in the following manner:
private void AddSelectedIntellisense(object sender, EventArgs e)
{
ToolStripItem x = sender as ToolStripItem;
int cursorpos = this.txt_Body.SelectionStart;
string final = this.txt_Body.Text.Insert(cursorpos, x.Text);
//final var at breakpoint is equal to "<data log=\"Original\""
//then i assign it/that to the RTB.Text
this.txt_Body.Text = final;
//when checked with breakpoint, this.txt_Body.Text is equal to
//"log=\"\"<data log=\"Original\""
this.txt_Body.SelectionStart = cursorpos + x.Text.Length;
}
I am thinking that it is the < character that is causing issues when i assign the string to the .Text property (because if i replace the < with a [ in my logic, no problems), but i don't know how to fix it... if you could help me i would really appreciate it.
I also checked all of the indexes manually and they all lign up perfectly... so i don't know why the RTB.Text value is different than the string but if someone knows please tell me.
Cheers!
You are first setting:
txt = this.RTB1.Text.Substring(starts, length);
Then on the next line you are replacing the value of txt:
txt = this.RTB1.Text.Insert(index,"log='test'></data>");
You are probably looking to concatenate the strings:
string txt = this.RTB1.Text.Substring(starts, length);
txt += this.RTB1.Text.Insert(index,"log='test'></data>");
this.RTB1.Text = txt;
Ok folks... i suppose i'll give it to Aaron, since it's like somewhat related and nobody else answered.
The answer was:
I am using the RTB.On_TextChanged event to fire off the intellisense based on a condition. However, because i am also setting text the RTB.Text value within the Intellisense, the condition became true twice and added the specific text twice. So i setup a flag when i add intellisense text and check it in the on_textchanged event.
Cheers and sorry for the confusion.
I know that richtextboxes can detect links (like http://www.yahoo.com) but is there a way for me to add links to it that looks like text but its a link? Like where you can choose the label of the link? For example instead of it appearing as http://www.yahoo.com it appears as Click here to go to yahoo
edit: forgot, im using windows forms
edit: is there something thats better to use (as in easier to format)?
Of course it is possible by invoking some WIN32 functionality into your control, but if you are looking for some standard ways, check this post out:
Create hyperlink in TextBox control
There are some discussions about different ways of integration.
greetings
Update 1:
The best thing is to follow this method:
http://msdn.microsoft.com/en-us/library/f591a55w.aspx
because the RichText box controls provides some functionality to "DetectUrls". Then you can handle the clicked links very easy:
this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked);
and you can simple create your own RichTextBox contorl by extending the base class - there you can override the methods you need, for example the DetectUrls.
Here you can find an example of adding a link in rich Textbox by linkLabel:
LinkLabel link = new LinkLabel();
link.Text = "something";
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.richTextBox1.GetPositionFromCharIndex(this.richTextBox1.TextLength);
this.richTextBox1.Controls.Add(link);
this.richTextBox1.AppendText(link.Text + " ");
this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
And here is the handler:
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}
I found a way which may not be the most elegant, but it's just a few lines of code and does the job. Namely, the idea is to simulate hyperlink appearance by means of font changes, and simulate hyperlink behavior by detecting what the mouse pointer is on.
The code:
public partial class Form1 : Form
{
private Cursor defaultRichTextBoxCursor = Cursors.Default;
private const string HOT_TEXT = "click here";
private bool mouseOnHotText = false;
// ... Lines skipped (constructor, etc.)
private void Form1_Load(object sender, EventArgs e)
{
// save the right cursor for later
this.defaultRichTextBoxCursor = richTextBox1.Cursor;
// Output some sample text, some of which contains
// the trigger string (HOT_TEXT)
richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Underline);
richTextBox1.SelectionColor = Color.Blue;
// output "click here" with blue underlined font
richTextBox1.SelectedText = HOT_TEXT + "\n";
richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Regular);
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectedText = "Some regular text";
}
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
int mousePointerCharIndex = richTextBox1.GetCharIndexFromPosition(e.Location);
int mousePointerLine = richTextBox1.GetLineFromCharIndex(mousePointerCharIndex);
int firstCharIndexInMousePointerLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine);
int firstCharIndexInNextLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine + 1);
if (firstCharIndexInNextLine < 0)
{
firstCharIndexInNextLine = richTextBox1.Text.Length;
}
// See where the hyperlink starts, as long as it's on the same line
// over which the mouse is
int hotTextStartIndex = richTextBox1.Find(
HOT_TEXT, firstCharIndexInMousePointerLine, firstCharIndexInNextLine, RichTextBoxFinds.NoHighlight);
if (hotTextStartIndex >= 0 &&
mousePointerCharIndex >= hotTextStartIndex && mousePointerCharIndex < hotTextStartIndex + HOT_TEXT.Length)
{
// Simulate hyperlink behavior
richTextBox1.Cursor = Cursors.Hand;
mouseOnHotText = true;
}
else
{
richTextBox1.Cursor = defaultRichTextBoxCursor;
mouseOnHotText = false;
}
toolStripStatusLabel1.Text = mousePointerCharIndex.ToString();
}
private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && mouseOnHotText)
{
// Insert your own URL here, to navigate to when "hot text" is clicked
Process.Start("http://www.google.com");
}
}
}
To improve on the code, one could create an elegant way to map multiple "hot text" strings to their own linked URLs (a Dictionary<K, V> maybe). An additional improvement would be to subclass RichTextBox to encapsulate the functionality that's in the code above.
Many moons later there is a solution for .NET (Core), as of at least .NET 6.0 (possibly earlier) - for .NET Framework (whose latest and last version is 4.8) you'll still need one of the other solutions here:
The .Rtf property now recognizes RTF-format hyperlinks; e.g., the following renders as:
This is a true RTF hyperlink: Example Link
this.richTextBox1.Rtf = #"{\rtf1 This is a true RTF hyperlink:\line {\field{\*\fldinst HYPERLINK ""https://example.org""}{\fldrslt Example Link}}} }";
The standard RichTextBox control (assuming you are using Windows Forms) exposes a rather limited set of features, so unfortunately you will need to do some Win32 interop to achieve that (along the lines of SendMessage(), CFM_LINK, EM_SETCHARFORMAT etc.).
You can find more information on how to do that in this answer here on SO.