By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,683 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,683 IT Pros & Developers. It's quick & easy.

how to use example code from help

P: n/a
cj
Lets just take this example I'm looking at now. I'm looking at the help
screen titled .NET Framework Class Library FolderBrowserDialog Class
[Visual Basic]. It gives an example at the bottom that begins with:

' The following example displays an application that provides the
ability to
' open rich text files (rtf) into the RichTextBox. The example demonstrates
' using the FolderBrowserDialog to set the default directory for opening
files.
' The OpenFileDialog class is used to open the file.

I don't know how to put this example in a program to try it. Where do I
paste the example to run it? Under form load? I don't think so. But
where?
Dec 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
If you are running 2005, then that example is the complete class code. I
would create a new form named "FolderBrowserDialogExampleForm"

Then open it up in code view, select everything and hit delete. Then just
paste in the code from the example.

Viola, instant form!

If you have 2003, you have to be more careful not to delete or paste over
the " Windows Form Designer generated code " region.

Take all the code in the Sub New and cut and paste it after this line:
'Add any initialization after the InitializeComponent() call

Then delete the now empty Sub New from the example.

HTH,
Greg

"cj" <cj@nospam.nospam> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Lets just take this example I'm looking at now. I'm looking at the help
screen titled .NET Framework Class Library FolderBrowserDialog Class
[Visual Basic]. It gives an example at the bottom that begins with:

' The following example displays an application that provides the ability
to
' open rich text files (rtf) into the RichTextBox. The example
demonstrates
' using the FolderBrowserDialog to set the default directory for opening
files.
' The OpenFileDialog class is used to open the file.

I don't know how to put this example in a program to try it. Where do I
paste the example to run it? Under form load? I don't think so. But
where?

Dec 22 '05 #2

P: n/a
cj
Ok, I'll look in a minute and see if I can figure it out. I'll let you
know. I am using 2003 and what you suggested for 2005 is what I had
done but your right it don't work in 2003.

How do you like 2005? Is it an official release yet or is it still
beta? Is it easier than 2003? Better? Just interested in your thoughts.

I'll get to work on that sample now.

Greg Burns wrote:
If you are running 2005, then that example is the complete class code. I
would create a new form named "FolderBrowserDialogExampleForm"

Then open it up in code view, select everything and hit delete. Then just
paste in the code from the example.

Viola, instant form!

If you have 2003, you have to be more careful not to delete or paste over
the " Windows Form Designer generated code " region.

Take all the code in the Sub New and cut and paste it after this line:
'Add any initialization after the InitializeComponent() call

Then delete the now empty Sub New from the example.

HTH,
Greg

"cj" <cj@nospam.nospam> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Lets just take this example I'm looking at now. I'm looking at the help
screen titled .NET Framework Class Library FolderBrowserDialog Class
[Visual Basic]. It gives an example at the bottom that begins with:

' The following example displays an application that provides the ability
to
' open rich text files (rtf) into the RichTextBox. The example
demonstrates
' using the FolderBrowserDialog to set the default directory for opening
files.
' The OpenFileDialog class is used to open the file.

I don't know how to put this example in a program to try it. Where do I
paste the example to run it? Under form load? I don't think so. But
where?


Dec 22 '05 #3

P: n/a
I downloaded my copy from MSDN a month ago. But I have in my hand the
retail upgrade edition my boss just got delivered today.

I love 2005, but there is SO much to learn. Everything from 2003 just
works as is, but there is so many new things I keep discovering and want to
start using.

Luckily, I haven't seen any of the ugly IDE issues some others are
complaining about.

Greg

"cj" <cj@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Ok, I'll look in a minute and see if I can figure it out. I'll let you
know. I am using 2003 and what you suggested for 2005 is what I had done
but your right it don't work in 2003.

How do you like 2005? Is it an official release yet or is it still beta?
Is it easier than 2003? Better? Just interested in your thoughts.

I'll get to work on that sample now.

Greg Burns wrote:
If you are running 2005, then that example is the complete class code. I
would create a new form named "FolderBrowserDialogExampleForm"

Then open it up in code view, select everything and hit delete. Then
just paste in the code from the example.

Viola, instant form!

If you have 2003, you have to be more careful not to delete or paste over
the " Windows Form Designer generated code " region.

Take all the code in the Sub New and cut and paste it after this line:
'Add any initialization after the InitializeComponent() call

Then delete the now empty Sub New from the example.

HTH,
Greg

