ASP.NET窗体对话框的实现(2)
2008-02-22 09:27:00来源:互联网 阅读 ()
' 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 或者 .bmp 的图片文件。
'Visual Basic
' NOTE: You must import the following namespaces:
' Imports System.IO
' Imports System.Drawing.Imaging
' Without these import statements at the beginning of your code,
' the code example will not function.
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
' Display an SaveFileDialog so the user can save the Image
' assigned to Button2.
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = "Save an Image File"
saveFileDialog1.ShowDialog()
' If the file name is not an empty string open it for saving.
If saveFileDialog1.FileName <> "" Then
' Save the Image via a FileStream created by the OpenFile method.
Dim fs As FileStream = CType(saveFileDialog1.OpenFile(), FileStream)
' Save the Image in the appropriate ImageFormat based upon the
' file type selected in the dialog box.
' NOTE that the FilterIndex property is one-based.
Select Case saveFileDialog1.FilterIndex
Case 1
Me.button2.Image.Save(fs, ImageFormat.Jpeg)
Case 2
Me.button2.Image.Save(fs, ImageFormat.Bmp)
Case 3
Me.button2.Image.Save(fs, ImageFormat.Gif)
End Select
fs.Close()
End If
End Sub
// C#
// NOTE: You must import the following namespaces:
// using System.IO;
// using System.Drawing.Imaging;
// Without these import statements at the beginning of your code,
// the code example will not function.
private void button2_Click(object sender, System.EventArgs e)
{
// Display an SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Save the Image via a FileStream created by the OpenFile method.
FileStream fs = (FileStream)saveFileDialog1.OpenFile();
// Save the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch(saveFileDialog1.FilterIndex)
{
case 1 :
this.button2.Image.Save(fs,ImageFormat.Jpeg);
break;
case 2 :
this.button2.Image.Save(fs,ImageFormat.Bmp);
break;
case 3 :
this.button2.Image.Save(fs,ImageFormat.Gif);
break;
}
fs.Close();
}
}
关于写入文件流的进一步信息,请参阅 FileStream.BeginWrite 方法。
ColorDialog 组件
此对话框显示颜色列表,并且返回所选的颜色。
与前两种对话框不同,ColorDialog 组件很容易实现其主要功能(挑选颜色)。选取的颜色将成为 Color 属性的设定值。因此,使用颜色就和设定属性值一样简单。在下面的例子中,按钮控制的 Click 事件将会开启一个 ColorDialog 组件。一旦用户选中某种颜色,并且单击了 OK ,按钮的背景将被设成所选的颜色。本例假设存在名为 Button1 的 Button 组件和名为 ColorDialog1 的 ColorDialog 组件。
' Visual Basic
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If ColorDialog1.ShowDialog() = DialogResult.OK Then
Button1.BackColor = ColorDialog1.Color
End If
End Sub
// C#
private void button1_Click(object sender, System.EventArgs e)
{
if(colorDialog1.ShowDialog() == DialogResult.OK)
{
button1.BackColor = colorDialog1.Color;
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:ASP.NET中的状态管理
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
