Formatting Writeline output in VB 2010 - vb.net

Good day,
I have been searching for a way to format output using writeline/write (streamwriter)
using RTF tags and wondering if there is a syntax for this, if it exists. I have not
been able to find a resource which clearly explains how to "pretty" up output sent to a
file.
The reason why I am asking is because I want to "print" results from my program into
a file that, at the very least, would be centered, tabbed and even bolded where possible
without requiring the user to go and futz with it. I think I saw that Crystal Reports
won't work with VB 2010 Express and, quite frankly, just want to create a file with
output.
The problem I am having is how I can make output look less like writeline did it
(you know, writeliney-ish) and more like someone took the time to make writeline
look like it did something awesome.
Here's some code to show you what I'm trying to do:
sw.Write(" RATE QUOTATION ")
sw.WriteLine(" ")
sw.WriteLine(" ")
sw.WriteLine(mycompanyname)
sw.WriteLine(mycompanyaddress1 + ControlChars.Tab + "Phone:", companyphone)
sw.WriteLine(mycompanyaddress3 + ControlChars.Tab + "Fax:", companyfax)
sw.WriteLine(cityname, stateprovince, zippostal)
sw.WriteLine("Visit us online at:", websiteaddress)
sw.WriteLine(" ")
sw.WriteLine("Quoted by:", contactname + " " + "E-mail:", contactemail)
sw.Write("Date: ")
sw.WriteLine(DateTime.Now)
sw.WriteLine(" ")
sw.WriteLine(("Customer Name:" + " " + quotename.Text), "Company:" + " " + quotecompany.Text + " " + "Customer E-mail:" + " " + quoteemail.Text)
sw.WriteLine(("Phone:" + " " + quotephone.Text + ControlChars.Tab + "Fax:" + " " + quotefax.Text))
sw.WriteLine(" ")
sw.WriteLine("Shipment Details:")
Is there a way to control writeline in such a way that you can create an RTF via streamwriter and specify formatting tags, like centering and bold, etc? I'm trying to think of a way so that the RTF header information is created when streamwriter starts it's business and prints what I want out. I don't think there's an elegant way to create a PDF either so I'm kind of opting for old school output for now.
Thanks in advance!

A StreamWriter is designed simply for writing data to a stream. There is no reason that it should know what RTF is.
If you want to use StreamWriter, you will need to calculate the RTF yourself.
There may be various RTF libraries online that would be helpful. I suggest googling for some.

As promised, started playing with some code and thought I'd share it, although I'm still trying to figure out why vbNewLine doesn't work. :/
Originally, I was trying to use streamwriter to embed RTF codes, like (\rtf1) and (\b) etc..but these do not work within writeline statements that I tried, such as:
writeline("\b This is in bold \b0")
So by pretending to have a richtext box (rtb), you can create a file that will allow font formatting and other text formatting (albeit still trying to figure out a few things lol),
which may be an alternative to streamwriter if you care about how output appears in a file.
Public Class Form1
Private Property richtextbox1 As Object
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim something As String
Dim somethingelse As String
Dim butwaittheresstillmore As String
something = TextBox1.Text
somethingelse = TextBox2.Text
butwaittheresstillmore = TextBox3.Text
Save1.InitialDirectory = "c:\users\shawn\desktop"
Save1.FileName = " "
Save1.Filter = " RTF file | *.rtf"
Save1.ShowDialog()
Dim rtb As New RichTextBox
rtb.SelectionAlignment = HorizontalAlignment.Center
rtb.Text = something & vbNewLine
rtb.SelectionAlignment = HorizontalAlignment.Left
rtb.Text = somethingelse + (" another field: ") + butwaittheresstillmore
rtb.SelectAll()
rtb.SelectionFont = New Font(New FontFamily("Arial"), 12, FontStyle.Regular,
GraphicsUnit.Pixel)
rtb.SaveFile(Save1.FileName)
End Sub
End Class
I wanted to control the font and alignment and this seems to do the trick, although I'm trying to sort out using "tab" position and line feeds. I see that I can align left, center and right. Anyway, I hope this helps someone who might otherwise want a quick way to make a document where they can control the font.
Thanks!

Related

spell checking richtextbox in winforms project

