_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'' Display an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Cursor Files|*.cur"
openFileDialog1.Title = "Select a Cursor File"
'' Show the Dialog.
'' If the user clicked OK in the dialog and
'' a .CUR file was selected, open it.
If openFileDialog1.ShowDialog() = DialogResult.OK Then
If openFileDialog1.FileName <> "" Then
'' Assign the cursor in the Stream to the Form''s Cursor property.
Me.Cursor = New Cursor(openFileDialog1.OpenFile())
End If
End If
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
// Display an OpenFileDialog so the user can select a Cursor.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Cursor Files|*.cur";
openFileDialog1.Title = "Select a Cursor File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .CUR file was selected, open it.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if(openFileDialog1.FileName != "")
{
// Assign the cursor in the Stream to the Form''s Cursor property.
this.Cursor = new Cursor(openFileDialog1.OpenFile());
}
}
}
关于读取文件流的进一步信息,请参阅FileStream.BeginRead 方法。
SaveFileDialog 组件
本对话框允许用户浏览文件系统并且选取将被写入的文件。当然,你还必须为文件写入编写具体代码。
下列代码通过 Button 控件的 Click 事件调用 SaveFileDialog 组件。当用户选中某个文件,并且单击 OK 的时候,RichTextBox 控件里的内容将被保存到所选的文件中。
本例假设存在名为 Button1 的 Button 控件,名为 RichTextBox1 的 RichTextBox 控件和名为 OpenFileDialog1 的 SaveFileDialog 控件。
'' Visual Basic
'' NOTE: You must import the following namespace:
'' Imports System.IO
'' Without this import statement at the beginning
'' of your code, the code example will not function.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
RichTextBox1.SaveFile(SaveFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
End If
End Sub
// C#
// NOTE: You must import the following namespace:
// using System.IO;
// Without this import statement at the beginning
// of your code, the code example will not function.
private void button1_Click(object sender, System.EventArgs e)
{
if((saveFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
}
保存文件还可以用 SaveFileDialog 组件的 OpenFile 方法,它将提供一个用于写入的 Stream 对象。
在下面的例子中,有一个包含图片的 Button 控件。 当你单击这个按钮的时候,一个 SaveFileDialog 组件将被打开,它将使用 .gif 、 .jpeg 和 .bmp 类型的文件过滤器。一旦用户通过 Save File 对话框内选中此类文件,按钮上的图片将被存入其中。
本例假设存在名为 Button2 的 Button 控件,并且它的 Image 属性被设为某个扩展名为 .gif 、 .jpeg 或者 .b