1

我有一个MDIChild表单,称为从表单和表单form1继承的普通表单,该工具栏顶部有一个工具栏顶部有一个新建按钮,当我单击新按钮时,它会在父表单内加载工具栏下方MDIChildMDIParentMDIParent formForm1

TextBox里面有一个form1,当我点击保存按钮时,TextBox 应该MessageBox通过调用它的函数来显示它的值,

但问题是我无法访问该TextBox Text物业?

我的 MDIParent 表单代码是

 public partial class MDIParent1 : Form
{
   // private int childFormNumber = 0;
    MdiClient mdi = null;
    string fname;



    public MDIParent1()
    {
        InitializeComponent();
        foreach (Control c in this.Controls)
        {
            if (c is MdiClient)
            {
                mdi = (MdiClient)c;
                break;
            }
        }
    }

  private void load_form(object form)
    {
        foreach (Form f in mdi.MdiChildren)
        {
            f.Close();

        }
        if (form == null)
            return;
        ((Form)form).MdiParent = this;
        ((Form)form).Show();
        ((Form)form).AutoScroll = true;



        fname = ((Form)form).Name;



    }

 private void newToolStripButton_Click(object sender, EventArgs e)
    {
        load_form(new Form1());
    }

 private void saveToolStripButton_Click(object sender, EventArgs e)
    {
        if (fname == "Form1")
        {
            Form1 f1 = new Form1();
            f1.show_message();

        }


    }

在我的 form1 代码是

public void show_message()
    {
        MessageBox.Show(textBox1.Text);
    }

我的 MDIChild 表单代码是

 public mdichild()
    {
        InitializeComponent();
        this.Load += new EventHandler(this.mdichild_Load);

    }

    private void mdichild_Load(object sender, EventArgs e)
    {
        this.ControlBox = false;
        this.WindowState = FormWindowState.Maximized;
        this.BringToFront();
        int h = Screen.PrimaryScreen.Bounds.Height;
        int w = Screen.PrimaryScreen.Bounds.Width;
        this.MinimumSize = new Size(w, h);
    }

谁能帮我?

4

1 回答 1

0

我了解您正在做什么访问 MDIChild 表单数据。尝试这个 : -

 private void ShowNewForm(object sender, EventArgs e)
    {
        Form childForm = new Form();
        childForm.MdiParent = this;
       // childForm.ContextMenuStrip = contextMenuStrip1;
        //this.contextMenuStrip1.ResumeLayout(false);
        RichTextBox text = new RichTextBox();
        text.Dock = DockStyle.Fill;
        childForm.Controls.Add(text);
        childForm.Text = "Page  " + this.MdiChildren.Count();
        childForm.Show();
    }

    private void OpenFile(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        if (openFileDialog.ShowDialog(this) == DialogResult.OK)
        {
            string FileName = openFileDialog.FileName;
        }
    }
    private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
        {
            string FileName = saveFileDialog.FileName;
        }
    }
于 2013-03-01T15:24:12.060 回答