我创建一个"部门选择器"的形式,是要作为一个模式弹出形式与很多我的"主要"形式的有它的应用程序。理想的用户要点击图标下的文字框中,将弹的形式,他们将选择的部门,他们的需要,并且当他们点击OK,对话将关闭,我将有价值选择为我的更新文本。

我已经做过的路线通过的所有者对话框入对话的形式和具有确定按钮击事件做了适当的更新,但这迫使我做一个DirectCast形式的类型和然后我就可以仅仅重复使用的选择器在目前的形式。

我已经能够使用一个ByRef变量的构造和功地更新价值,但它只能在构造。如果我尝试分配的ByRef值到一些内部变量的部门选择器类,我失去基准的方方面面。这就是我基本的代码连接到我的形式:

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

有帮助吗?

解决方案

在这种情况下,我通常是

  • 写ShowDialog功能,不会是我想要的(例如回值)或
  • 只是让的结果是一个属性的对话。这是怎样的共同文件的对话做到这一点,在BCL.呼叫者必须然后读取财产得到的结果。这就是现在我的意见。

你也可以结合这些方法,通过使结果值中的一个属性的对话,并创造一个ShowDialog方法,该方法返回,财产价值,无论是作为ByRef只要你想,或作为回返的价值,这取决于你的需求。

我会添加这样一个使用说明,例如(对不起,没有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实现在这个样本。

其他提示

问题是在构造函数中分配TargetResult是使用字符串作为引用。 m_TargetResult字符串只是ref字符串的副本,而不是对原始字符串的引用。

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

由于VB.NET不支持不安全的代码块,因此无法对字符串进行指针引用。

您可以将文本框引用传递给模式窗体。

让用户选择任何部门。当用户单击“确定”时,将引用的文本框的文本属性设置为所选部门的文本或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


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

注意:我没有测试过这个。我希望你能明白我的意思。

OregonGhost:这看起来更好吗?

用户可以调用新的DeptPicker()。GetSelectedDepartment()。 我不知道我不需要再次发布答案&可以使用同一个帖子。

感谢OregonGhost。现在,它看起来不错吗?

这可能有效:

    // 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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top