我正在开发一个多表单应用程序,大约有6个表单。当我想打开另一个表单并关闭当前表单时,一切正常。我只是运行这段代码:
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO;
using System.Reflection;
namespace MetroLoader
{
public partial class updater : MetroForm
{
int val = 0;
bool dir = true;
bool upd = false;
public updater()
{
InitializeComponent();
timer1.Start();
CheckUpdate();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (val == 0)
dir = true;
if (val == 100)
dir = false;
if (dir == true)
{
metroProgressSpinner1.Value = val;
val = val + 1;
}
else
{
metroProgressSpinner1.Value = val;
val = val - 1;
}
}
private void CheckUpdate()
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "x.database.windows.net";
builder.UserID = "x";
builder.Password = "x";
builder.InitialCatalog = "Sirius Accounts Database";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM Data WHERE lastver='1.3'");
String sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql, connection))
{
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
upd = false;
}
else
{
upd = true;
this.Text = "Update available!";
}
reader.Close();
connection.Close();
}
}
}
finally
{
if (upd == true)
{
metroProgressSpinner1.Visible = false;
timer1.Stop();
timer1.Enabled = false;
metroButton1.Visible = true;
label1.Visible = true;
}
else
{
timer1.Stop();
timer1.Enabled = false;
this.Hide();
var form1 = new Form1();
form1.Closed += (s, args) => this.Close();
form1.Show();
}
}
}
private void metroButton1_Click(object sender, EventArgs e)
{
object link = "www.google.ro";
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "x.database.windows.net";
builder.UserID = "x";
builder.Password = "x";
builder.InitialCatalog = "Sirius Accounts Database";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM Data WHERE this='True'");
String sql = sb.ToString();
using (SqlCommand command = new SqlCommand(sql, connection))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
link = reader["downLink"];
}
}
}
}
finally
{
System.Diagnostics.Process.Start(Convert.ToString(link));
Application.Exit();
}
}
}
}
一切都很完美。但今天,我决定改变我的主表单,即我第一次运行程序时启动的表单,然后我进入了program.cs并将其更改为新表单。完善!但现在,如果我试图关闭这个新的主要形式,并打开任何其他形式,他们只是打开这个。我很喜欢,我会关闭另一个,但如果我尝试关闭任何一个表单,整个应用程序退出。似乎其他形式以某种方式依赖于主要形式。