C # Note 6 dialog box, menu, toolbar, and status bar

Source: Internet
Author: User

I. Dialog Box

Standard dialog box provided by Windows: open file, save as, browse folder, color dialog box, font dialog box

C # provides two kinds of special dialog boxes: message dialog box and dialog box


1. Open the openfiledialog dialog box.

(1) filter attribute: Excel



File
| *. Xls




", The previous"
Excel

File "becomes a tag and is a readable string that can be customized."
| *. Xls

"Is a filter, indicating that the Suffix in the filter folder is
. Xls

File
,

"
*

"Indicates matching
Excel

File Name string.

  • Filter is null or empty, indicating that all files are displayed and folders are always displayed.
  • You need to filter specific files and set the filter attribute to "tag | * tags ".
  • You need to filter multiple files. For example, you need to filter image files, but there are several suffixes of image files, such as JPG, PNG, and GIF. When you need to filter these files at the same time, set the filter attribute: tag | *. JPG ;*. PNG ;*. GIF ", Note: Only a few suffixes are added to the filter, and different suffixes are separated by semicolons.
  • When you need to filter multiple files, but not all of them are listed at the same time, you only need to select the desired file type from the drop-down list. In this case, you only need to set several filters. The filter attribute is set as follows: "tag 1 | *. jpg | tag 2 |. PNG | tag 3 |. GIF ". Note: Use "|" to separate different filters.

(2) multiselect: multiple choice

(3) filenames attribute: used to obtain the names of all selected files in the dialog box. Each file name contains both the file path and the file extension. If no file is selected, this method returns an empty array.
(4) restoredirectory attribute: used to obtain or set a value indicating whether to restore the current directory before closing the dialog box. If you change the Directory and the attribute value is true during file search, the dialog box restores the current directory to the initial value. If the attribute value is false, the original value is not returned. The default value is false.

Run: Common Dialog Box object name. showdialog ();
Return Value: If you click OK in the dialog box, the returned value is dialogresult. OK; otherwise, the returned value is dialogresult. Cancel.

Private void button#click (Object sender, eventargs e) <br/>{< br/> openfiledialog1.title = "open the file .. "; // window title," open "by default <br/> openfiledialog1.filter =" document (*. doc ;*. XLS) | *. doc ;*. XLS | image (*. PNG ;*. PIC ;*. JPG) | *. PNG ;*. PIC ;*. jpg "; <br/> openfiledialog1.filterindex = 1; // The selected filter index <br/> openfiledialog1.initialdirectory = application. executablepath; // initial directory <br/> openfiledialog1.multiselect = true; // whether to select multiple options <br/> If (openfiledialog1.showdialog () = dialogresult. OK) // whether the file is selected <br/>{< br/> for (INT I = 0; I <openfiledialog1.filenames. length; I ++) <br/>{< br/> richtextbox1.text + = openfiledialog1.filenames [I] + "/N "; <br/>}< br/>}

 

2. Save As dialog box savefiledialog

Example: one button opens a text file and displays the content in the RichTextBox Control. The other button writes the RichTextBox content to the text file.

Using system. IO;

