Rich Text Box *Formatting - text

Language: C#
My Question...
Q. Is there a SIMPLE way to change the format of strings in the rich text box, similar the way you handle it with HTML.
For example:
richTextBox1.text = "[i]Hello[/i] [Bold]world![/Bold]";
output: Hello World!
I am also wondering if there is colour?
With kind regards and thank in advance for your feedback.

Rich Text Format is a language in and of itself. However, you don't need to learn it to use it.
However, there are other ways this can be accomplished that might be more practical, like so:
RichTextBox1.Text = "Hello World"
' Select Hello and Bold It
RichTextBox1.Find("Hello")
Dim fntBold As New Font(RichTextBox1.Font, FontStyle.Bold)
RichTextBox1.SelectionFont = fntBold
' Select World and Set to Italics
RichTextBox1.Find("World")
Dim fntItalic As New Font(RichTextBox1.Font, FontStyle.Italic)
RichTextBox1.SelectionFont = fntItalic
Afterwards, if you are curious, you can look at RichTextBox.Rtf property (it's a string) to see what it looks like. This is what I saw after I did it:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\lang1033\b\f0\fs17 Hello\b0 \i World\i0\par}
In fact, if you then take that text, open Notepad, copy the Rtf property's text, save as an RTF file. Close down Notepad and open the RTF in Word, you'll see this same same text with the same look and feel.

Hi and thank's so much #Ctznkane525, thanks for putting me on the right track, I did convert it to C# and this is the outcome:
using System.Drawing.Text;
rtb_Main.Text = "Hello World";
rtb_Main.Find("Hello");
Font fntBold = new Font(FontFamily.GenericSansSerif, 14.0F, FontStyle.Bold);
rtb_Main.SelectionFont = fntBold;
rtb_Main.Find("World");
Font fntItalic = new Font(FontFamily.GenericSansSerif, 14.0F, FontStyle.Italic);
rtb_Main.SelectionFont = fntItalic;

Related

Bolding multiple specific words based on a read in value in VBA

I am trying to bold specific words based on a string being read into vba code.
I have the string in read into vba and now want to pick out a word or words to bold them.
If the following was read into vba code: The boy ran down the street.
In the above sentence, what code would I use if I wanted to bold just the word boy? What would I use if I wanted to bold the words boy and the?
I have tried the following code but do not know how to modify it to my specific case.
https://code.adonline.id.au/vba-format-text-microsoft-word/
In short, I want code that will read in a string, search that string for x amount of words, and bold said words to be later exported.
Let me know if you need any more details and what I am trying to accomplish.
Excel is pretty good at formatting whole cells, but it's not very good at formatting parts of cells. One way that I've been able to do this is to turn the text into very basic HTML and then paste the text into a cell. Here's an example.
Public Sub BoldCertainWords()
Dim sSentence As String
Dim dobj As DataObject
Set dobj = New DataObject
sSentence = "The boy ran down the street"
sSentence = Replace$(sSentence, "boy", "<strong>boy</strong>")
sSentence = Replace$(sSentence, "the", "<strong>the</strong>")
dobj.SetText "<html>" & sSentence & "</html>"
dobj.PutInClipboard
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End Sub
You need to set a reference to the MS Forms 2.0 library to get the DataObject if you don't already have it.
Note also that case matters. This will bold the but not The. You could repeat for typical capitalization or get clever with how you find the words and build the HTML string.
Also note that this particular PastSpecial method is Worksheet method, not a Range method. You have to select the range first.

textbox1 + textbox2 in Textbox3

Ok so what I am trying to do is create a program that will do this:
Text Box 1: ASDFGHJK
Text Box 2: ZXCVBNMO
Result Text Box: AZSXDCFVGBHNJMKO
In a windows application using text boxes and buttons. Can somewhere give me a starting point on this? I don't even know how to research how to do this. My online teacher sucks so any help is appreciated. I am supposed to make this program in a struct and in a class. If you could help me figure out the difference, that would be awesome too! TIA
It's been 11 days since you posted this question so I'm hoping you did get more answers from your teacher. If not, I can't do your homework for you but I can give you some hints.
The process you described above is "string concatenation" (a String is a variable with characters in it)
You tagged your question with windows-applications and basic so I assume you're using Visual Studio to write a VB.NET Windows Forms Application
Here's an example of concatenation
' Letters ("string" variables)
Dim a = "abc"
Dim b = "def"
Dim c = a & b ' concatenation, c = "abcdef"
The other aspects you mentioned were struct and class. Structures and classes are two different ways to make complex variables, where a variable can have multiple properties. Do searches on these terms for more information (e.g. "vb.net strucure class"). For example, I could create a Structure that has properties corresponding to the three variables listed above like this:
Public Structure ExampleStructure
Public a As String
Public b As String
Public c As String
End Structure
Then, to use it in my concatenation example it would look like:
Dim s As New ExampleStructure
s.a = "abc"
s.b = "def"
s.c = s.a & s.b ' concatenation, s.c = "abcdef"
As far as getting started with a Windows Forms application with a textbox and a button, check out http://www.tutorialspoint.com/vb.net/vb.net_textbox.htm

export Crystal report to Excel (empty rows)

I am trying to export a report to excel. When I export my report to Excel I am getting blank rows between each detail section. I assume this is because I have a context menu in form of a normal text element which overlies the "normal" text elements.
Does anybody have any advice on how I can stop the blank rows occurring? Is it possible to suppress a text element only when it is exported to Excel?
Thanks!
Try to make it compact.
It should be no space between each object. You should set every object on the same row has the same height and every object on column has the same width.
When there's a space between objects, it will create a cell on excel.
There are 2 articles from Ken Hamady that might be helpfull:
http://kenhamady.com/cru/archives/231
http://www.kenhamady.com/news0506.shtml (scroll to the bottom of the page)
Another option , if you are working with tabular data is to use a report extension like it is shown in this video: https://www.youtube.com/watch?v=3hk6FJ1dvb4
This approach will use the Crystal report as a datasource and will export the data from a grid with much better formatting. The video is using a 3rd party tool, but it is free - http://www.r-tag.com/Pages/CommunityEdition.aspx
Use This Code:
Public Shared Sub ExportDataSetToExcel(ByVal ds As DataTable, ByVal filename As String)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.Buffer = True
response.Charset = ""
response.ContentType = "application/vnd.ms-excel"
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
Dim dg As New DataGrid()
dg.DataSource = ds
dg.DataBind()
dg.RenderControl(htw)
response.Charset = "UTF-8"
response.ContentEncoding = System.Text.Encoding.UTF8
response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble())
response.Output.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub
Finally I've solved this issue, after a long time researching. Make the fields inside the details section fill the whole height of the section... no spaces between fields and top and bottom edges.
instead of this

