Windows窗体 - 在mdi parent - vb.net中将值从child1格式传递给child2格式

时间:2014-04-17 06:57:37

标签: vb.net winforms

如何在mdi父vb.net中将值从child form传递到另一个child form

child 1 code -
Dim objKey As New frmKeyboard
objKey.Show()

打开子2表格

child 2 code - 
Dim Obj As New frmSoll
Obj.strVari = txtSearch.Text
Me.Close()

2 个答案:

答案 0 :(得分:0)

尝试这样

表单2 Button_click事件

   Dim xfr As New Form3
   xfr.MdiParent = Me.MdiParent
   xfr.strVari.Text =txtSearch.Text
   xfr.Show()

Form3按钮单击

    For Each xFrm As Form In CType(Me.MdiParent, Object).MdiChildren

                If xFrm.Name = "Form2" Then
                    CType(xFrm, Object).TextBox2.Text = TextBox1.Text
                End If

   Next

   Me.Close()

答案 1 :(得分:0)

你好另一种可能性是使用eventhandler,并通过它的父节点甚至父节点将消息或值从一个孩子发送到另一个孩子。

这是我在c#中的例子(抱歉,我理解VB.NET但不说话。:))

父表:

/// Mdi parent form
public partial class ParentForm : Form
{
    private int ChildCounter = 0;

    public ParentForm()
    {
        InitializeComponent();            
        this.IsMdiContainer = true;
    }

    //Child forms will hook to this event handler
    public event MessageEventHandler MessageHandler;

    /// Sends the message.
    public void SendMessage(MessageEventArgs e)
    {
        if (MessageHandler != null) MessageHandler(e);
    }

    /// Adds children form.
    private void btnAddChildForm_Click(object sender, EventArgs e)
    {            
        var child = new ChildForm(this) { Name = string.Format("child_{0}", ChildCounter++) };
        child.Text = child.Name;
        child.Show();
    }
}

/// Event handler delegate
public delegate void MessageEventHandler(MessageEventArgs e);

/// Message argument with recipient and message
public class MessageEventArgs : EventArgs
{
    public string Message { get; set; }
    public string Recipient { get; set; }
}

儿童表格

/// Children form
public partial class ChildForm : Form
{
    //Contructor which will setup child form
    public ChildForm(ParentForm parent)
        : this()
    {
        this.MdiParent = parent;
        if (parent != null)
            parent.MessageHandler += Message_Received; // Register to receive message from handler

    }
    public ChildForm()
    {
        InitializeComponent();

    }

    /// Unregister event receiver while closing form.
    protected override void OnClosing(CancelEventArgs e)
    {
        var parent = this.MdiParent as ParentForm;
        if(parent!=null)
        {
            parent.MessageHandler -= Message_Received;
        }
        base.OnClosing(e);
    }


    /// Manage received message.
    private void Message_Received(MessageEventArgs e)
    {
        // When no ricipient all child form will show message.
        if (e.Recipient == this.Name || string.IsNullOrEmpty(e.Recipient))
        {
            txtReceivedMessage.Text = e.Message;
        }
        this.Refresh();
    }


    /// Sends message when button pressed.
    private void btnSendMessage_Click(object sender, EventArgs e)
    {
        var parent = MdiParent as ParentForm;
        if (this.MdiParent != null)
        {
            // MessageEventArgs contains information about recipient and message.
            parent.SendMessage(new MessageEventArgs() { Recipient = txtRecipientName.Text, Message = txtMessageToSend.Text });

        }
    }
}

enter image description here