2012-03-09 59 views
0

我有一个窗体窗体没有在应用程序中运行它。没有应用程序处理窗体事件

这是我必须在窗体内调用的核心功能......有一些类变量唯一真正重要的是闭锁许可。用户必须按下一个按钮关闭窗口。

不幸的是我无法得到它来更新和处理事件。 this.Update()this.Refresh()不会做伎俩。

internal short ask(String question, String RegExFilter, out String answer) 
    { 
     this.RegExFilter = RegExFilter; 
     this.Text = question; 

     this.Show(); 
     while (!closingpermission) 
     { 
      //Window needs to process its events and allow the user to interact. 
     } 
     answer = answerBox.Text; 
     this.Close(); 
     return RetVal; 
    } 

编辑:

“应用程序” 是指单身申请 - 我不有。

循环必须保持窗体显示 - 我认为soution是Hans Passant写道。我需要抽取一个消息循环。

Ken2K的解决方案不适合我。

编辑#2:

这里是一个可编译示例 - 更新methos刷新我的窗口,但我不能编辑文本框 - 没关系有关按钮或下一步与文本我会做。我甚至不能编辑文本。

using System.Windows.Forms; 
using System.Drawing; //System.Drawing.dll 

namespace StackOverFlowDemo 
{ 
    class Example 
    { 
     public static void Main() 
     { 
      Input input = new Input(); 
      input.Ask("Something"); 
     } 
    } 

    class Input : Form 
    { 
     TextBox textbox = new TextBox(); 

     public Input() 
     { 
      this.Controls.AddRange(new Control[] { textbox }); 
      this.Bounds = new Rectangle(0, 0, 500, 500); 
      this.textbox.Bounds = new Rectangle(10, 10, 480, 200); 
     } 

     internal void Ask(string question) 
     { 
      this.Text = question; 
      this.Show(); 
      while (true) 
      { 
       this.Update(); 
      } 
     } 
    } 
} 

编辑#3

我想我要不能做什么。我对这个主题进行了阅读,似乎你需要“某种东西”,一直呼叫protected override void WndProc(ref Message m);。这东西似乎是应用程序。我不知道有什么方法可以在没有应用程序的应用程序中执行此操作。请不同意我:)

+0

我不明白。“没有申请”是什么意思? – Steve 2012-03-09 12:25:58

+2

显然它不起作用,因为你没有“应用程序”。需要Application.Run()调用来保持表单活着,它会泵送消息循环。或Form.ShowDialog()。 – 2012-03-09 12:34:34

+0

看看[这个问题](http://stackoverflow.com/questions/4721962/c-sharp-application-run-without-form)和在接受的答案。 – Steve 2012-03-09 13:17:34

回答

0

其中第一个评论者有它分辩从开始。

显然它不工作,因为你没有'应用程序'。需要使用Application.Run()调用来保持表单处于活动状态,它将泵送 消息循环。或Form.ShowDialog()。 - 汉斯帕桑特3月9日12:34

如果你想使用栌您还可以使用的应用程序 - 我你不想使用的应用程序,你必须切换到一些其他的框架进行可视化像XNA作为一个例子。

接受您的应用程序中存在单例并使用该应用程序。

0

简单的解决方案将使用Application.DoEvents();,但我有点困惑你的标题。

+1

'Application.DoEvents()'是一个肮脏的解决方法,通常只有**隐藏**问题,但*不能修复它们*。尽可能避免“DoEvents()”,它通常是邪恶错误的来源。 – ken2k 2012-03-09 12:30:24

+0

我有Application.DoEvents();在不同的环境中 - 但我不再拥有全局的“应用程序”,所以我无法打电话给它。 – Johannes 2012-03-09 12:41:31

1

根据我的理解,您正在尝试向用户弹出一个表单,并要求他输入一些文本。不要做无限循环,它不会工作。 UpdateRefresh也是毫无意义的。

你可以使用一个模式窗体(通常最好的弹出提示的值给用户)和FormClosingEventArgsCancel属性:

public partial class Form2 : Form 
{ 
    private bool preventClose = true; 

    public string ResultString 
    { 
     get 
     { 
      // Returns the content of a textbox 
      return this.textBox1.Text; 
     } 
    } 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // Closes only when button1 is clicked 
     this.preventClose = false; 
     this.Close(); 
    } 

    private void Form2_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     e.Cancel = this.preventClose; 
    } 
} 


using (Form2 frm = new Form2()) 
{ 
    frm.ShowDialog(); 

    string res = frm.ResultString; 
}