Lỗi không hiện lên showdialog trên visual studio 2023

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Tutorial: Add code to the picture viewer Windows Forms app in Visual Studio

  • Article
  • 03/16/2023

In this article

Applies to:

Lỗi không hiện lên showdialog trên visual studio 2023
Visual Studio
Lỗi không hiện lên showdialog trên visual studio 2023
Visual Studio for Mac
Lỗi không hiện lên showdialog trên visual studio 2023
Visual Studio Code

In this series of three tutorials, you'll create a Windows Forms application that loads a picture and displays it. The Visual Studio Integrated Design Environment (IDE) provides the tools you need to create the app. To learn more, see Welcome to the Visual Studio IDE.

Controls use C# or Visual Basic code to take the actions associated with them.

In this third tutorial, you learn how to:

  • Add event handlers for your controls
  • Write code to open a dialog box
  • Write code for the other controls
  • Run your application

Prerequisites

This tutorial builds on the previous tutorials, Create a picture viewer application and Add UI controls to the picture viewer. If you haven't done those tutorials, go through them first.

Add event handlers for your controls

In this section, add event handlers for the controls that you added in the second tutorial, Add controls to a picture viewer application. Your application calls an event handler when an action takes place, such as selecting a button.

  1. Open Visual Studio. Your Picture Viewer project appears under Open recent.
  2. In the Windows Forms Designer, double-click the Show a picture button. You can instead select the Show a picture button on the form, and then press Enter. The Visual Studio IDE opens a tab in the main window. For C#, the tab is named Form1.cs. If you're using Visual Basic, the tab is named Form1.vb. This tab displays the code file behind the form.
    Lỗi không hiện lên showdialog trên visual studio 2023
    Note Your Form1.vb tab might display showButton as ShowButton.
  3. Focus on this part of the code.

    private void showButton_Click(object sender, EventArgs e) { }

    Private Sub showButton_Click() Handles showButton.Click End Sub

    Important Use the programming language control at the top right of this page to view either the C# code snippet or the Visual Basic code snippet.
    Lỗi không hiện lên showdialog trên visual studio 2023
  4. Choose the Windows Forms Designer tab again, and then double-click the Clear the picture button to open its code. Repeat for the remaining two buttons. Each time, the Visual Studio IDE adds a new method to the form's code file.
  5. Double-click the CheckBox control in Windows Forms Designer to add a

    Private Sub showButton_Click() Handles showButton.Click End Sub

    8 method. When you select or clear the check box, it calls this method. The following snippet shows the new code that you see in the code editor.

    private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub


Methods, including event handlers, can have any name that you want. When you add an event handler with the IDE, it creates a name based on the control's name and the event being handled.

For example, the Click event for a button named showButton is called

Private Sub showButton_Click() Handles showButton.Click
End Sub

9 or