Adding content control throws an exception dynamically

I am fairly new to Word Addin development. Fortunately I was able to do almost everything but stuck at some simple issue I belive.
I want to insert plain text controls dynamically at the selected range. For this I am using the following:
currentDocument = application.ActiveDocument;
foreach(var field in myFieldsList)
{
Microsoft.Office.Interop.Word.Range rng = currentDocument.ActiveWindow.Selection.Range;
object oRng = rng;
var contentControlPlain = application.ActiveDocument.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText, ref oRng);
contentControlPlain.Tag = formField.FormFieldId.ToString();
contentControlPlain.SetPlaceholderText(null, null, " <" + formField.FormFieldName + "> ");
contentControlPlain.LockContentControl = (formField.TypeName.Trim() == "Blank");
}
Code seems to be working fine but when I try to insert the second field it complains saying:
This method or property is not available because the current selection partially covers a plain text content control.
I understand that addin is trying to insert next content control into the previously inserted plain text control. But I tried giving some other range and could not fix it.
Any help is greatly appreciated.
Thanks.
After adding every content control use
Application.Selection.Start = lastControl.Range.End+1

vba code for excel - to encoding gibberish to hebrew

i have files that open with excel.
when i open the file the text is like gibberish.
i need to encode - tools-internet option - general-encode - hebrew iso-visual
and then the file turn to hebrew
there is a vba code that do that ?
thanks,
omri
I don't really have a way to test this, so I am just taking a shot:
Excel.ActiveWorkbook.WebOptions.Encoding = msoEncodingHebrew
Use the following function from ADODB Stream, with the following code.
Page 1255 is the original Hebrew page.
And you need to reference the latest Microsoft ActiveX Data Objects Library.
(Tools/References)
Public Function CorrectHebrew(gibberish As String) As String
Dim inStream As ADODB.stream
Set inStream = New ADODB.stream
inStream.Open
inStream.Charset = "WIndows-1255"
inStream.WriteText gibberish
inStream.Position = 0 ' bring it back to start preparing for the ReadText
inStream.Charset = "UTF-8"
CorrectHebrew = inStream.ReadText ' return the corrected text
inStream.Close
End Function

Resources