• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    ASP.NET窗體對話框的實現

    發布: 2008-10-13 11:48 | 作者: 不詳 | 來源: programfan | 查看: 138次 | 進入軟件測試論壇討論

    領測軟件測試網 窗體對話框組件與微軟視窗操作系統中的對話框是一樣的;也就是說,PrintDialog 組件是“打印”對話框,OpenFileDialog 組件是 “打開文件”對話框,依此類推。

      與以往的 Microsoft Visual Basic 6.0 等 Windows 程序設計語言相似,.NET 框架提供了 Windows 用戶耳熟能詳的對話框。對話框的具體用途(如 Printdialog 可用于文件打印等)通常是多種多樣的。故而在 .NET 框架提供的基礎類中不包含用于文件打印、顏色選擇等具體操作的代碼,而你卻可以根據應用程序的需要靈活地實現它們。因此,在 .NET 框架下,你不但可以直接應用標準對話框,而且能根據用戶的選擇作出不同的響應。本文提供的代碼其用途就在于此。

      注意,關于各種對話框的屬性、方法和事件的完整描述,可以在相應類的 Members 頁面中找到。比如要查看 OpenFileDialog 組件的某一方法,就可以在文檔索引的“OpenFileDialog class, all members”欄目中找到相關的主題。

    1.OpenFileDialog 組件

      OpenFileDialog 對話框使得用戶能夠通過瀏覽本地或者遠程的文件系統以打開所選文件。它將返回文件路徑和所選的文件名。

      OpenFileDialog 組件和SaveFileDialog 組件(下文將會詳細描述)包含了用于瀏覽和選取文件所必需的基本代碼。有了它們,你就不必為這些功能編寫任何代碼,進而能夠專心實現打開或者保存文件等具體操作。

      注意,FileDialog 類的 FilterIndex 屬性(由于繼承的原因,為 OpenFileDialog 和 SaveFileDialog 類所共有) 使用 one-based 索引(譯者注:指從 1 開始編號的索引)。 此特性將在下文的代碼中用到(并且會在相應位置再次強調)。當應用程序通過類型過濾器打開文件時,或者需要保存為特定格式的文件(比如:保存為純文本文件而不是二進制文件)時,這一點是非常重要的。人們在使用 FilterIndex 屬性時卻經常忘了它,因此現在務必要把它記住。

      下列代碼通過 Button 控件的 Click 事件調用 OpenFileDialog 組件。當用戶選中某個文件,并且單擊 OK 的時候,所選的文件將被打開。在本例中,文件內容將被顯示在消息框內,以證實文件流被讀入。

    本例假設存在名為 Button1 的 Button 控件和名為 OpenFileDialog1 的 OpenFileDialog 控件。

    ' Visual Basic
    ' NOTE: You must import the following namespace:
    ' Imports System.IO
    ' Without this import statement at the beginning
    ' of your code, the example will not function.
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim sr As New StreamReader(OpenFileDialog1.FileName)
    MessageBox.Show(sr.ReadToEnd)
    sr.Close()
    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 example will not function.
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    StreamReader sr = new StreamReader(openFileDialog1.FileName);
    MessageBox.Show(sr.ReadToEnd());
    sr.Close();
    }
    }

      打開文件還可以使用 OpenFileDialog 組件的 OpenFile 方法,它將返回文件的每一個字節。在下面的例子中,一個 OpenFileDialog 組件將被實例化,它使用了 cursor 過濾器,以限定用戶只能選取光標文件(擴展名為 .cur)。一旦某個 .cur 文件被選中,窗體的光標就被設成該文件描繪的光標形狀。

    本例假設存在名為 Button1 的 Button 控件。

    ' Visual Basic
    Private Sub Button1_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 方法。

    2.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 或者 .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)
    {

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    TAG: asp ASP net NET Net 窗體 對話框

    21/212>

    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>