ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Programming
    CSI-415
    Progress0 / 55 topics
    Topics
    1. Visual Programming Basics2. Introduction to Events3. Fundamentals of Event-Driven Programming4. Message Handling5. User Interfaces6. Graphics Device Interface7. Painting and Drawing8. Windows Management9. Input Devices10. Resources11. String and Menu Resource12. Dialogs and Windows Controls13. Common Controls14. Dynamic Link Libraries (DLLs)15. Threads and Synchronization16. Network Programming17. Building Class Libraries at the Command Line18. Class Libraries19. Using References20. Assemblies21. Private Assembly Deployment22. Shared Assembly Deployment23. Configuration Overview24. Configuration Files25. Programmatic Access to Configuration26. Using SDK Tools for Signing and Deployment27. Metadata28. Reflection29. Late Binding30. Directories and Files31. Serialization32. Attributes33. Memory Management and Garbage Collection34. Threading and Synchronization35. Asynchronous Delegates36. Application Domains37. Marshal by Value38. Marshal by Reference39. Authentication and Authorization40. Configuring Security41. Code Access Security42. Code Groups43. Evidence44. Permissions45. Role-Based Security46. Principals and Identities47. Using Data Readers48. Using Data Sets49. Interacting with XML Data50. Tracing Event Logs51. Using the Boolean Switch and Trace Switch Classes52. Print Debugging Information with the Debug Class53. Instrumenting Release Builds with the Trace Class54. Using Listeners55. Implementing Custom Listeners
    CSI-415›Dialogs and Windows Controls
    Advanced ProgrammingTopic 12 of 55

    Dialogs and Windows Controls

    6 minread
    948words
    Intermediatelevel

    Dialogs and Windows Controls in C#

    In C#, Dialogs and Windows Controls are fundamental concepts in creating interactive desktop applications. These are typically used in Windows Forms and WPF to allow users to interact with the application, input data, and receive feedback.

    Let's go through Dialogs and Windows Controls in detail, covering their purpose, usage, and common examples.


    1. Dialogs in C#

    Dialogs Overview

    Dialogs are specialized windows that prompt users for input or provide information. There are two main types of dialogs:

    • Modal Dialogs: Prevent interaction with other windows until the user closes the dialog.
    • Modeless Dialogs: Allow interaction with other windows while the dialog is open.

    In C#, dialogs are commonly used in Windows Forms and WPF applications, but the dialog components themselves differ based on the framework you're using.

    Common Dialogs in Windows Forms

    Windows Forms provides a set of built-in Common Dialogs, which are useful for file operations, color picking, printing, etc.

    1. MessageBox

      • The MessageBox is the simplest dialog to display messages or alerts to the user. It is modal and blocks interaction with other windows until closed.

      Example:

      MessageBox.Show("This is a simple message.", "Message Box", MessageBoxButtons.OK, MessageBoxIcon.Information);
      
      • MessageBox Buttons: OK, Cancel, Yes, No, etc.
      • MessageBox Icons: Information, Error, Warning, etc.
    2. OpenFileDialog

      • Used to allow the user to select a file from the file system.

      Example:

      OpenFileDialog openFileDialog = new OpenFileDialog();
      openFileDialog.Filter = "Text Files|*.txt|All Files|*.*";
      if (openFileDialog.ShowDialog() == DialogResult.OK)
      {
          string filePath = openFileDialog.FileName;
          // Open and process the file
      }
      
    3. SaveFileDialog

      • Allows the user to specify a location and name for saving a file.

      Example:

      SaveFileDialog saveFileDialog = new SaveFileDialog();
      saveFileDialog.Filter = "Text Files|*.txt|All Files|*.*";
      if (saveFileDialog.ShowDialog() == DialogResult.OK)
      {
          string filePath = saveFileDialog.FileName;
          // Save the file at the specified location
      }
      
    4. FolderBrowserDialog

      • Opens a dialog that allows the user to select a folder.

      Example:

      FolderBrowserDialog folderDialog = new FolderBrowserDialog();
      if (folderDialog.ShowDialog() == DialogResult.OK)
      {
          string folderPath = folderDialog.SelectedPath;
          // Do something with the selected folder
      }
      
    5. ColorDialog

      • Opens a color picker dialog to allow the user to select a color.

      Example:

      ColorDialog colorDialog = new ColorDialog();
      if (colorDialog.ShowDialog() == DialogResult.OK)
      {
          Color selectedColor = colorDialog.Color;
          // Use the selected color
      }
      
    6. FontDialog

      • Opens a dialog to allow the user to select a font.

      Example:

      FontDialog fontDialog = new FontDialog();
      if (fontDialog.ShowDialog() == DialogResult.OK)
      {
          Font selectedFont = fontDialog.Font;
          // Use the selected font
      }
      

    Dialogs in WPF

    In WPF (Windows Presentation Foundation), dialogs are typically implemented using Window classes or using the same common dialogs available in Windows Forms, which can be accessed via interoperability.

    MessageBox in WPF

    In WPF, the MessageBox class is used similarly to Windows Forms to display messages.

    MessageBox.Show("This is a message", "Message Box", MessageBoxButton.OK, MessageBoxImage.Information);
    

    Custom Dialogs in WPF

    To create a custom dialog in WPF, you can create a new Window and treat it as a dialog.

    Example:

    // Define a new window (dialog) in XAML
    <Window x:Class="MyApp.CustomDialog"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Custom Dialog" Height="200" Width="300">
        <Grid>
            <Button Content="OK" Width="75" Height="30" HorizontalAlignment="Center" VerticalAlignment="Bottom" Click="Button_Click"/>
        </Grid>
    </Window>
    
    // Code-behind for the CustomDialog.xaml.cs
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.DialogResult = true;
        this.Close();
    }
    
    // Main Window calling the dialog
    private void ShowDialogButton_Click(object sender, RoutedEventArgs e)
    {
        CustomDialog dialog = new CustomDialog();
        if (dialog.ShowDialog() == true)
        {
            MessageBox.Show("Dialog closed with OK!");
        }
    }
    

    Resources for Dialogs:

    • Windows Forms Common Dialogs: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog?view=windowsdesktop-7.0
    • WPF MessageBox: https://learn.microsoft.com/en-us/dotnet/api/system.windows.messagebox?view=windowsdesktop-7.0

    2. Windows Controls in C#

    Windows Controls Overview

    In both Windows Forms and WPF, Windows Controls are the components used to build the user interface. These controls can include buttons, labels, textboxes, list boxes, etc.

    Common Windows Controls in Windows Forms

    1. Button

      • A button allows users to perform an action by clicking on it.

      Example:

      Button btnClickMe = new Button();
      btnClickMe.Text = "Click Me";
      btnClickMe.Click += (sender, e) => MessageBox.Show("Button clicked!");
      Controls.Add(btnClickMe);
      
    2. TextBox

      • A TextBox allows users to input and edit text.

      Example:

      TextBox txtInput = new TextBox();
      txtInput.Text = "Type here";
      Controls.Add(txtInput);
      
    3. Label

      • A Label is used to display text but doesn't allow user interaction.

      Example:

      Label lblMessage = new Label();
      lblMessage.Text = "Hello, World!";
      Controls.Add(lblMessage);
      
    4. ComboBox

      • A ComboBox displays a dropdown list from which the user can select one item.

      Example:

      ComboBox comboBox = new ComboBox();
      comboBox.Items.Add("Option 1");
      comboBox.Items.Add("Option 2");
      comboBox.SelectedIndexChanged += (sender, e) => MessageBox.Show(comboBox.SelectedItem.ToString());
      Controls.Add(comboBox);
      
    5. ListBox

      • A ListBox displays a list of items. It can allow multiple selections.

      Example:

      ListBox listBox = new ListBox();
      listBox.Items.Add("Item 1");
      listBox.Items.Add("Item 2");
      listBox.SelectedIndexChanged += (sender, e) => MessageBox.Show(listBox.SelectedItem.ToString());
      Controls.Add(listBox);
      
    6. CheckBox

      • A CheckBox is used for binary choices (checked or unchecked).

      Example:

      CheckBox checkBox = new CheckBox();
      checkBox.Text = "Agree to terms";
      checkBox.CheckedChanged += (sender, e) => MessageBox.Show("Checkbox state changed!");
      Controls.Add(checkBox);
      
    7. RadioButton

      • A RadioButton allows for selecting one option from a group.

      Example:

      RadioButton radioButton = new RadioButton();
      radioButton.Text = "Option 1";
      radioButton.CheckedChanged += (sender, e) => MessageBox.Show("Radio button selected!");
      Controls.Add(radioButton);
      
    8. ProgressBar

      • A ProgressBar shows the progress of a task.

      Example:

      ProgressBar progressBar = new ProgressBar();
      progressBar.Minimum = 0;
      progressBar.Maximum = 100;
      progressBar.Value = 50;  // Example progress
      Controls.Add(progressBar);
      

    Windows Controls in WPF

    WPF provides a much more flexible system for building complex UIs. The controls are generally the same, but you also have more styling options due to XAML.

    Example:

    <Button Content="Click Me" Width="100" Height="50" Click="Button_Click"/>
    <TextBox Name="
    
    Previous topic 11
    String and Menu Resource
    Next topic 13
    Common Controls

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time6 min
      Word count948
      Code examples0
      DifficultyIntermediate