ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-415Dialogs And Windows Co…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-415

    Advanced Programming

    Progress0 / 55 done
    Topics55
    1Visual Programming Basics2Introduction to Events3Fundamentals of Event-Driven Programming4Message Handling5User Interfaces6Graphics Device Interface7Painting and Drawing8Windows Management9Input Devices10Resources11String and Menu Resource12Dialogs and Windows Controls13Common Controls14Dynamic Link Libraries (DLLs)15Threads and Synchronization16Network Programming17Building Class Libraries at the Command Line18Class Libraries19Using References20Assemblies21Private Assembly Deployment22Shared Assembly Deployment23Configuration Overview24Configuration Files25Programmatic Access to Configuration26Using SDK Tools for Signing and Deployment27Metadata28Reflection29Late Binding30Directories and Files31Serialization32Attributes33Memory Management and Garbage Collection34Threading and Synchronization35Asynchronous Delegates36Application Domains37Marshal by Value38Marshal by Reference39Authentication and Authorization40Configuring Security41Code Access Security42Code Groups43Evidence44Permissions45Role-Based Security46Principals and Identities47Using Data Readers48Using Data Sets49Interacting with XML Data50Tracing Event Logs51Using the Boolean Switch and Trace Switch Classes52Print Debugging Information with the Debug Class53Instrumenting Release Builds with the Trace Class54Using Listeners55Implementing Custom Listeners
    6 min read
    Topic 12 of 55
    Topic Study Note Illustration
    12

    Dialogs and Windows Controls

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    948words
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Programming