c# 對話方塊控制項

來源:互聯網
上載者:User

標籤:彈出對話方塊   exception   沒有   管道   ogre   cat   rectangle   bsp   pen   

  對話方塊也是一種表單,通常調用對對話方塊相互關聯類型的ShowDialog方法來彈出對話方塊,當使用者關閉對話方塊後,該方法會返回一個DialogResult枚舉值,通過該值就可以判斷使用者採取了什麼操作,例如單擊確認按鈕後,對話方塊關閉,showDialog方法返回DialogResult.ok,更具傳回值就能知道確認了操作。

  FileDialog類提供了選擇檔案對話方塊的基本結構,他是一個抽象類別,並派生出兩個子類——OpenFileDialog和SaveFialeDialog

  OpenDialog用於開啟檔案,SaveFialeDialog用於儲存檔案是選新檔案夾。開啟檔案對話方塊應該選擇已存在的檔案,所以通過CheckFileExists屬性控制是否檢查檔案的存在性,預設為True,應為開啟不存在的檔案沒有意義。在使用SaveFileDialog來選擇新檔案的檔案名稱,有時候會遇到儲存的檔案已經存在的情況,所以OverwritePrompt屬性應該為True,當儲存的檔案存在的情況下提示使用者是否要覆蓋現有檔案。

  Filter屬性是指定在選擇檔案對哈框中應該顯示那些檔案。用字串表示。

文字檔(*.txt)|*.txt  (|  符號,管道符號,分隔)

  在選擇完成後,單擊“確認按鈕”關閉對話方塊,可以從FileName屬性獲得檔案名稱字,該屬性是返回的檔案全部路徑。

  對於OpenFileDialog來說,Multiselect屬性為Ture,支援選擇多個檔案,可以從FileNames屬性中得到一個string數組,代表使用者已經選擇的檔案清單。

例如:OpenFileDialog和SaveFileDialog對話方塊的使用

在開啟按鈕Click事件添加

    private void button1_Click(object sender, EventArgs e)        {            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)            {                //顯示檔案名稱                label1.Text  = openFileDialog1.FileName;                //載入圖片                try                {                    using (System.IO.FileStream Stream = System.IO.File.Open(openFileDialog1.                        FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))                    {                        //建立映像                        Image img = Image.FromStream(Stream);                        //在pictureBox中顯示映像                        pictureBox1.Image = img;                        //關閉檔案流,釋放資源                        Stream.Close();                    }                }                catch (Exception ex)                {                    label1.Text = ex.Message;//顯示資訊                }            }        }

  首先電泳ShowDialog方法顯示選擇檔案的對話方塊,如果使用者進行選擇並單擊確認按鈕,返回DialogResult.OK就從FileName屬性中得到選擇檔案的路徑。通過File類的靜態方法Open開啟檔案,返迴文件流

,在檔案流基礎上建立Image對象,顯示在PictureBox控制項中。

在儲存Click事件中添加

 private void button2_Click(object sender, EventArgs e)        {            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)            {                //準備寫入檔案                System.IO.FileStream StreamOut = null;                try                {                    StreamOut = new System.IO.FileStream(saveFileDialog1.FileName, System.IO.FileMode.OpenOrCreate, System.                        IO.FileAccess.Write, System.IO.FileShare.Read);                    using (Bitmap bmp = new Bitmap(100, 100))                    {                        Graphics g = Graphics.FromImage(bmp);                        //填充背景                        g.Clear(Color.DarkBlue);                        //填充圓形地區                        g.FillEllipse(Brushes.Yellow, new Rectangle(0, 0, bmp.Width, bmp.Height));                        //釋放對象                        g.Dispose();                        //將映像內容寫入檔案流                        bmp.Save(StreamOut, System.Drawing.Imaging.ImageFormat.Png);                    }                    //顯示儲存成功                    MessageBox.Show("影像檔儲存成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message);                }                finally                {                    //釋放檔案流佔用的資源                    if (StreamOut != null)                    {                        StreamOut.Close();                        StreamOut.Dispose();                    }                }            }        }

 

c# 對話方塊控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.