Page 1 of 1

Some Vb Questions

Posted: Sat Apr 26, 2008 10:51 am
by Andrew_b
Im just coding a random app that acts as a crappy MS Word.

Heres my questions:

For my open file code i use the following. But it exceptions if you press Cancel. Not sure how to catch exception. I have been searching for a while.

Code: Select all

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim File_Dialog As OpenFileDialog = New OpenFileDialog()
        Dim File_Name As String
        Dim File_Contents As String
        File_Dialog.DefaultExt = "text"
        File_Dialog.Title = "Open A Text File."
        File_Dialog.ShowDialog()
        File_Name = File_Dialog.FileName
        File_Contents = My.Computer.FileSystem.ReadAllText(File_Name)
        RichTextBox1.Text = File_Contents
    End Sub
Bolding Selected Text in Rich Text Boxes. How do i do this? I tried the following but got an error.

Code: Select all

RichTextBox1.Font.Bold = True

Property 'Bold' is 'ReadOnly'.
Also I am stumped on saving. Well i think i would just make it stream writer but im not sure and also im not sure how to code Save As.

Any help would be nice :wink:

Thanks
-Andrew_b

Posted: Sat Apr 26, 2008 8:17 pm
by LuxuriousMeat
Use this code to load a file:

Code: Select all

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim File_Dialog As OpenFileDialog = New OpenFileDialog()
        Dim File_Name As String
        Dim File_Contents As String
        File_Dialog.DefaultExt = "text"
        File_Dialog.Title = "Open A Text File."
        If FileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            File_Name = File_Dialog.FileName
            File_Contents = My.Computer.FileSystem.ReadAllText(File_Name)
            RichTextBox1.Text = File_Contents
        End If
    End Sub
About your problem with bolding the text, I'm not sure, I've never really done anything with formatting text.

And to save, use this code:

Code: Select all

   Dim sfd As New SaveFileDialog()
   sfd.Filter = "Text Files (*.txt)|*.txt"
   If sfd.ShowDialog() = DialogResult.OK Then
       rtb.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText)
   End If

Posted: Sun Apr 27, 2008 12:09 am
by Patrickssj6
I think this code is better...

Opening:

Code: Select all

Dim File_Dialog As OpenFileDialog = New OpenFileDialog()
Dim AllText, LineOfText as String

File_Dialog.Filter = "Text files (*.TXT)|*.TXT"
File_Dialog.ShowDialog()

If File_Dialog.FileName <> "" Then 'this fixes your cancel error
Try
FileOpen(1,File_Dialog.FileName,OpenMode.Input)
Do Until EOF(1) 
LineOfText = LineInput(1)
AllText = AllText & LineOfText & vbCrLf
Loop
RichTextBox1.Text = AllText
Catch
Messagebox.Show("Error opening file")
Finally 
FileClose(1)
End Try
End If
or...haven't tried this one though

Code: Select all


'Don't forget Imports System.IO at the top of the class
Dim File_Dialog As OpenFileDialog = New OpenFileDialog()
File_Dialog.Filter = "Text files (*.TXT)|*.TXT"
File_Dialog.ShowDialog()

Dim Stream as StreamReader
Stream = New StreamReader(File_Dialog.FileName)
RichTextBox1.Text = Stream.ReadToEnd
Stream.Close()

and for saving...

Code: Select all

Dim File_Dialog As SaveFileDialog = New SaveFileDialog()
File_Dialog.ShowDialog()
If File_Dialog.FileName <> "" Then
FileOpen(1,File_Dialog.FileName,OpenMode.Output)
PrintLine(1,RichtTextbox1.Text)
FileClose()
End If


Posted: Wed Apr 30, 2008 5:20 pm
by Jdogg
For your font problem:

Code: Select all

        Dim rt_font As New Font(RichTextBox1.Font, FontStyle.Bold)
        RichTextBox1.Font = rt_font
[/code]