Appending Date to the end of line in multiple textbox - c#

how could i add a date to the very end of each line with another font and size ?!
for example i want to add 1:15 AM to the end of "hello World" line.

If it doesn't have to be a textbox, you can apply some nice formatting using some manipulation of datagridview.
Using 2 columns with the formatting and colors changed to match the rest of the controls.
It's just as easy to add lines to.
dataTextView.Rows.Add(txtAddText.Text, DateTime.Now.ToShortTimeString());
Example source: http://mcspazzy.com/code/TextDisplay.zip

AppendText exists for WinForms TextBoxes
textBox1.AppendText(DateTime.Now.ToString("h:mm tt"));
However, changing font and size for a just a part of a TextBox is not possible,
You need a RichTextBox for that
StringBuilder sb=new StringBuilder();
foreach(string s in richTextBox1.Lines)
{
sb.AppendLine(s + " "+DateTime.Now.ToString("h:mm"));
}
richTextBox1.Text=sb.ToString();

Related

How to bold a part of string being passed into TextBox in c#

I have a string builder where value is appending at runtime via placeholder. Once string builder has appended, it is assigned to a text box (not rich txt) to show up in UI.
I want part of the text to be bold.
sb.AppendFormat("Added {0} by {1}:\n{2}", DateTime.ToString(), userName, note);
txt.Text = sb.ToString();
Expected output:
Added 9/01/2016 8:47:19 PM by Vinoth: Testing Purpose
How can I achieve this? Is there anyway of looping over words with the : symbol until I want it to be bold?
You will have to build up the text in sections.
Either as different TextBoxes/TextBlocks or as a single RichTextBox with separate Runs for the sections you want in a different style.

horizontal tab escape not working in string.Format [duplicate]

This should be very simple.
I have a Label control on my Form and I am trying to put a tab character between text
Label.Text = "Is there a\ttab";
The output is "Is there atab";
What am I doing wrong?
Tab is actually a non-printing character—or rather, a control character. What it does is entirely dependent on the application. What exactly do you expect? 8 spaces? 4 spaces? As many spaces as needed to get to a multiple of 8 columns? Indentation of the following text by one cm?
To put it short: The Label control doesn't support tabs. Actually, Label just uses normal graphics routines for rendering its text and how should they know what you intend to do with your tab character?
If you need to display that character as a number of spaces, then you should replace it by that number of spaces.
I wanted to add tabs ("\t") to a dropdown list of items. The items have a ToString method that gives about 3 words concatenated together. They did not line up. For example:
1-I 45
123-AB 511
123456-MMM 611
A long list like this is hard to read. So I used string.Format like this:
string.Format("{0,6}-{1,-4} {2}",id,name,num);
The number after the comma will right align/pad if positive and left align/pad if negative.
Then I changed my font in the Combobox to be monospaced, like Courier New, and you get something like this:
1-I 45
123-AB 511
123456-MMM 611
That is much easier for a user to read.
Nothing, windows forms labels are very limited in functionality and don't support the \t character.
A (slightly awkward) alternative might be:
label1.Text = "test\ting\t123".Replace("\t"," ");
Old thread, but since none of the answers seemed to work for me, I will go ahead and throw in my 2 cents. I could not get a "\t" or even use manual spaces to add spacing to the label. What I ended up doing was using alt code alt-255 5 times. This worked like a charm. Gotta love total hacks...
Right, to insert a tab, just add the spaces desired.
If you want to offset the next by a specified length, you could try
int offset_text = 20;
label1.Text = "Is there a".PadRight(offset_text)+"Tab";
label2.Text = "More Text".PadRight(offset_text)+"Too";
Just use a literal string and you should be good to go...
label1.Text = #"Test for Tab";
Where that big space is where I actually hit tab three times...hope this helps
I had the same problem. A textbox does instead a label accept Tabs. So if you change the label in a textbox, the pro
Just click in the arrow at the right of the Text property of the label (click in the Text property content and the drop-down-arrow will show up). A box for text-editing will open, and in that box you can use Enter, Tab, and so on.

Fill word Content Control using C# issue

