how to replace a selected text in a TextBox with a string? - c#

I want to replace the selected text in a TextBox with another string in c# . I am using the following code but it replaces all the same texts in all the TextBox (not only my selected text). How may I solve this?
string selectedTxt = TextBox1.SelectedText;
TextBox1.Text = TextBox1.Text.Replace(selectedTxt, "<b>" + selectedTxt + "</b>");

No need to replace, just set the SelectedText property with new text. Explanation here
string selectedTxt = TextBox1.SelectedText;
TextBox1.SelectedText = "<b>" + selectedTxt + "</b>";

Related

Outputting text boxes to text file doesn't include CR

I've got a problem whereby I've created an application where the user enters text into various text boxes. They then click a button which outputs it all into a text log file in a specific format.
I create a string which I output to a file and that string is compiled from various pieces of text and the contents of various form elements.
When it outputs, each separate line which has been created as part of the string creation outputs with CR LF (\r\n) which is how I want it, but any text which was entered into a Rich Text Box outputs with only LF (\n)
Code goes like:
string[] lines = {
#"HEADER TEXT HERE",
#"-----------------------------------------------",
Text_Box.Text,
Rich_Text_Box.Text,
........
Directory.CreateDirectory(#"\\basedirectory\" + project_name_tb.Text + #"\" + strDate);
System.IO.File.WriteAllLines(#"\\basedirectory\" + project_name_tb.Text + #"\" + strDate
+ #"\" + strDate + #"_" + project_name_tb.Text + #"_"
+ session_number_mtb.Text + #".txt", lines);
The rich text boxes are multiline.
How do I make the rich text box output CR LF?
You could try replacing all instances of LF with CRLF:
Rich_Text_Box.Text.Replace("\n", "\r\n")
I think this will solve your problem even it's safe if it's already \r\n it won't replace it.
public string ReplaceRichTextBoxContent(string data)
{
return Regex.Replace(data, "(?<!\r)\n", "\r\n");
}
use it like
string[] lines = {
#"HEADER TEXT HERE",
#"-----------------------------------------------",
Text_Box.Text,
ReplaceRichTextBoxContent(Rich_Text_Box.Text),

cdec function doesn't work if value type is html in rdlc report c#

The following value expression for placeholder works in html type input in the table header
= Sum(CDec(Fields!TotalDueAmount.Value))
But when I change it to concatenate a string it doesn't work. I tried below both of them shows #Error
= "Total: " + Sum(CDec(Fields!TotalDueAmount.Value))
= "<b>" + First(Fields!TotalLabel.Value, "ModelData") + ": " + "</b>" + Sum(Fields!TotalDueAmount.Value, "ModelData")
You need to convert the sum back to a string to concatenate it with other strings:
= "Total: " + CStr(Sum(CDec(Fields!TotalDueAmount.Value)))
An even better option is to use a placeholder. Type "Total: " in the textbox and then right-click in the textbox after it. Select the option to create a placeholder. Set the Value to Sum(CDec(Fields!TotalDueAmount.Value)). Now you can adjust the number formatting on the placeholder independently.

Creating a newline in rich text box

I need help on creating a new line for my RichTextBox which I cant make work when using CheckBox.
It keeps overlapping instead of creating a newline of words.
Tried using the method of rtbdisplay.text = (display+envrionment.newline);
example from my code:
if (rbtnSmall.Checked == true)
{
rtbDisplay.Text = "displaytext".PadRight(20) + "size".PadRight(23) +
qty.ToString().PadRight(20) + StrongDummy;
}
Use the RichTextBox.Text property or the RichtTextBox.AppendText method to append a string with a newline.
myRichTextBox.Text += Environment.NewLine + "My new line.";
// Or
myRichTextBox.AppendText( Environment.NewLine + "My new line." );
You can use c# Environment.NewLine Property as described in http://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx. Or, the "old style" like #"\r\n".
Rgds,

bold part of text string code behind

We built an application in XAML. Now I need to format the text strings.
Is it possible to bold only part of the text string. We are replacing an element's text in a XAML textblock with a text string. What would be the easiest way to make parts of the text string bold? Would I add a label? Something else in the XAML or in the C#?
Here is an example of our XAML and our code behind:
XAML
<TextBlock x:Name="PrimaryNameText" Text="Primary Member Name:"></TextBlock>
C# String
PrimaryNameText.Text = "Primary Member Name: " + reAccount.MyPerson.Prefix + " " + reAccount.MyPerson.FirstName + " " + reAccount.MyPerson.LastName;
In the example above, we more or less want to bold the part of the string "Primary Member Name: "
I know you can do a "\n" for a page break, is there a way we can do something for bolding text in the string?
Use a span inside your TextBlock.
<TextBlock>
<Span x:Name="PrimaryNameBold" FontWeight="Bold"></Span>
<Span x:Name="PrimaryNameNormal"></Span>
</TextBlock>
And in your code:
PrimaryNameBold.Text = "Primary Member Name: ";
PrimaryNameNormal.Text = reAccount.MyPerson.Prefix + " " + reAccount.MyPerson.FirstName + " " + reAccount.MyPerson.LastName;
Not an awesome solution, but this should work.

Insert value into a string at a certain position?

i'm looking to place a value from a text box lets say "12" to a certain place in a string temp variable. Then I want to place another value after that say "10" but with a : in between like a time. Both come from Text boxes and are validated so they can only be numbers.
If you just want to insert a value at a certain position in a string, you can use the String.Insert method:
public string Insert(int startIndex, string value)
Example:
"abc".Insert(2, "XYZ") == "abXYZc"
You can't modify strings; they're immutable. You can do this instead:
txtBox.Text = txtBox.Text.Substring(0, i) + "TEXT" + txtBox.Text.Substring(i);
If you have a string and you know the index you want to put the two variables in the string you can use:
string temp = temp.Substring(0,index) + textbox1.Text + ":" + textbox2.Text +temp.Substring(index);
But if it is a simple line you can use it this way:
string temp = string.Format("your text goes here {0} rest of the text goes here : {1} , textBox1.Text , textBox2.Text ) ;"
var sb = new StringBuilder();
sb.Append(beforeText);
sb.Insert(2, insertText);
afterText = sb.ToString();

Categories

Resources