"cj" <cj@nospam.nospam> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Lets just take this example I'm looking at now. I'm looking at the help
screen titled .NET Framework Class Library FolderBrowserDialog Class
[Visual Basic]. It gives an example at the bottom that begins with:

' The following example displays an application that provides the ability
to
' open rich text files (rtf) into the RichTextBox. The example
demonstrates
' using the FolderBrowserDialog to set the default directory for opening
files.
' The OpenFileDialog class is used to open the file.

I don't know how to put this example in a program to try it. Where do I
paste the example to run it? Under form load? I don't think so. But
where?



Dec 22 '05 #4

P: n/a
cj
Well, I guess I'll keep learning 2003 till I get 2005. At least what
I'm learning now will transfer over.

Back to the example code question. This example has everything inside a
Public Class FolderBrowserDialogExampleForm. I don't think I can just
take the sub new code out and put it inside that windows generated code
area wher you said. I tried but it gives me many errors.

I created a new solution and selected view code. I get this:
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

End Class

Where do I paste the rest of the stuff in the example? Does Microsoft
have any documentation on how to use this sample code? I couldn't find
any.

Greg Burns wrote:
I downloaded my copy from MSDN a month ago. But I have in my hand the
retail upgrade edition my boss just got delivered today.

I love 2005, but there is SO much to learn. Everything from 2003 just
works as is, but there is so many new things I keep discovering and want to
start using.

Luckily, I haven't seen any of the ugly IDE issues some others are
complaining about.

Greg

"cj" <cj@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Ok, I'll look in a minute and see if I can figure it out. I'll let you
know. I am using 2003 and what you suggested for 2005 is what I had done
but your right it don't work in 2003.

How do you like 2005? Is it an official release yet or is it still beta?
Is it easier than 2003? Better? Just interested in your thoughts.

I'll get to work on that sample now.

Greg Burns wrote:
If you are running 2005, then that example is the complete class code. I
would create a new form named "FolderBrowserDialogExampleForm"

Then open it up in code view, select everything and hit delete. Then
just paste in the code from the example.

Viola, instant form!

If you have 2003, you have to be more careful not to delete or paste over
the " Windows Form Designer generated code " region.

Take all the code in the Sub New and cut and paste it after this line:
'Add any initialization after the InitializeComponent() call

Then delete the now empty Sub New from the example.

HTH,
Greg

"cj" <cj@nospam.nospam> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
Lets just take this example I'm looking at now. I'm looking at the help
screen titled .NET Framework Class Library FolderBrowserDialog Class
[Visual Basic]. It gives an example at the bottom that begins with:

' The following example displays an application that provides the ability
to
' open rich text files (rtf) into the RichTextBox. The example
demonstrates
' using the FolderBrowserDialog to set the default directory for opening
files.
' The OpenFileDialog class is used to open the file.

I don't know how to put this example in a program to try it. Where do I
paste the example to run it? Under form load? I don't think so. But
where?

Dec 22 '05 #5

P: n/a
Not to beat a dead horse, but in fact my advice for VB 2005 wasn't
technically correct either. Some of that code should be placed in the
form1.designer.vb partial class file to allow the IDE to be able to modify
it as well.

"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Whoops! While the previous post would work, I should have pasted some of
that stuff inside the InitializeComponent sub. That way the form can be
further modified using the designer.

Greg

Dec 22 '05 #6

P: n/a
cj
Thank you! I ran the program so it's working. I'll take a good look at
it to see how you did what you did. Hopefully it'll make sense to me.
It's about time I wrap things up here at the office to leave for
Christmas. I might be working on this over the holiday some though.

Have a Merry Christmas! And thanks again.
cj

Greg Burns wrote:
Whoops! While the previous post would work, I should have pasted some of
that stuff inside the InitializeComponent sub. That way the form can be
further modified using the designer.

Greg
' The following example displays an application that provides the ability to
' open rich text files (rtf) into the RichTextBox. The example demonstrates
' using the FolderBrowserDialog to set the default directory for opening
files.
' The OpenFileDialog class is used to open the file.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO

Public Class FolderBrowserDialogExampleForm
Inherits Form

Private folderBrowserDialog1 As FolderBrowserDialog
Private openFileDialog1 As OpenFileDialog

Private richTextBox1 As RichTextBox

Private mainMenu1 As MainMenu
Private fileMenuItem As MenuItem
Private WithEvents folderMenuItem As MenuItem, _
closeMenuItem As MenuItem, _
openMenuItem As MenuItem

