0
Ở bài hướng dẫn này StudyCoding đi tới làm thế nào để hiển thị các DialogBox control bằng Visual C#.NET 
Bước 1: Tạo mới project trong Visual Studio 2010 chọn Windows Form Application.  

Thiết kế project như sau:

Five DialogBox Controls



How to use DialogBox Controls C# Tutorial


Có tất cả 5 cái Dialog Controls

How to use DialogBox Controls C# Tutorial

  • ColorDialog: Double click vào Choose Color  button và chèn code.

private void btnColorDialog_Click(object sender, EventArgs e)
{
     colorDialog1.ShowDialog();
     lblColor1.BackColor = colorDialog1.Color;

}

Click  Choose Color cửa sổ mới xuất hiện cho chọn màu như sau:

How to use DialogBox Controls C# Tutorial


Chọn 1 màu và click OKMàu của label1 thay đổi thành red.   

                 
How to use DialogBox Controls C# Tutorial

  • FontDialog: Double click vào button Font Dialog và chèn code.
private void btnFontDialog_Click(object sender, EventArgs e)
{
    fontDialog1.ShowDialog();
    Font font = fontDialog1.Font;
    lblColor2.Font = font;

}

Run project Khi bạn click vào button Font Dialog và bạn sẽ thấy của sổ.

How to use DialogBox Controls C# Tutorial

Chọn font và size rồi click OK.

·         SaveFile: Viết code trong sự kiện button như sau.
    private void btnSaveFile_Click(object sender, EventArgs e)
    {
       saveFileDialog1.ShowDialog();

    }
    Bây giờ double click vào Save File Dialog Controls và chèn code:
    private void saveFileDialog1_FileOk(object sender,CancelEventArgs e)
    {
        string name = saveFileDialog1.FileName;
        File.WriteAllText(name, textBox1.Text);

    }

    How to use DialogBox Controls C# Tutorial

    Click vào Save File :

    How to use DialogBox Controls C# Tutorial

    Tên file .txt và click vào save.

    Bạn sẽ thấy file  .txt ở ngoài destop và bạn có thể mở file để kiểm tra nội dung.

    How to use DialogBox Controls C# Tutorial

    OpenFileDialog: Bạn muốn mở và lưu file, Viết code vào button Folder Browser:
    private void btnFolderBrowser_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        string file = openFileDialog1.FileName;
        string text = File.ReadAllText(file);
        textBox1.Text = text;

    }
    Run  project click vào button Folder Browser bạn sẽ thấy của sổ.

    How to use DialogBox Controls C# Tutorial


    The file you saved before “TutorialsCode.txt” select it, click on the open button and you will get the output in the textbox.

    How to use DialogBox Controls C# Tutorial

    Post a Comment

     
    Top