代码之家  ›  专栏  ›  技术社区  ›  Dillie-O

如何将ByRef变量持久化到.net winforms对话框窗体中?

  •  3
  • Dillie-O  · 技术社区  · 17 年前

    我已经完成了将对话框的所有者传递到对话框表单并让OK button click事件进行适当更新的过程,但是这迫使我对表单类型执行DirectCast,然后我只能在当前表单上重用选择器。

    我已经能够在构造函数中使用ByRef变量并成功地更新值,但它只在构造函数中有效。如果我试图将ByRef值赋给Department Picker类中的某个内部变量,我将丢失它的引用方面。这是我的基本代码,附在我的表格上:

    Public Class DeptPicker
    
       Private m_TargetResult As String
    
       Public Sub New(ByRef TargetResult As String)
    
          InitializeComponent()
    
          ' This works just fine, my "parent" form has the reference value properly updated.
          TargetResult = "Booyah!"
    
          ' Once I leave the constructor, m_TargetResult is a simple string value that won't update the parent
          m_TargetResult = TargetResult
    
       End Sub
    
       Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    
          DialogResult = Windows.Forms.DialogResult.OK
    
          ' I get no love here. m_TargetResult is just a string and doesn't push the value back to the referenced variable I want.
          m_TargetResult = "That department I selected."
          Me.Close()
    
       End Sub
    
       Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    
          DialogResult = Windows.Forms.DialogResult.Cancel
          Me.Close()
    
       End Sub
    
    End Class
    

    有人能告诉我我在这里遗漏了什么,或者用不同的方法来实现这一点吗?

    注意:代码示例在VB.NET中,但我也会接受任何C#答案。8^D

    5 回复  |  直到 12 年前
        1
  •  4
  •   OregonGhost    17 年前

    在这种情况下,我通常

    • 编写一个ShowDialog函数,该函数执行我想要的操作(例如返回值)或
    • 让结果成为对话框中的一个属性。这是常见文件对话框在BCL中的操作方式。然后调用方必须读取该属性才能获得结果。我认为那很好。

    例如,我会将此添加为使用说明(对不起,这里没有VB,您说C#是受欢迎的):

    using (var dlg = new DeptPicker()) {
        if (dlg.ShowDialog() == DialogResult.OK) {
            myTextBoxOrWhatEver.Text = dlg.TargetResult;
        }
    }
    

    void okButton_Click(object sender, EventArgs e)
    {
        TargetResult = whatever; // can also do this when the selection changes
        DialogResult = DialogResult.OK;
        Close();
    }
    

    不过,我在这个示例中没有使用新的ShowDialog实现。

        2
  •  0
  •   MagicKat    17 年前

    至于如何制作指向原作的“指针”,我不知道。

    由于VB.NET不支持不安全的代码块,因此无法对字符串进行指针引用,这使得这一点更加困难。

        3
  •  0
  •   shahkalpesh    17 年前

    让用户选择任何部门。当用户单击“确定”时,将引用文本框的文本属性设置为所选部门的文本或id(取决于您的需要)

    我正在使用您提供的代码。

    
    Public Class DeptPicker
    
       Private m_TargetTextBox As TextBox
    
       Public Sub New(ByRef TargetTextBox As TextBox)
          InitializeComponent()
    
          m_TargetTextBox = TargetTextBox
    
       End Sub
    
       Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    
          DialogResult = Windows.Forms.DialogResult.OK
    
          ' I get no love here. m_TargetResult is just a string and doesn't push the value back to the referenced variable I want.
          m_TargetTextBox.Text = "That department I selected."
          Me.Close()
    
       End Sub
    
       Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    
          DialogResult = Windows.Forms.DialogResult.Cancel
          Me.Close()
    
       End Sub
    
    End Class
    
    
        4
  •  0
  •   osp70    17 年前
    
    Public Class DeptPicker
    
        dim dlgResult as DialogResult
    
        Public Function GetSelectedDepartment() As String
            Me.Show vbModal
            If (dlgResult = Windows.Forms.DialogResult.OK) Then
                return "selected department string here"
            Else
                return "sorry, you didnt canceled on the form"
            End If
        End Function
    
        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            dlgResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End Sub
    
        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            dlgResult = Windows.Forms.DialogResult.Cancel
            Me.Close()
        End Sub
    End Class
    
    

    主持人:这个看起来更好吗?

    用户可以调用new DeptPicker().GetSelectedDepartment()。

    谢谢主持人。现在,它看起来还好吗?

        5
  •  0
  •   Rich Rich    17 年前

    这可能会起作用:

        // This code in your dialog form.  Hide the base showdialog method 
        // and implement your own versions
        public new string ShowDialog() {
            return this.ShowDialog(null);
        }
    
        public new string ShowDialog(IWin32Window owner) {
            // Call the base implementation of show dialog
            base.ShowDialog(owner);
    
            // You get here after the close button is clicked and the form is hidden.  Capture the data you want.
            string s = this.someControl.Text;
    
            // Now really close the form and return the value
            this.Close();
            return s;
        }
    
        // On close, just hide.  Close in the show dialog method
        private void closeButton_Click(object sender, EventArgs e) {
            this.Hide();
        }
    
        // This code in your calling form
        MyCustomForm f = new MyCustomForm();
        string myAnswer = f.ShowDialog();