Rich text box help needed!!  
Author Message
SP534





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

I'm pretty new to VB2005 Express and I'm trying to make an app with a rich text box and have no idea on how to code the 'save file dialog', the 'open file dialog', the 'font dialog', or the 'print dialog'.

I would really appreciate help.




Visual Studio Express Editions4  
 
 
Brendan Grant





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

I'd suggest taking a look at this tutorial which discusses all 4 dialogs and how to use them.

 
 
SP021





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

Well nearly everything works, but I cant figure out one little problem, one part says:

Private Sub FileName As String

There's something wrong with 'As String', VB underlines it in blue and says 'End of Statement Expected' and I went to the help and it told me to

Insert a line terminator after the element that completes the statement, but I'm not sure what a line terminator is. I've tried '.' ',' '*' ':' and '()' but no progress!

Hope someone knows how to fix it or could tell me what a line terminator is.



 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

you are creating a sub which appears to be wanting to return a string back to the caller but you can't do this as the signature is wrong. to return something back to the caller you need to make a function....

private function FileName as String

end function

again not entirely sure what you are wanting to do. Are you trying to make a function or a variable



 
 
Dave299





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

I think you'll find it actually said

Private FileName As String


 
 
SP021





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

Just tried what you said but that doesn't work either. What I'm trying to do is make open file, and save file dialogs, and for the file name I have:FileName = .FileName, but it always underlines filename and says "Name 'filename' is not declared" and I thought that that would fix it. I've also tried using Dim filename as string but that doesn't work either!

 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

can you post the code snippet you currently have so we can help rather than guessing :-)

 
 
spotty





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

In the samples I'm sure you have just copied part of the code, you are probably missing the following lines.

With OpenFileDialog1
'With statement is used to execute statements using a particular object, here,_
'OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
'setting filters so that Text files and All Files choice appears in the Files of Type box
'in the dialog
If .ShowDialog() = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
FileName = .FileName
sr = New StreamReader(.OpenFile)
'using streamreader to read the opened text file
RichTextBox1.Text = sr.ReadToEnd()
'displaying text from streamreader in richtextbox
End If
End With

Or you can fully qualify the item

FileName = OpenFileDialog1.FileName


 
 
SP021





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

This is what I have- for the save file dialog:

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click

Dim sw As IO.StreamWriter

'streamwriter is used to write text

Try

With SaveFileDialog1

.FileName = FileName

.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"

If .ShowDialog() = Windows.Forms.DialogResult.OK Then

FileName = .FileName

sw = New IO.StreamWriter(FileName)

'using streamwriter to write text from richtextbox and saving it

sw.Write(RichTextBox1.Text)

End If

End With

Catch es As Exception

MessageBox.Show(es.Message)

Finally

If Not (sw Is Nothing) Then

sw.Close()

End If

End Try

End Sub

and for the open file dialog:

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

Dim sr As IO.StreamReader

'streamreader is used to read text

Try

With OpenFileDialog1

'With statement is used to execute statements using a particular object, here,_

'OpenFileDialog1

.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"

'setting filters so that Text files and All Files choice appears in the Files of Type box

'in the dialog

If .ShowDialog() = Windows.Forms.DialogResult.OK Then

'showDialog method makes the dialog box visible at run time

FileName = .FileName

sr = New IO.StreamReader(.OpenFile)

'using streamreader to read the opened text file

RichTextBox1.Text = sr.ReadToEnd()

'displaying text from streamreader in richtextbox

End If

End With

Catch es As Exception

MessageBox.Show(es.Message)

Finally

If Not (sr Is Nothing) Then

sr.Close()

End If

End Try

End Sub



 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

you have not declared "FileName" anywhere so you may well be getting the error.

you could declare it globally at the top of the class...

private FileName as String = String.Empty

example

imports System

Imports System.Windows.Forms

public class MyClass

private FileName as String = String.Empty

'methods here

'etc.....

end class



 
 
SP021





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

I just tried that, and I've fully qualified the item like you said, doesn't work, I've posted what code I have, but somehow it ended up over your post.

 
 
spotty





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

The following code is yours with some change - you hadnt declared the string variable you were trying to assign called 'filename' and this variable has the same name as a property which confuses things a little from a readability point of view.

Personally if I was using VB 2005 I'd ditch using the stremareader and streamwriter and using the My.Computer.Filesystem.ReadAllText and My.Computer.Filesystem.WriteAllText methods as they are single line statements to handle reading and writing of textfiles

Public Class Form1
Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
Dim sw As IO.StreamWriter
Dim str_filename As String = ""
'streamwriter is used to write text
Try
With SaveFileDialog1
.FileName = str_filename
.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
str_filename = .FileName
sw = New IO.StreamWriter(str_filename)
'using streamwriter to write text from richtextbox and saving it
sw.Write(RichTextBox1.Text)
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sw Is Nothing) Then
sw.Close()
End If
End Try
End Sub

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
Dim sr As IO.StreamReader
Dim str_filename As String = ""

'streamreader is used to read text

Try

With OpenFileDialog1
'With statement is used to execute statements using a particular object, here,_
'OpenFileDialog1
.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
'setting filters so that Text files and All Files choice appears in the Files of Type box
'in the dialog
If .ShowDialog() = Windows.Forms.DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
str_filename = .FileName
sr = New IO.StreamReader(.OpenFile)

'using streamreader to read the opened text file
RichTextBox1.Text = sr.ReadToEnd()

'displaying text from streamreader in richtextbox
End If
End With
Catch es As Exception
MessageBox.Show(es.Message)
Finally
If Not (sr Is Nothing) Then
sr.Close()
End If
End Try
End Sub
End Class



 
 
SP_021





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

All works great now, but when I highlight any text, or click in the text box, an unhandled exeption occurs and this is the error i get:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Jotter 1.0
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Documents%20and%20Settings/Sean/My%20Documents/Visual%20Studio%202005/Projects/Jotter%201.0/Jotter%201.0/bin/Debug/Jotter%201.0.exe
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
skincrafter.net-vs2005_light
Assembly Version: 2.1.0.0
Win32 Version: 2, 1, 0, 0
CodeBase: file:///C:/Documents%20and%20Settings/Sean/My%20Documents/Visual%20Studio%202005/Projects/Jotter%201.0/Jotter%201.0/bin/Debug/skincrafter.net-vs2005_light.DLL
----------------------------------------
msvcm80
Assembly Version: 8.0.50727.163
Win32 Version: 8.00.50727.163
CodeBase: file:///C:/WINDOWS/WinSxS/x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.163_x-ww_681e29fb/msvcm80.dll
----------------------------------------
Accessibility
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT de**** registered on the computer
rather than be handled by this dialog box.



 
 
Brendan Grant





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

Because this question/thread is a month old and the only people who will receive notification of your post to it are those who had originally posted to it... I'd suggest reposting this new question in a seperate thread in the hopes that more people would see it and be able to assist more quickly.

 
 
SP_021





PostPosted: Visual Basic Express Edition, Rich text box help needed!! Top

Yeah i suppose so, Thx