首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过MDIParent表单元素访问MDIChild表单功能

通过MDIParent表单元素访问MDIChild表单功能
EN

Stack Overflow用户
提问于 2013-03-01 22:08:38
回答 1查看 1.8K关注 0票数 1

我有一个MDIChild窗体,叫做form1的Normal form继承自MDIChild窗体和MDIParent窗体,在MDIParent form的顶部有一个工具栏在该工具栏中有一个新建按钮,当我单击新建按钮时,它会在父窗体内的工具栏下方加载Form1

form1中有一个TextBox,当我单击保存按钮时,TextBox的值应该通过调用它的函数显示在MessageBox中。

但问题是我不能访问TextBox Text属性??

我的MDIParent表单代码是

代码语言:javascript
复制
 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代码中是

代码语言:javascript
复制
public void show_message()
    {
        MessageBox.Show(textBox1.Text);
    }

我的MDIChild表单代码是

代码语言:javascript
复制
 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);
    }

有谁可以帮我?

EN

回答 1

Stack Overflow用户

发布于 2013-03-01 23:24:12

我理解您访问MDIChild表单数据所做的工作。试试这个:--

代码语言:javascript
复制
 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;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15159665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档