I am trying to input a predefined string into a RichTextBox during the process of a method, e.g.:
"various words" = RichTextBox1,ToString;
How can this be done?
You need to modify the Text property.
Example:
RichTextBox1.Text = "various words";
If you need the text to be richly formatted, you need the Rtf property instead.
Example:
RichTextBox1.Rtf = #"{\rtf1\ansi Hello in \b bold-face \b0 .}";
Related
I have searched up how to do this. All I got was removing text based on a index point. I would like to have it setup where it uses a string to remove text, instead of choosing the point.
Something along the lines of
string text1 = "Hello World!";
string text2 = "Hello";
string text3;
void RemoveText()
{
text3 = text1.Remove(text2);
Console.WriteLine(text3);
}
Output:
Console: World!
Is there a method that can remove text by using a string? I was looking at the Microsoft Docs, but the problem is that the string wouldn't have a denfinite spot that need to be removed.
text3 = text1.Replace(text2,string.empty);
If you wanted to get part of a user inputed responce, you would doing something along the lines of
string textToRemove = "set background ";
Console.WriteLine("Type 'set background ' then your color.");
string userInput = Console.ReadLine();
string answer = userInput.Replace(textToRemove, string.empty);
SetBackgroundColor(answer);
First what you want to do is set the text you don't care about/need
string textToRemove = "set background ";
Next you are going to tell the user what to input and get the input
Console.WriteLine("Type 'set background ' then your color.");
string userInput = Console.ReadLine();
We would then have to make a new string without the unnessary text
string answer = userInput.Replace(textToRemove, string.empty);
In this case we are using the input to set the background color. So we would write a function for changing the background color with a string argument for the color the user gave.
SetBackgroundColor(answer);
You could also use an If statment or an for/foreach statment to see if the user's text without the unneccasry text is in an array or list containing strings that are allowed to be used.
I need to set the text of a TextBlock code behind to a string containing formatted text.
For example this string:
"This is a <Bold>message</Bold> with bold formatted text"
If I put this text in xaml file in this way it work correctly
<TextBlock>
This is a <Bold>message</Bold> with bold formatted text
</TextBlock>
But if I set it using the Text property don't work.
string myString = "This is a <Bold>message</Bold> with bold formatted text";
myTextBlock.Text = myString;
I know I can use Inlines:
myTextBlock.Inlines.Add("This is a");
myTextBlock.Inlines.Add(new Run("message") { FontWeight = FontWeights.Bold });
myTextBlock.Inlines.Add("with bold formatted text");
But the problem is that I get the string as it is from another source and I have no idea how I can pass this string to the TextBlock and see if formatted.
I hope there is a way to set the content of the TextBlock directly with the formatted string, because I have no idea of how I can parse the string to use it with Inlines.
You may parse a TextBlock from your string and return a collection of its Inlines:
private IEnumerable<Inline> ParseInlines(string text)
{
var textBlock = (TextBlock)XamlReader.Parse(
"<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ text
+ "</TextBlock>");
return textBlock.Inlines.ToList(); // must be enumerated
}
Then add the collection to your TextBlock:
textBlock.Inlines.AddRange(
ParseInlines("This is a <Bold>message</Bold> with bold formatted text"));
TextBlock wouldn't support that directly, you'd have write a method to parse the string yourself and set the styles on the inlines. Doesn't look that difficult to parse. Use regular expressions or a token parser. Depends on how many different styles you need to support, but Regex is the easier approach.
I have a label that I want to only copy the first character. The label reads 1000. I have my copy button set up like this
var textCopy = label.Text;
Clipboard.SetText(textCopy);
Can I add something to the label.Text part that only allows the first character to be copied?
string is an array of characters, so just get the first index of the string like this:
Clipboard.SetText(textCopy[0]);
Use Substring:
var textCopy = label.Text;
Clipboard.SetText(textCopy.Substring(0,1));
msdn
In my ASP.NET page, I have a string (returned from SQL db). I would like to bold certain part of the string text based on given text position.
For example, if I have a string as follows:
"This is an example to show where to bold the text"
And I am given character start position: 6 and end position: 7, then I would bold the word "is" in my string, getting:
"This is an example to show where to bold the text"
Any ideas?
EDIT: Keep in mind I need to use the start/end position as there may be duplicate words in the string.
Insert a close tag into position 7 of the string
Insert an open tag into position 5 (6 - 1) of the string.
You will get a string like "This is an example…"
I.e. modify string (insert markup) from end to start:
var result = str.Insert(7, "</b>").Insert(6 - 1, "<b>");
You can use String.Replace method for this.
Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string.
string s = "This is an example to show where to bold the text".Replace(" is ", " <b>is</b> ");
Console.WriteLine(s);
Here is a DEMO.
Since you clear what you want, you can use StringBuilder class.
string s = "This is an example to show where to bold the text";
var sb = new StringBuilder(s);
sb.Remove(5, 2);
sb.Insert(5, "<b>is</b>");
Console.WriteLine(s);
Here is a DEMO.
NOTE: Since you didn't see <b> tags as an output, it doesn't mean they are not there ;)
You need to do something like this...
**
strStart = MID(str, 0 , 7) ' Where 7 is the START position
str2Replace = "<b>" & MID(str, 8, 10) & "</b>" ' GRAB the part of string you want to replace
str_remain = MId(str, 11, Len(str)) ' Remaining string
Response.write(strStart & str2Replace & str_remain )
**
First find the string to replace in your full string.
Replace the string with <b>+replacestring+</b>
string str="This is an example to show where to bold the text";
string replaceString="string to replace"
str=str.Replace(replaceString,<b>+replaceString+</b>);
Edit 1
string replaceString=str.Substring(6,2);
str=str.Replace(replaceString,<b>+replaceString+</b>);
SubString Example:
http://www.dotnetperls.com/substring
Edit 2
int startPosition=6;
int lastPosition=7;
int lastIndex=lastPosition-startPosition+1;
string str="This is an example to show where to bold the text";
string replaceString=str.Substring(startPosition,lastIndex);
str=str.Replace(replaceString,<b>+replaceString+</b>);
I have a string with special characters insert in different places. For example:
string myString = "This is a textbox: ##";
I would like to replace the ## with a control (namely, a textbox).
The Replace method only allows the string to be replaced with another string or character (understandably). But what would be the best way to dynamically replace the ## with a control in its position?
I was thinking maybe I could replace it with HTML markup which would be executed, but not quite sure how that would be achieved.
Thanks
EDIT: To clarify some details. The strings are being retrieved from a database, so I can't use the PlaceHolder control. The user selects a string from a drop-down list. The value of the item is the string with special characters. When the postback occurs from the item selection, I would like to display the string on the site, but replace the special characters with a fully working control (in this case, a textbox)
Consider leveraging the TextBox's Render() method. That'll get you the HTML that would be output from that TextBox.
You can then use that string to be the replacement text to replace the ## portion of your string.
TextBox Render() on MSDN
var myTxtBox = new TextBox();
myTxtBox.Text = "Hello World";
//implement the Render code in here
string myRenderedTextBoxHTML = RenderIt(myTxtBox);
string myString = "This is a textbox: " + myRenderedTextBoxHTML;
I'm unsure ViewState would be available for this control or not.
Something like this:
Panel panel = new Panel();
string myString = "This is a textbox: ##";
// some parsing logic
string[] arr = { "This is a textBox", "##" };
foreach(var item in arr)
{
if (item == "##"){
TextBox tb = new TextBox();
panel.Controls.Add(tb);
}
else{
Label l = new Label();
l.Text = item;
panel.Controls.Add(l);
}
}
your_plaaceholder.Controls.Add(panel);
myString = string.Replace("##", "<input type='text' />");
Note that this is not a control: it will just be an html element that won't be wired up on the server side later. And depending on what you do with the string maybe not even that much, as some controls (like label) will automatically escape your < and > characters.
If you really want a fully-working asp.net control there we need to know more about how you are adding that string to the page.
You could indeed replace it with markup:
string mystring = "This is a textbox: ##".Replace("##", "<input type='text'/>");
Response.Write(mystring);
I'm not sure why you would want to do this, though. Why not use a PlaceHolder control and just stick a TextBox in it in the code behind?
What Sash said, BUT make sure you put that in the Page.Init() every time if you wish to take advantage of viewstate.