【发布时间】:2012-06-13 05:04:41
【问题描述】:
在一个 Windows 应用程序中,我创建了两个表单,名为 Form1 和 Configuration_Form。首先,我正在加载Configuration_Form。在这种形式中,我检查与 txt 文件的连接。如果txt文件有一些数据,则表示会加载Form1。
否则 Configuration_Form 不会加载任何东西。
现在我有一个问题,假设txt文件有一些数据,那么这意味着它将加载Form1并打开另一个空表单。我只想显示Form1,而不是空表单。我怎样才能阻止那个空表单?
这是我的部分代码:
public partial class Configuration_Form : Form
{
Form1 form = new Form1();
public Configuration_Form()
{
StreamReader tr = new StreamReader(Application.StartupPath + "\\" + "config.txt");
string config = tr.ReadToEnd();
if (config.Replace("\r\n", string.Empty) == "")
{
tr.Close();
InitializeComponent();
}
else
{
form.Show();
}
}
///////////////////////////
private void btn_submit_Click(object sender, EventArgs e)
{
try
{
if ((txtIP.Text != "") && (txtdatabase.Text != "") && (txtuser.Text != "") && (txtpass.Text != ""))
{
StreamWriter sr = new StreamWriter(Application.StartupPath + "\\" + "config.txt");
sr.Write(Convert.ToString((txtIP.Text) + ";" + (txtport.Text) + ";" + (txtdatabase.Text) + ";" + (txtuser.Text) + ";" + (txtpass.Text)));
sr.WriteLine();
sr.Close();
this.Hide();
form.Show();
}
else
{
DialogResult msg = MessageBox.Show("All are mandatory fileds!", "SBS-BIO-CONFIG Administrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
if (Convert.ToBoolean(msg) == true)
{
this.Show();
}
}
}
catch (Exception e1)
{
MessageBox.Show("'" + e1.Message + "'");
}
}
这里是Form1 代码:
public partial class Form1 : Form
{
MySqlConnection con;
MySqlCommand cmd;
MySqlDataAdapter DA;
MySqlDataReader DR;
DataSet DSS;
#region Form_Load
public Form1()
{
InitializeComponent();
}
这是我的Program.cs 代码:
namespace BIO_PUNCH_UPDATE
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Configuration_Form());
}
}
}
请帮我解决这个错误。
【问题讨论】:
-
你在
Form_Load上加载数据吗? -
额外的空表单是什么意思?
Form1是在btn_submit_Click还是在构造函数上打开? -
btn_submit_Click 不是问题。假设 config.text 没有任何值意味着我从按钮单击事件加载数据。现在您认为 config.text 具有一些价值,首先它转到 Configuration_Form 并检查 config.text。它有一些价值,所以转到 form1 这一次打开两个窗口,一个 id Form1,另一个是空表单。我怎样才能阻止空表单..
标签: c# asp.net winforms windows-applications