how to create a display form in c#

Len G 0 Reputation points
2024-02-09T16:37:35.9566667+00:00

how to create an interactive form in c#

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,708 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,601 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 47,256 Reputation points Microsoft Vendor
    2024-02-12T02:32:12.76+00:00

    Hi,@ Len G. Welcome to Microsoft Q&A. To create a display form and an interactive form in C# WPF (Windows Presentation Foundation), you'll typically use XAML for designing the user interface and C# for handling the logic and interaction. Here are examples of both a display form and an interactive form in WPF. Display Form: A display form is used to show information without allowing users to edit it. It typically contains labels, text blocks, or other controls to display data. Window1(DisplayWindow):

    <Grid>
        <Label Content="Name:" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10"/>
        <TextBlock Text="{Binding UserName}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="70,0,0,0"/>
    </Grid>
    
    
    
       public partial class Window1 : Window
       {
           public Window1(string userName)
           {
               InitializeComponent();
               DataContext = new DisplayViewModel(userName);
           }
       }
       public class DisplayViewModel
       {
           public string UserName { get; }
           public DisplayViewModel(string userName)
           {
               UserName = userName;
           }
       }
    

    Interactive Form: An interactive form allows users to input or edit data. It typically contains text boxes, combo boxes, buttons, etc., to facilitate user interaction. MainWindow:

     <Grid>
          <Label Content="Name:" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10"/>
          <TextBox Text="{Binding UserName}" VerticalAlignment="Center" HorizontalAlignment="Left" Width="100" Margin="70,0,0,0"/>
          <Button Content="Submit" Click="SubmitButton_Click" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,30,0,0"/>
         
       
      </Grid>
    
    
    
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
             DataContext = new InteractiveViewModel(); // Assign view model to provide data and handle interaction
         }
         private void SubmitButton_Click(object sender, RoutedEventArgs e)
         {
             string userName = ((InteractiveViewModel)DataContext).UserName;
             Window1 displayWindow = new Window1(userName);
             displayWindow.Show();
         }
     }
     public class InteractiveViewModel : INotifyPropertyChanged
     {
         private string _userName;
         public string UserName
         {
             get => _userName;
             set
             {
                 _userName = value;
                 OnPropertyChanged(nameof(UserName));
             }
         }
         // Add more properties for other data fields
         public event PropertyChangedEventHandler PropertyChanged;
         protected virtual void OnPropertyChanged(string propertyName)
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
     }
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.