private void clearButton_Click(object sender, EventArgs e) { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

0. If you want to change a code variable name, right-click the variable in the code and then choose Refactor > Rename. All instances of that variable in the code are renamed. For more information, see Rename refactoring.

Write code to open a dialog box

The Show a picture button uses the OpenFileDialog component to display a picture file. This procedure adds the code used to call that component.

The Visual Studio IDE offers a powerful tool called IntelliSense. As you type, IntelliSense suggests possible code.

  1. In Windows Forms Designer, double-click the Show a picture button. The IDE moves your cursor inside the

    Private Sub showButton_Click() Handles showButton.Click End Sub

    9 or

    private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    0 method.
  2. Type an i on the empty line between the two braces private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    3or between

    private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    4 and

    private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    5. An IntelliSense window opens.
    Lỗi không hiện lên showdialog trên visual studio 2023
  3. The IntelliSense window should highlight the word private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    6. Select the Tab key to insert

    private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    6.
  4. Select true and then type private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    8 to overwrite it for C# or

    private void clearButton_Click(object sender, EventArgs e)

    { } private void backgroundButton_Click(object sender, EventArgs e) { } private void closeButton_Click(object sender, EventArgs e) { } private void checkBox1_CheckedChanged(object sender, EventArgs e) { }

    9 for Visual Basic.
    Lỗi không hiện lên showdialog trên visual studio 2023
    IntelliSense displays openFileDialog1.
  5. Select Tab to add openFileDialog1.
  6. Type a period ( Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

  7. or dot, right after openFileDialog1. IntelliSense provides all of the OpenFileDialog component's properties and methods. Start to type Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    1 and select Tab. The

    Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    2 method will show the Open File dialog box.
  8. Add parentheses Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    3 immediately after the "g" in

    Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    1. Your code should be

    Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    5.
  9. For C#, add a space, and then add two equal signs ( Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    6). For Visual Basic, add a space, and then use a single equal sign (

    Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    7).
  10. Add another space. Use IntelliSense to enter DialogResult.
  11. Type a dot to open the DialogResult value in the IntelliSense window. Enter the letter Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    8 and choose the Tab key to insert OK. Note The first line of code should be complete. For C#, it should be similar to the following.

    Private Sub clearButton_Click() Handles clearButton.Click

    End Sub Private Sub backgroundButton_Click() Handles backgroundButton.Click End Sub Private Sub closeButton_Click() Handles closeButton.Click End Sub Private Sub CheckBox1_CheckedChanged() Handles CheckBox1.CheckedChanged End Sub

    9 For Visual Basic, it should be the following.

    pictureBox1.Load(openFileDialog1.FileName);

    0
  12. Add the following line of code.

    pictureBox1.Load(openFileDialog1.FileName);

    PictureBox1.Load(OpenFileDialog1.FileName)

    You can copy and paste or use IntelliSense to add it. Your final

    Private Sub showButton_Click() Handles showButton.Click End Sub

    9 method should look similar to the following code.

    private void showButton_Click(object sender, EventArgs e)

    {

    if (openFileDialog1.ShowDialog() == DialogResult.OK)  
    {  
        pictureBox1.Load(openFileDialog1.FileName);  
    }  
    
    }

    Private Sub showButton_Click() Handles showButton.Click
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then  
        PictureBox1.Load(OpenFileDialog1.FileName)  
    End If  
    
    End Sub

  1. Add the following comment to your code. private void showButton_Click(object sender, EventArgs e)

    {

    // Show the Open File dialog. If the user clicks OK, load the  
    // picture that the user chose.  
    if (openFileDialog1.ShowDialog() == DialogResult.OK)  
    {  
        pictureBox1.Load(openFileDialog1.FileName);  
    }  
    
    }

    Private Sub showButton_Click() Handles showButton.Click
    ' Show the Open File dialog. If the user clicks OK, load the  
    ' picture that the user chose.  
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then  
        PictureBox1.Load(OpenFileDialog1.FileName)  
    End If  
    
    End Sub

It's the best practice to always comment your code. Code comments make it easier to understand and maintain your code in the future.

Write code for the other controls

If you run your application now, you can select Show a picture. Picture Viewer opens the Open File dialog box, where you can select a picture to display.

In this section, add the code for the other event handlers.

  1. In Windows Forms Designer, double-click the Clear the picture button. Add the code in the braces.

    Private Sub showButton_Click() Handles showButton.Click End Sub

    0

    Private Sub showButton_Click() Handles showButton.Click End Sub

    1
  2. Double-click the Set the background color button and add the code in braces.

    Private Sub showButton_Click() Handles showButton.Click End Sub

    2

    Private Sub showButton_Click() Handles showButton.Click End Sub

    3
  3. Double-click the Close button and add the code in braces.

    Private Sub showButton_Click() Handles showButton.Click End Sub

    4

    Private Sub showButton_Click() Handles showButton.Click End Sub

    5
  4. Double-click the Stretch checkbox and add the code in braces.

    Private Sub showButton_Click() Handles showButton.Click End Sub

    6

    Private Sub showButton_Click() Handles showButton.Click End Sub

    7

Run your application

You can run your application at any time, while you're writing it. After you add the code in the previous section, the Picture Viewer is complete. As in the previous tutorials, use one of the following methods to run your application:

  • Select the F5 key.
  • On the menu bar, select Debug > Start Debugging.
  • On the toolbar, select the Start button.

A window with the title Picture Viewer appears. Test all the controls.

  1. Select the Set the background color button. The Color dialog box opens.
    Lỗi không hiện lên showdialog trên visual studio 2023
  2. Choose a color to set the background color.
  3. Select Show a picture to display a picture.
    Lỗi không hiện lên showdialog trên visual studio 2023
  4. Select and unselect Stretch.
  5. Select the Clear the picture button to make sure the display clears.
  6. Select Close to exit the app.

Next steps

Congratulations! You've completed this series of tutorials. You've done these programming and design tasks in the Visual Studio IDE: