我有一个MDIChild窗体,叫做form1的Normal form继承自MDIChild窗体和MDIParent窗体,在MDIParent form的顶部有一个工具栏在该工具栏中有一个新建按钮,当我单击新建按钮时,它会在父窗体内的工具栏下方加载Form1
form1中有一个TextBox,当我单击保存按钮时,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);
}有谁可以帮我?
发布于 2013-03-01 23:24:12
我理解您访问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;
}
}https://stackoverflow.com/questions/15159665
复制相似问题