1,创建一个父窗口Form1,添加一个button(为了弹出子窗口)
2,创建一个子窗口Form2一个Combobox控件 ,一个button按钮(保存参数使用)
3,初始化显示form1,点击button弹出Form2,选择combobox中的数值,值有:0,1,2,3,4 ...,现在任意
选择其中的一个假定选择了列表中的3,点击button按钮保存并且button按钮变位禁用,再次打开Form2之后combobox中的 值仍旧为3,
同时Form2中的button仍旧为禁用状态。希望能给出一些详细的代码!谢谢!
2,创建一个子窗口Form2一个Combobox控件 ,一个button按钮(保存参数使用)
3,初始化显示form1,点击button弹出Form2,选择combobox中的数值,值有:0,1,2,3,4 ...,现在任意
选择其中的一个假定选择了列表中的3,点击button按钮保存并且button按钮变位禁用,再次打开Form2之后combobox中的 值仍旧为3,
同时Form2中的button仍旧为禁用状态。希望能给出一些详细的代码!谢谢!
下次打开 显示 就OK 了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TestDlgForm1
{
public partial class Form2 : Form
{
private Form1 pParentWin = null;
public Form2(Form1 WinMain)
{
InitializeComponent();
// m_Form1 = p_Form;
pParentWin = WinMain;
} private void Form2_Load(object sender, EventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
}
}
}
以上是部分代码,是窗口交换数据的问题,但是还是没有解决我要说明的问题!
2、把Form1的对象传入子窗口Form2中。
3、子窗口Form2中操作属性X,把Combox的值保存在属性X中。
4、同理,建立其他属性,保存子窗口数据不知道这样是否可以实现楼主的意思
父窗体代码 public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private int index = 0;
public int Index
{
set
{
index = value;
}
get
{
return index;
}
} private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Tag = this;
f2.ShowDialog();
}
}
子窗体代码 public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ } private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = this.Tag as Form1;
this.comboBox1.SelectedIndex = f1.Index;
} private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = this.Tag as Form1;
f1.Index = this.comboBox1.SelectedIndex;
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;namespace _00
{
public partial class Form4 : Form
{
String m_strReader = "";
String FILE_NAME = "FLIE_.txt";
String Path = System.Environment.CurrentDirectory.ToString();
public Form4()
{
InitializeComponent();
} private void Form4_Load(object sender, EventArgs e)
{
try
{
StreamReader srFile = new StreamReader(Path + "\\" + FILE_NAME);
while (!srFile.EndOfStream)
{
m_strReader = srFile.ReadLine().ToString().Trim(); }
srFile.Close();
this.comboBox1.Text = m_strReader; }
catch (System.Exception ex)
{
;
}
} private void OK_button_Click(object sender, EventArgs e)
{
StreamWriter sr;
if (File.Exists(FILE_NAME))
{
File.Delete(FILE_NAME);
sr = File.CreateText(FILE_NAME);
}
else
{
sr = File.CreateText(FILE_NAME);
}
sr.WriteLine(this.comboBox1.Text);
sr.Close();
this.OK_button.Enabled = false;
this.Close();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.Text.Length > 0 && !this.comboBox1.Text.Equals(m_strReader))
{
this.OK_button.Enabled = true;
}
else
{
;
}
} }
}
上面是Form2 的代码。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;namespace _00
{
public partial class Form4 : Form
{
String m_strReader = "";
String FILE_NAME = "FLIE_.txt";
String Path = System.Environment.CurrentDirectory.ToString();
public Form4()
{
InitializeComponent();
} private void Form4_Load(object sender, EventArgs e)
{
try
{
StreamReader srFile = new StreamReader(Path + "\\" + FILE_NAME);
while (!srFile.EndOfStream)
{
m_strReader = srFile.ReadLine().ToString().Trim(); }
srFile.Close();
this.comboBox1.Text = m_strReader; }
catch (System.Exception ex)
{
;
}
} private void OK_button_Click(object sender, EventArgs e)
{
StreamWriter sr;
if (File.Exists(FILE_NAME))
{
File.Delete(FILE_NAME);
sr = File.CreateText(FILE_NAME);
}
else
{
sr = File.CreateText(FILE_NAME);
}
sr.WriteLine(this.comboBox1.Text);
sr.Close();
this.OK_button.Enabled = false;
this.Close();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.Text.Length > 0 && !this.comboBox1.Text.Equals(m_strReader))
{
this.OK_button.Enabled = true;
}
else
{
;
}
} }
}
上面是Form2 的代码。
FORM1代码 设置Form1的IsMdiContainer属性true
namespace _1234
{
public partial class Form1 : Form
{
public static string ss = null;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show();
}
}
}
Form2代码
namespace _1234
{ public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(new string[]{"A","B","C","D"});
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Text = Form1.ss;
} private void button1_Click(object sender, EventArgs e)
{
Form1.ss = comboBox1.Text.Trim();
this.Close();
}
}
}
show() 的时候再调用。。