c#中的幾種Dialog


1、OpenFileDialog

 1         private void FileOpen_Click(object sender, EventArgs e)
 2         {
 3             OpenFileDialog openFile = new OpenFileDialog();//創建OpenFileDialog對象
 4 
 5             openFile.InitialDirectory = @"E:\";//打開初始目錄
 6             openFile.Title = "選擇打開文件";//窗體標題
 7             openFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";//過濾條件
 8             openFile.FilterIndex = 2;//獲取第二個過濾條件開始的文件
 9             openFile.Multiselect = true;//是否多選
10 
11             if (openFile.ShowDialog() == DialogResult.OK)//頁面彈出判斷是否點擊確定按鈕
12             {
13                 //沒勾選多選時
14                 //string filename = openFile.FileName;
15                 //string name=openFile.SafeFileName; 
16 
17                 //勾選多選時
18                 for (int i = 0; i < openFile.SafeFileNames.Length; i++)//獲取文件名,拓展名
19                 {
20                     rictbo.Text += openFile.SafeFileNames[i] + "\r\n";
21                 }
22                 for (int i = 0; i < openFile.FileNames.Length; i++)//獲取文件全部路徑
23                 {
24                     rictbo.Text += openFile.FileNames[i] + "\r\n";
25                 }
26             }
27         }
OpenFileDialog常用屬性

2、SaveFileDialog

 SaveFileDialog與OpenFileDialog屬性基本相同就簡單寫了

 1         private void button1_Click(object sender, EventArgs e)
 2         {
 3             SaveFileDialog saveFile = new SaveFileDialog();
 4 
 5             saveFile.InitialDirectory= @"E:\";//打開初始目錄
 6             saveFile.Title = "選擇保存文件";
 7             saveFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*|圖片(.jpg)|*.jpg";//過濾條件
 8             saveFile.FilterIndex = 1;//獲取第二個過濾條件開始的文件拓展名
 9             saveFile.FileName = "新建";//默認保存名稱
10             
11             if (saveFile.ShowDialog()==DialogResult.OK)//頁面彈出判斷是否點擊確定按鈕
12             {
13                 string txt = rictbo.Text;
14                 //寫入
15                 File.WriteAllText(saveFile.FileName, txt);
16             }
17         }
18     }
SaveFileDialog常用屬性

 

3、FolderBrowserDialog

這個使用的不多日后用到再添加

 1         private void button2_Click(object sender, EventArgs e)
 2         {
 3             FolderBrowserDialog dialog = new FolderBrowserDialog();
 4             dialog.Description = "選擇匹配目錄"; ;//左上角提示
 5             string path = string.Empty;
 6 
 7             if (dialog.ShowDialog() == DialogResult.OK)
 8             {
 9                 path = dialog.SelectedPath;//獲取選中文件路徑
10             }
11         }
FolderBrowserDialog使用

4、FontDialog

 1         private void button3_Click(object sender, EventArgs e)
 2         {
 3             FontDialog fontDialog = new FontDialog();
 4 
 5             fontDialog.ShowColor=true;//顯示顏色選擇
 6             fontDialog.Font = rictbo.Font;
 7             fontDialog.Color = rictbo.ForeColor;
 8 
 9             if (fontDialog.ShowDialog()==DialogResult.OK)//頁面彈出判斷是否點擊確定按鈕
10             {
11                 rictbo.Font = fontDialog.Font;//字體
12                 rictbo.ForeColor = fontDialog.Color;//字體顏色
13             }
14         }
FontDialog常用屬性

5、ColorDialog

 1         private void color_Click(object sender, EventArgs e)
 2         {
 3             ColorDialog colorDialog = new ColorDialog();
 4 
 5             //colorDialog.AllowFullOpen = false;是否啟用自定義顏色
 6             colorDialog.Color = rictbo.ForeColor;
 7             if (colorDialog.ShowDialog()==DialogResult.OK)
 8             {
 9                 rictbo.ForeColor = colorDialog.Color;
10             }
11         }
ColorDialog常用屬性

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM