Fill word Content Control using C# issue - c#

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

Related

How to remove a specified characters in labels or controls?

I want to change texts every time that user click on a button. So, if the user clicked on specified button the "✓ " characters will be added to that button's text. also, if the user clicked on another button, That characters will be removed from the specified button's text.
I tried the first code below to add the "✓ " characters and it worked well without problems. Then I tried to remove that characters by replacing + into -
and I got problems. I need now a code to remove that characters from main text and thanks for helping me.
English_Language.Text = "✓ " + English_Language.Text;
//-----------
English_Language.Text = English_Language.Text - "✓ ";
In order to remove a character from a string you cannot just subtract. That is undefined, if there is more than one, what will happen? So you must use one of the methods on the string class in to get the result you want. If you only have one of those characters, or you want them all gone then you can use Replace, otherwise, you can use SubString as follows.
English_Language.Text = English_Language.Text.SubString(1);
Using SubString in this way will skip the first character seeing as that's where you are placing the character.

Response.Write print text from database as HTML

I'm storing text from a textarea to my database. This text can contain new lines, special characters etc. However I want to make sure that when I show it using response.write, it comes out exactly as if it was to show in the textarea control. The reason why I want to do this is because I want to give the user a print view form. The solution below does not work. Using ASP.NET/C#
e.g
foreach (DataRow row in dview.Table.Rows)
{
Response.Write("<tr>");
Response.Write(String.Format("<td>{0}<br/><br/></td>", row[1].ToString().Replace("\n", "<br/>")));
Response.Write("</tr>");
Thanks
Never use "\n" always use Environment.NewLine, instead:
foreach (DataRow row in dview.Table.Rows)
{
Response.Write("<tr>");
Response.Write(String.Format("<td>{0}<br/><br/></td>", row[1].ToString().Replace(Environment.NewLine, "<br/>")));
Response.Write("</tr>");
}
Don't do it that way, you'll run into problems, especially when quotes and symbols are involved.
Store the data like this :
HttpUtility.HtmlEncode("<p>Lorem ipsum ...</p>"); //your HTML to encode goes here
And retrieve it like this:
myPlaceHolder.Text = HttpUtility.HTMLDecode(myData);
Here's a little more information on HTMLEncode/Decode
You could display the output inside a <pre></pre> block which will preserve all whitespace, including multiple spaces, newlines, etc.
HTML encoding is recommended so any angle brackets in the data don't break the output HTML.

Is there a way to preserve formatting when outputing to a label?

I have some user input that I am outputting to a label.
I have used HTML.Encode in order to show the input in the way the user entered it (ignoring as a html tag).
However, I have noticed that the user input like New Line are not using in the label. It's simply displayed as white space.
I've done this
msg.Text = msg.Text.Replace(Environment.NewLine, "<br />");
which seems to now be displaying the right input.
Is that the best way to do this? Or is there like a common method that can convert newLines, tabs, etc. all of the invisible formatting things into HTML tags?
I don't know of any other way. What I usually do (in case you have a single "\n" or a "\r\n" combo) is replace all "\r\n" first, then any single "\n" last.
lbl.Text = lbl.Text.Replace("\r\n", "<br />").Replace("\n", "<br />");
For tabs you can use 4 non-breaking spaces.:
lbl.Text = lbl.Text.Replace("\t", " ")
To preserve any spacing (as html will aggregate multiple continuous spaces into a single space) use:
lbl.Text = lbl.Text.Replace(" ", " ")//Replace every 2-space pair.
Remember to Encode your text first before adding in markup like <br /> that you intentionally want to render.
You could also use a TextBox, set it's MultiLine property to "true" and Enabled to "false" if you want to display the information with the original carriage returns without resorting to inserting markup. I think the label is the best choice though.
If possible, just use a Multiline Textbox instead of label.

Insert carriage returns in Excel via OleDb c#

I am inserting text into a Memo field in a Excel cell via a insert statement using OleDb command object.
When I try to insert a carriage return, char 10, what is displayed is a black square (MS Sans Serif). When I look at the top edit cell (don't know the offical name) the text is formatted correctly including carriage returns.
I am trying to duplicate what happens when a user presses Alt+Enter in a cell.
I have tried \n, \r, \r\n and char.ConvertFromUtf32(10).
Nothing seems to change the text of the cell.
I'm assuming you meant \n and \r\n?
Also have you tried Environment.Newline?
Got it to work as well via:
Insert the newline character \n inside the string
Set the column to autofit. The EntireColumn property can also be used to set the width of the column or to force the column to Autofit.
public void AutoFitColumn(Worksheet ws, int col) {
((Range)ws.Cells[1, col]).EntireColumn.AutoFit();
}
See this link if more info desired.
Try expanding the height of the row in excel. I was beating my head against the wall over this as well, and finally got it to work using \n. But then one day it stopped working for no reason other than the cell was too smal to display all lines of data.
I had the same same blank squares hou had.
I managed to make it work by inserting a \n character and by activating the option "Wrap text" in the format of the Excel cell.

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