Private openFileName As String, folderName As String

Private fileOpened As Boolean = False

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
'
'FolderBrowserDialogExampleForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "FolderBrowserDialogExampleForm"
Me.Text = "Form1"

Me.mainMenu1 = New System.Windows.Forms.MainMenu
Me.fileMenuItem = New System.Windows.Forms.MenuItem
Me.openMenuItem = New System.Windows.Forms.MenuItem
Me.folderMenuItem = New System.Windows.Forms.MenuItem
Me.closeMenuItem = New System.Windows.Forms.MenuItem

Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.folderBrowserDialog1 = New
System.Windows.Forms.FolderBrowserDialog
Me.richTextBox1 = New System.Windows.Forms.RichTextBox

Me.mainMenu1.MenuItems.Add(Me.fileMenuItem)
Me.fileMenuItem.MenuItems.AddRange( _
New System.Windows.Forms.MenuItem() {Me.openMenuItem, _
Me.closeMenuItem, _
Me.folderMenuItem})
Me.fileMenuItem.Text = "File"

Me.openMenuItem.Text = "Open..."

Me.folderMenuItem.Text = "Select Directory..."

Me.closeMenuItem.Text = "Close"
Me.closeMenuItem.Enabled = False

Me.openFileDialog1.DefaultExt = "rtf"
Me.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf"

' Set the Help text description for the FolderBrowserDialog.
Me.folderBrowserDialog1.Description = _
"Select the directory that you want to use As the default."

' Do not allow the user to create New files via the
FolderBrowserDialog.
Me.folderBrowserDialog1.ShowNewFolderButton = False

' Default to the My Documents folder.
Me.folderBrowserDialog1.RootFolder =
Environment.SpecialFolder.Personal

Me.richTextBox1.AcceptsTab = True
Me.richTextBox1.Location = New System.Drawing.Point(8, 8)
Me.richTextBox1.Size = New System.Drawing.Size(280, 344)
Me.richTextBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or _
AnchorStyles.Bottom Or AnchorStyles.Right

Me.ClientSize = New System.Drawing.Size(296, 360)
Me.Controls.Add(Me.richTextBox1)
Me.Menu = Me.mainMenu1
Me.Text = "RTF Document Browser"
End Sub

#End Region

<STAThread()> _
Shared Sub Main()
Application.Run(New FolderBrowserDialogExampleForm)
End Sub

' Bring up a dialog to open a file.
Private Sub openMenuItem_Click(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles openMenuItem.Click
' If a file is not opened, then set the initial directory to the
' FolderBrowserDialog.SelectedPath value.
If (Not fileOpened) Then
openFileDialog1.InitialDirectory =
folderBrowserDialog1.SelectedPath
openFileDialog1.FileName = Nothing
End If

' Display the openFile dialog.
Dim result As DialogResult = openFileDialog1.ShowDialog()

' OK button was pressed.
If (result = DialogResult.OK) Then
openFileName = openFileDialog1.FileName
Try
' Output the requested file in richTextBox1.
Dim s As Stream = openFileDialog1.OpenFile()
richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText)
s.Close()

fileOpened = True

Catch exp As Exception
MessageBox.Show("An error occurred while attempting to load
the file. The error is:" _
+ System.Environment.NewLine +
exp.ToString() + System.Environment.NewLine)
fileOpened = False
End Try
Invalidate()

closeMenuItem.Enabled = fileOpened

' Cancel button was pressed.
ElseIf (result = DialogResult.Cancel) Then
Return
End If
End Sub

