我如何 return 从显示对话框表单中获取值?

how can I return value from show dialog form?

主要形式:

fDocForm fDocForm = new fDocForm()
fDocForm.ShowDialog();

if(fDocForm.DialogResult!=null)
    //use of id returned from dialog form
else
    MessageBox.Show("null");

在对话窗体中:

private void button1_Click(object sender, EventArgs e)
{
    //insert sql command and returned id of inserted recored

    DialogResult = DialogResult.OK;
    this.Hide();
}

如何return将对话框表单中的 id 值传递给主表单?

您可以将 id 存储在 public 属性 的对话框中,并从主窗体中访问 属性。

一个选项是使用事件将数据从子表单传递到父表单,当单击子表单上的按钮时调用该事件,数据处于验证状态调用事件然后将 DialogResult 设置为 OK。

以下是一个概念性示例,其中主窗体打开一个子窗体以添加注释类型的新项目。

如果您只需要一个 property/value,这仍然可以通过更改委托签名来工作 OnAddNote

备注class

public class Note : INotifyPropertyChanged
{
    private string _title;
    private string _content;
    private int _id;

    public int Id
    {
        get => _id;
        set
        {
            _id = value;
            OnPropertyChanged();
        }
    }

    public string Title
    {
        get => _title;
        set
        {
            _title = value;
            OnPropertyChanged();
        }
    }

    public string Content
    {
        get => _content;
        set
        {
            _content = value;
            OnPropertyChanged();
        }
    }

    public override string ToString() => Title;
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

操作class

NewNote 方法在验证数据时从子窗体添加按钮调用

public class Operations
{
    public delegate void OnAddNote(Note note);
    public static event OnAddNote AddNote;

    public static List<Note> NotesList = new List<Note>();
    
    /// <summary>
    /// Pass new Note to listeners
    /// </summary>
    /// <param name="note"></param>
    public static void NewNote(Note note)
    {
        AddNote?.Invoke(note);
    }

    /// <summary>
    /// Edit note, do some validation
    /// </summary>
    /// <param name="note"></param>
    /// <returns></returns>
    public static Note EditNote(Note note)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Load mocked data, for a real application if persisting data use <see cref="LoadNotes"/>
    /// </summary>
    public static void Mocked()
    {
        NotesList.Add(new Note()
        {
            Title = "First", 
            Content = "My note"
        });
    }

    /// <summary>
    /// For a real application which persist your notes we would load from
    /// - a database
    /// - file (xml, json etc)
    /// </summary>
    /// <returns></returns>
    public static List<Note> LoadNotes()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Delete a note in <see cref="NotesList"/>
    /// </summary>
    /// <param name="note"></param>
    public static void Delete(Note note)
    {
        throw new NotImplementedException();
    }

    public static Note FindByTitle(string title)
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Save data to the data source loaded from <see cref="LoadNotes"/>
    /// </summary>
    /// <returns>
    /// Named value tuple
    /// success - operation was successful
    /// exception - if failed what was the cause
    /// </returns>
    public static (bool success, Exception exception) Save()
    {
        throw new NotImplementedException();
    }
}

子表

public partial class AddNoteForm : Form
{
    public AddNoteForm()
    {
        InitializeComponent();
    }

    private void AddButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(TitleTextBox.Text) && !string.IsNullOrWhiteSpace(NoteTextBox.Text))
        {
            Operations.NewNote(new Note() {Title = TitleTextBox.Text, Content =  NoteTextBox.Text});
            DialogResult = DialogResult.OK;
        }
        else
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}

主窗体

public partial class Form1 : Form
{
    private readonly BindingSource _bindingSource = new BindingSource();
    
    public Form1()
    {
        InitializeComponent();
        Shown += OnShown;
    }
    private void OnShown(object sender, EventArgs e)
    {
        Operations.AddNote += OperationsOnAddNote;
        
        Operations.Mocked();
        
        _bindingSource.DataSource = Operations.NotesList;
        NotesListBox.DataSource = _bindingSource;
        ContentsTextBox.DataBindings.Add("Text", _bindingSource, "Content");
    }
    private void OperationsOnAddNote(Note note)
    {
        _bindingSource.Add(note);
        NotesListBox.SelectedIndex = NotesListBox.Items.Count - 1;
    }
    private void AddButton_Click(object sender, EventArgs e)
    {
        var addForm = new AddNoteForm();
        try
        {
            addForm.ShowDialog();
        }
        finally
        {
            addForm.Dispose();
        }
    }
}

Full source