I have word contains content control like
I want to write many lines on it so in C# I tried many ways :
using EnviromentNewLine .
\n
Example:
for (int i = 0; i < items.Count; i++)
{
items[i] = string.Format("{0}{1}{2}{3}", (i + 1).ToString(CultureInfo.InvariantCulture), ". ", items[i], "<br />");
}
but I still get the word content control on one line , any idea how to fix it
The actual problem is, that the plain text content control does not support multiple paragraphs, it is only possibly to insert manual line breaks (which you normally do by pressing Shift+Return). Within the plain text content control, Word automatically replaces Return by Shift+Return as you can see when switching on the formatting symbols.
You basically have two options to solve your issue:
Change the content control to a rich text content control. That way you will be able to insert arbitrary content, also new line characters (i.e. multiple actual Word paragraphs)
Insert a vertical tab, i.e. ASCII character 11 or \B instead of a new line. This will insert the manual line break required by the plain text content control.
Try
\r
MSDN 2.4.4.4 Character literals

Edit RichTextBox programmatically without losing formatting

I have formatted text in the rtf file and I load it to my richTextBox. It works fine, but then I want to remove some text parts programmatically. If I do like that:
richTextBox.LoadFile("TextFile.rtf");
richTextBox.Text = richTextBox1.Text.Substring(fromPosition, length);
text formatting disappears. I've tried to work with richTextBox.Lines, but there is no delete or edit function. All I need is to delete parts of loaded text without losing its formatting. Is it possible?
Thanks in advance.
To remove text you first select it, programmatically by setting SelectionStart and -Length. Then you use the Cut Method:
richTextBox1.SelectionStart = 20;
richTextBox1.SelectionLength = 120;
richTextBox1.Cut();
If you want to avoid putting the removed text to the clipboard you can set it to "" instead of 'cutting' it:
richTextBox1.SelectedText = ""

Winforms Label Text property not displaying \t tab character

This should be very simple.
I have a Label control on my Form and I am trying to put a tab character between text
Label.Text = "Is there a\ttab";
The output is "Is there atab";
What am I doing wrong?
Tab is actually a non-printing character—or rather, a control character. What it does is entirely dependent on the application. What exactly do you expect? 8 spaces? 4 spaces? As many spaces as needed to get to a multiple of 8 columns? Indentation of the following text by one cm?
To put it short: The Label control doesn't support tabs. Actually, Label just uses normal graphics routines for rendering its text and how should they know what you intend to do with your tab character?
If you need to display that character as a number of spaces, then you should replace it by that number of spaces.
I wanted to add tabs ("\t") to a dropdown list of items. The items have a ToString method that gives about 3 words concatenated together. They did not line up. For example:
1-I 45
123-AB 511
123456-MMM 611
A long list like this is hard to read. So I used string.Format like this:
string.Format("{0,6}-{1,-4} {2}",id,name,num);
The number after the comma will right align/pad if positive and left align/pad if negative.
Then I changed my font in the Combobox to be monospaced, like Courier New, and you get something like this:
1-I 45
123-AB 511
123456-MMM 611
That is much easier for a user to read.
Nothing, windows forms labels are very limited in functionality and don't support the \t character.
A (slightly awkward) alternative might be:
label1.Text = "test\ting\t123".Replace("\t"," ");
Old thread, but since none of the answers seemed to work for me, I will go ahead and throw in my 2 cents. I could not get a "\t" or even use manual spaces to add spacing to the label. What I ended up doing was using alt code alt-255 5 times. This worked like a charm. Gotta love total hacks...
Right, to insert a tab, just add the spaces desired.
If you want to offset the next by a specified length, you could try
int offset_text = 20;
label1.Text = "Is there a".PadRight(offset_text)+"Tab";
label2.Text = "More Text".PadRight(offset_text)+"Too";
Just use a literal string and you should be good to go...
label1.Text = #"Test for Tab";
Where that big space is where I actually hit tab three times...hope this helps
I had the same problem. A textbox does instead a label accept Tabs. So if you change the label in a textbox, the pro
Just click in the arrow at the right of the Text property of the label (click in the Text property content and the drop-down-arrow will show up). A box for text-editing will open, and in that box you can use Enter, Tab, and so on.

Categories

Resources