Private void button2_click (Object sender, eventargs e) <br/>{< br/> openfiledialog2.filter = "text file (*. TXT) | *. TXT "; <br/> If (openfiledialog2.showdialog () = dialogresult. OK) {<br/> streamreader sreader = new streamreader (openfiledialog2.filename, encoding. default); <br/> richtextbox2.text = sreader. readtoend (); <br/> sreader. close (); <br/>}< br/> private void button3_click (Object sender, eventargs E) <br/>{< br/> savefiledialog2.filter = "text file (*. TXT) | *. TXT "; <br/> If (savefiledialog2.showdialog () = dialogresult. OK) {<br/> streamwriter swriter = new streamwriter (savefiledialog2.filename, true); <br/> swriter. write (richtextbox2.text); <br/> swriter. close (); <br/>}< br/>}

 

3. Browse folders

Path selected: object. selectedpath

Initial path: object. rootfolder

Private void button4_click (Object sender, eventargs e) <br/>{< br/> folderbrowserdialog1.rootfolder = environment. specialfolder. desktop; <br/> If (folderbrowserdialog1.showdialog () = dialogresult. OK) {<br/> textbox1.text = folderbrowserdialog1.selectedpath; <br/>}< br/>}

 

4. Color dialog box

. Allowfullopen: Allow custom colors

. Anycolor: show all colors

. Solidcoloronly: You can select a complex color.

Example:

Private void button5_click (Object sender, eventargs e) <br/>{< br/> colordialog1.allowfullopen = true; // allow custom colors <br/> colordialog1.anycolor = true; // display all colors <br/> colordialog1.solidcoloronly = false; // You can select a Complex Color <br/> If (colordialog1.showdialog () = dialogresult. OK) {<br/> If (richtextbox3.selectedtext = "") {// whether the text is selected <br/> richtextbox3.selectall (); <br/> richtextbox3.selectioncolor = colordialog1.color; <br/>}< br/>}

 

5. Font dialog box


Ii. Menu menustrip

The menustrip control is used to design the menu bar. It supports multiple document interfaces, menu merging, tooltip, overflow, and other functions.

Developers can increase menu availability and readability by adding access keys, shortcuts, selected tags, images, and splitters.

Right-click the control to insert the separator and set the icon

 

3. toolstrip

 

Iv. Status Bar control statusstrip

Generally, the statusstrip control consists of toolstripstatuslabel objects. Each object can display text, images, or simultaneously.

You can also include toolstripdropbutton, toolstripsplitbutton, and toolstripprogressbar.

Private void menuform_load (Object sender, eventargs e) <br/>{< br/> This. toolstripstatuslabel1.text = "current system date:" + datetime. now. toshortdatestring (); <br/>}


5. Custom Event message processing. Open File history in the menu

 

Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. io; <br/> namespace dialogs <br/>{< br/> Public partial class menuform: Form <br/>{< br/> Public menuform () <br/>{< br/> initializecomponent (); <br/>}< br/> private void menuform_load (Object sender, eventargs E) <br/>{< br/> file toolstripmenuitem. dropdownitems. clear (); // clear the menu items in the file <br/> This. toolstripstatuslabel1.text = "current system date:" + datetime. now. tow.datestring (); <br/> toolstripmenuitem menuitem1 = new toolstripmenuitem ("open"); // instantiate the Open menu <br/> file toolstripmenuitem. dropdownitems. insert (0, menuitem1); // insert it to the first menu position in the "file" Main Menu drop-down table <br/> menuitem1.click + = new eventhandler (Open toolstripmenuitem_click ); // specify the click event <br/> toolstripmenuitem menuitem2 = new toolstripmenuitem ("exit"); // instantiate the exit menu <br/> file toolstripmenuitem. dropdownitems. insert (1, menuitem2); // insert to file main menu item 2 <br/> streamreader sr = new streamreader ("D: // menu. ini "); // read the stream object <br/> int I = This. file toolstripmenuitem. dropdownitems. count-1; // the location of the historical file in the menu <br/> while (Sr. peek ()> = 0) {<br/> toolstripmenuitem menuitem = new toolstripmenuitem (Sr. readline (); // read a row as a menu item <br/> This. file toolstripmenuitem. dropdownitems. insert (I, menuitem); // Add history menu <br/> I ++; <br/> menuitem. click + = new eventhandler (menuitem_click); // specifies the click event <br/>}< br/> Sr. close (); // close the stream <br/>}< br/> private void open toolstripmenuitem_click (Object sender, eventargs E) {<br/> openfiledialog1.filter = "all files (*. *) | *. * "; <br/> If (openfiledialog1.showdialog () = dialogresult. OK) {<br/> streamwriter S = new streamwriter ("D: // menu. ini ", true); // historical file <br/> S. writeline (openfiledialog1.filename); // write the file name <br/> S. flush (); <br/> S. close (); <br/> system. diagnostics. process. start (openfiledialog1.filename); // open the specified file <br/>}< br/> menuform_load (sender, e ); // reload the menu <br/>}< br/> private void menuitem_click (Object sender, eventargs E) {<br/> try <br/> {<br/> toolstripmenuitem menu = (toolstripmenuitem) sender; // type conversion, obtain the click item <br/> system. diagnostics. process. start (menu. text ); // open the specified file <br/>}< br/> catch {}< br/>}</P> <p >}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.