I have a WinForms project that contains a RichTextBox (RTB) written with VB
I have set ShortcutsEnabled = FALSE in the RTB
To use any Spell Checker I am guessing this would need to set to TRUE
That is NOT my question! I have been reading for way more hours than I care to admit
With the understanding that Spell Checking is easy if you have a ASP.Net OR WPF project
Well I don't so here are the three candidates from NuGet NONE of these candidates offer much help
WeCantSpell.Hunspell and VPKSoft.SpellCheckUtility and NetSpell
I am not asking for a recommendation
Because I can not find a tutorial and am clueless on how to implement these Add In's with code
As well as NOT knowing if they are compatible with WinForms
I even looked at this CP post
CP LINK
Just a suggestion how to use one of these Add In's OR how to add spell checking to the RTB?
To achieve spell checking, you can try Nuget Package NHunspell.
First, you need to add "NHunspell" from "NuGet" and import it. The specific operation is as follows:
Right click the Reference and select "Manage NuGet Packages...", then type "NHunspell " in the search bar and install it:
Second step, you need to create a folder to store ".aff" and ".dic" like this.
Download the "zip" containing the corresponding file, you can access this site.
Here is a demo you can refer to.
Private Sub btCheck_Click(sender As Object, e As EventArgs) Handles btCheck.Click
Dim affFile As String = AppDomain.CurrentDomain.BaseDirectory & "../../Dictionaries/en_us.aff"
Dim dicFile As String = AppDomain.CurrentDomain.BaseDirectory & "../../Dictionaries/en_us.dic"
lbSuggestion.Items.Clear()
lbmorph.Items.Clear()
lbStem.Items.Clear()
Using hunspell As New Hunspell(affFile, dicFile)
Dim correct As Boolean = hunspell.Spell(TextBox1.Text)
checklabel.Text = TextBox1.Text + " is spelled " & (If(correct, "correct", "not correct"))
Dim suggestions As List(Of String) = hunspell.Suggest(TextBox1.Text)
countlabel.Text = "There are " & suggestions.Count.ToString() & " suggestions"
For Each suggestion As String In suggestions
lbSuggestion.Items.Add("Suggestion is: " & suggestion)
Next
Dim morphs As List(Of String) = hunspell.Analyze(TextBox1.Text)
For Each morph As String In morphs
lbmorph.Items.Add("Morph is: " & morph)
Next
Dim stems As List(Of String) = hunspell.Stem(TextBox1.Text)
For Each stem As String In stems
lbStem.Items.Add("Word Stem is: " & stem)
Next
End Using
End Sub
The result,
Hope this can help you.

Write a program to create a file that consists of only 2 fields from another text file with 6? (VB2012)

I'm using
Dim sw As IO.StreamWriter = IO.File.CreateText("Justices1.txt")
to create the file but I'm having trouble writing the code that will take only specific parts of the original "Justices.txt" file and put it into "Justices1.txt"
The first line of the original Justices file looks like this:
Henry,Baldwin,Andrew Jackson,PA,1830,1844
And I'm trying to get it to this in the file I'm creating (Justices1):
Henry Baldwin,PA
Sorry if this is a stupid question- I'm new to this.
I'd normally suggest using a StringBuilder but I figure your app won't be burning too many CPU cycles with something simple like this:
Private Sub FormatJusticesFile(filePath As String, formattedFilePath As String)
IO.File.WriteAllLines(formattedFilePath,
From line In IO.File.ReadAllLines(filePath)
Let terms = line.Split(","c)
Select "(" & IO.Path.GetFileNameWithoutExtension(filePath) & "): " & terms(0) & " " & terms(1) & "," & terms(3))
End Sub
Usage:
FormatJusticesFile("c:\Justices1.txt", "c:\Justices2.txt")

How can I remove the initial return when the multiple texts are added to the field?

I am asking the user to select a txt file from a specified folder on a server [This is in PowerPoint 2007], but I need to give them the option of selecting more than one, so I have a bit of conditional code to determine this.
One file selected uses this code:
oShape.TextFrame.TextRange.Text = Text
More than one file selected currently uses this:
oShape.TextFrame.TextRange.Text = oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf & Text
…but this results in an extra return space above it all in the field, which is a bit untidy.
Could anyone advise me on how I can modify this to only get the returns in between the two texts, but not at the beginning?
I am not entirely sure I got the problem, but I believe this will do:
oShape.TextFrame.TextRange.Text = iif(oShape.TextFrame.TextRange.Text <> "", oShape.TextFrame.TextRange.Text & vbCrLf & vbCrLf, "") & Text

Changing one Words color in label.text

Windows forms .net 4.0 vb application. This is a small trivial thing but I was trying to just change the color of one word in label.text. But its not happening and I have a strong feeling that to make it happen is going to be more extensive than its worth... A snippet of what I am trying to use is below... Am I just missing a key detail or is this honestly more of a burden than its worth..
Dim _changeLabel1 As String = " Note Fields Marked in "
Dim _changeLabel2 As String = " are Required"
Dim _attrib As New Label
With _attrib
.ForeColor = Color.Red
.Text = "RED"
End With
_notificationLabel1.Text = _changeLabel1 + " " + _attrib.Text + " " + _changeLabel2
Winform labels don't support this functionality, unfortunately.
You could use multiple labels instead, with one that's red for your RED text.
Here's a related question on the matter.

VB big block of text?

Is there a way of putting a block of text (ie a paragraph) without using the label tool?
Thanks
Dim myString As String = "Hello world " + ControlChars.NewLine + _
" trying the next line of code " + ControlChars.NewLine + _
" trying the third line of code..."
Label1.Text = myString
Fixed, this works for me.
Regardless of winform or webform, you can store your very large text in a file and use System.IO.File.File.ReadAllText Method to read that block of text and assign the read text to relevant control.