' Close the current file.
Private Sub closeMenuItem_Click(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles closeMenuItem.Click
richTextBox1.Text = ""
fileOpened = False

closeMenuItem.Enabled = False
End Sub

' Bring up a dialog to chose a folder path in which to open or save a
file.
Private Sub folderMenuItem_Click(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles folderMenuItem.Click
' Show the FolderBrowserDialog.
Dim result As DialogResult = folderBrowserDialog1.ShowDialog()

If (result = DialogResult.OK) Then
folderName = folderBrowserDialog1.SelectedPath
If (Not fileOpened) Then
' No file is opened, bring up openFileDialog in selected
path.
openFileDialog1.InitialDirectory = folderName
openFileDialog1.FileName = Nothing
openMenuItem.PerformClick()
End If
End If
End Sub

End Class

' The following example displays an application that provides the ability to
' open rich text files (rtf) into the RichTextBox. The example demonstrates
' using the FolderBrowserDialog to set the default directory for opening files.
' The OpenFileDialog class is used to open the file.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO

Public Class FolderBrowserDialogExampleForm
Inherits Form

Private folderBrowserDialog1 As FolderBrowserDialog
Private openFileDialog1 As OpenFileDialog

Private richTextBox1 As RichTextBox

Private mainMenu1 As MainMenu
Private fileMenuItem As MenuItem
Private WithEvents folderMenuItem As MenuItem, _
closeMenuItem As MenuItem, _
openMenuItem As MenuItem

Private openFileName As String, folderName As String

Private fileOpened As Boolean = False

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'FolderBrowserDialogExampleForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "FolderBrowserDialogExampleForm"
Me.Text = "Form1"

Me.mainMenu1 = New System.Windows.Forms.MainMenu
Me.fileMenuItem = New System.Windows.Forms.MenuItem
Me.openMenuItem = New System.Windows.Forms.MenuItem
Me.folderMenuItem = New System.Windows.Forms.MenuItem
Me.closeMenuItem = New System.Windows.Forms.MenuItem

Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
Me.richTextBox1 = New System.Windows.Forms.RichTextBox

Me.mainMenu1.MenuItems.Add(Me.fileMenuItem)
Me.fileMenuItem.MenuItems.AddRange( _
New System.Windows.Forms.MenuItem() {Me.openMenuItem, _
Me.closeMenuItem, _
Me.folderMenuItem})
Me.fileMenuItem.Text = "File"

Me.openMenuItem.Text = "Open..."

Me.folderMenuItem.Text = "Select Directory..."

Me.closeMenuItem.Text = "Close"
Me.closeMenuItem.Enabled = False

Me.openFileDialog1.DefaultExt = "rtf"
Me.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf"

' Set the Help text description for the FolderBrowserDialog.
Me.folderBrowserDialog1.Description = _
"Select the directory that you want to use As the default."

' Do not allow the user to create New files via the FolderBrowserDialog.
Me.folderBrowserDialog1.ShowNewFolderButton = False

' Default to the My Documents folder.
Me.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal

Me.richTextBox1.AcceptsTab = True
Me.richTextBox1.Location = New System.Drawing.Point(8, 8)
Me.richTextBox1.Size = New System.Drawing.Size(280, 344)
Me.richTextBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or _
AnchorStyles.Bottom Or AnchorStyles.Right

Me.ClientSize = New System.Drawing.Size(296, 360)
Me.Controls.Add(Me.richTextBox1)
Me.Menu = Me.mainMenu1
Me.Text = "RTF Document Browser"
End Sub

#End Region

<STAThread()> _
Shared Sub Main()
Application.Run(New FolderBrowserDialogExampleForm)
End Sub

' Bring up a dialog to open a file.
Private Sub openMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles openMenuItem.Click
' If a file is not opened, then set the initial directory to the
' FolderBrowserDialog.SelectedPath value.
If (Not fileOpened) Then
openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath
openFileDialog1.FileName = Nothing
End If

' Display the openFile dialog.
Dim result As DialogResult = openFileDialog1.ShowDialog()

' OK button was pressed.
If (result = DialogResult.OK) Then
openFileName = openFileDialog1.FileName
Try
' Output the requested file in richTextBox1.
Dim s As Stream = openFileDialog1.OpenFile()
richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText)
s.Close()

fileOpened = True

Catch exp As Exception
MessageBox.Show("An error occurred while attempting to load the file. The error is:" _
+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine)
fileOpened = False
End Try
Invalidate()

closeMenuItem.Enabled = fileOpened

' Cancel button was pressed.
ElseIf (result = DialogResult.Cancel) Then
Return
End If
End Sub

' Close the current file.
Private Sub closeMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles closeMenuItem.Click
richTextBox1.Text = ""
fileOpened = False

closeMenuItem.Enabled = False
End Sub

' Bring up a dialog to chose a folder path in which to open or save a file.
Private Sub folderMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles folderMenuItem.Click
' Show the FolderBrowserDialog.
Dim result As DialogResult = folderBrowserDialog1.ShowDialog()

If (result = DialogResult.OK) Then
folderName = folderBrowserDialog1.SelectedPath
If (Not fileOpened) Then
' No file is opened, bring up openFileDialog in selected path.
openFileDialog1.InitialDirectory = folderName
openFileDialog1.FileName = Nothing
openMenuItem.PerformClick()
End If
End If
End Sub

End Class

Dec 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.