WinFrom 执行打开新窗口代码一样,结果却不一样

Diack 2010-11-19 10:01:13
就是指桌面下面的窗口栏,我点击配置按钮时,会打开多个窗口,当配置存在时,程序自动登录时却不会打开新窗口,打开新窗口的代码一样,一直找不到是什么原因

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Data.SqlClient;

namespace Vote
{
public partial class SysManage : Form
{
public SysManage()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection("server=" + textBox1.Text.Trim() + ";database=vote;uid=" + textBox2.Text.Trim() + ";pwd=" + textBox3.Text.Trim() + ";Connection Timeout=5;"))
{
try
{
connection.Open();
connection.Close();
System.Xml.XmlDocument str1 = new System.Xml.XmlDocument();  //创建一个xml文档模型
str1.Load(Application.StartupPath + @"\Config.xml");     //加载操作对象

System.Xml.XmlNode nodeServer = str1.SelectSingleNode("/Database/Server"); //获得节点
System.Xml.XmlNode nodeDatabaseName = str1.SelectSingleNode("/Database/DatabaseName");
System.Xml.XmlNode nodeUser = str1.SelectSingleNode("/Database/User");
System.Xml.XmlNode nodePassword = str1.SelectSingleNode("/Database/Password");
System.Xml.XmlNode nodeMode = str1.SelectSingleNode("/Database/mode");



nodeServer.InnerText = textBox1.Text.Trim();    // 为server节点里加文本内容
nodeDatabaseName.InnerText = "vote";
nodeUser.InnerText = textBox2.Text.Trim();
nodePassword.InnerText = textBox3.Text.Trim();
str1.Save(Application.StartupPath + @"\Config.xml");  //保存更新
//判断是否是由其他窗口打开,是则退出初始化
if (Application.OpenForms["Main"] != null || Application.OpenForms["KeyMain"] != null)
{
MessageBox.Show("配置修改需要重新启动程序才生效");
this.Close();
}
else
{
Main from = new Main();
from.MdiParent = null;
from.ShowDialog();
this.Close();
}
}
catch (System.Data.SqlClient.SqlException E)
{
MessageBox.Show("连接错误,请输入正确的连接参数");
}
finally
{
connection.Close();
}
}

}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void SysManage_Load(object sender, EventArgs e)
{

DataSet ds = new DataSet();
if (!File.Exists(Application.StartupPath + @"\Config.xml"))
{
MessageBox.Show("配置文件损坏,重新安装");
return;
}

ds.ReadXml(Application.StartupPath + @"\Config.xml"); //用dataset读取xml
string Server = ds.Tables[0].Rows[0]["Server"].ToString().Trim(); //第一行 节点为server的 文本
string aseName = ds.Tables[0].Rows[0]["DatabaseName"].ToString().Trim();
string User = ds.Tables[0].Rows[0]["User"].ToString().Trim();
string Password = ds.Tables[0].Rows[0]["Password"].ToString().Trim();
string mode = ds.Tables[0].Rows[0]["mode"].ToString().Trim();

textBox1.Text = Server;
textBox2.Text = User;
textBox3.Text = Password;
if (mode == "0")
{
radioButton1.Checked = true;
radioButton2.Checked = false;
}
else
{
radioButton1.Checked = false;
radioButton2.Checked = true;
}

using (SqlConnection connection = new SqlConnection("server=" + Server + ";database=" + aseName + ";uid=" + User + ";pwd=" + Password + ";Connection Timeout=5"))
{
try
{
connection.Open();
connection.Close();
//判断是否是由其他窗口打开,是则退出初始化
if (Application.OpenForms["Main"] != null || Application.OpenForms["KeyMain"] != null)
{
return;
}
Main from = new Main();
from.ShowDialog();
from.MdiParent = null;
this.Close();
}
catch (System.Data.SqlClient.SqlException E)
{

}
finally
{
connection.Close();
}
}
}
}
}

...全文
85 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjx198934 2010-11-19
  • 打赏
  • 举报
回复
1.from.MdiParent = null;
2.from.ShowDialog();
这个两句都应该是from.ShowDialog();先执行!

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