早上好,不能关闭表格
我有几个问题,但我不确定哪个是重要问题,所以我会先说明我的整体问题。我无法关闭我的Winform应用程序。我搜索并找到了很多答案,但他们要么不工作,我不明白,或两者兼而有之。
如果我做了我所有的工作,然后调用Application.Exit,表单永远不会关闭。如果我放这个,结果相同。关闭。但是,如果我在表单上放置一个按钮并调用Application.Exit,它将关闭表单。
我显然不理解这个流程,我希望对某人清楚我正在尝试做什么。作为一名非程序员,我一直在为这个项目拼凑几个月,这是我的最后一步 - 如果从命令行运行参数运行,完成工作后关闭表单。我会尝试更长的时间来解决它,但我的Visual Studio试用期结束后这一周,所以我把专家的意见:)
谢谢 托德
的Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(String[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//add if
if (args.Length == 0)
{
Application.Run(new Form1("Form"));
}
else
{
Application.Run(new Form1(args[0]));
}
}
}
}
Form1上。 cs
using System;
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;
namespace ProgramCSToormTest
{
public partial class Form1 : Form
{
string CLArg1;
string ReturnText;
public Form1(string Arg1)
{
InitializeComponent();
if (Arg1 != null)
{
CLArg1 = Arg1;
textBox1.Text = Display(CLArg1);
//button1.Enabled = false;
}
else
{
textBox1.Text = "click button to start";
}
Application.Exit(); //This seems to be ignored
}
public void button1_Click(object sender, EventArgs e)
{
CLArg1 = null;
textBox1.Text = Display("Hello World");
Application.Exit();
}
public string Display(string DisplayText)
{
if (CLArg1 != null)
{
ReturnText = CLArg1;
return(ReturnText);
}
else
{
ReturnText = DisplayText;
return(ReturnText);
}
}
}
}
您可以下载visual studio express for c#。它的免费,并会让你继续。 :) – paqogomez 2014-10-07 15:43:52
你试图退出应用程序的构造函数将是我敢打赌,为什么它不工作。如果您要在创建表单之前删除表单,那么使用表单有什么意义。 – Hammerstein 2014-10-07 15:45:19
谢谢@paqogomez - 我必须检查一下。 – 2014-10-07 15:52:16