Moving dialog control data in C#

Michael Tadyshak 80 Reputation points
2024-01-07T22:08:03.53+00:00

In MFC, we can use DoDataExchange() and UpdateData() functions to transfer dialog control data between controls and member variables. Is there an equivalent way to do this using C# and Windows Forms? Please provide examples or resources to help me accomplish this.

Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2024-01-08T07:12:41.8333333+00:00

    Hi @Michael Tadyshak , Welcome to Microsoft Q&A,

    First of all, I don't know much about MFC's DoDataExchange and UpdateData functions. After a rough query.

    The UpdateData function is used for bidirectional data transfer between dialog controls and member variables. When the user enters data and clicks the "OK" button, or when some other event occurs, UpdateData is called to ensure that the current value of the dialog box control is synchronized with the associated member variable. If the data is valid, UpdateData will update the value of the member variable;

    In C#, we usually use data binding mechanism to automatically synchronize between UI controls and data model.

    using System.Windows.Forms;
    
    namespace xxx
    {
         public partial class Form1 : Form
         {
             private MyDataModel dataModel;
             public Form1()
             {
                 InitializeComponent();
                 dataModel = new MyDataModel();
    
                 // Bind the data model to the control
                 textBox1.DataBindings.Add("Text", dataModel, "FirstName");
                 textBox2.DataBindings.Add("Text", dataModel, "LastName");
                 checkBox1.DataBindings.Add("Checked", dataModel, "IsStudent");
             }
             //Call this method where data needs to be saved
             private void SaveData()
             {
                 // Data can be saved here because the data has been automatically synchronized to dataModel
                 MessageBox.Show($"First Name: {dataModel.FirstName}, Last Name: {dataModel.LastName}, Is Student: {dataModel.IsStudent}");
             }
             private void button1_Click(object sender, System.EventArgs e)
             {
                 SaveData();
             }
         }
         public class MyDataModel
         {
             public string FirstName { get; set; }
             public string LastName { get; set; }
             public bool IsStudent { get; set; }
         }
    
    }
    

    User's image

    Best Regards,

    Jiale


    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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.