Reputation: 6707
I have a windows form without running it in an application.
this is the core function that i have to call within the form... there are some class variables the only really important one is the closingpermission. the user has to press a button to close the window.
Unfortunately i cant get it to update and process events. this.Update()
, this.Refresh()
wont do the trick.
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;
}
EDIT:
"Application" means the singleton Application - i dont have that.
The loop has to keep the form displayed - i think the soution is something Hans Passant wrote. I need to pump a message loop.
The solution of Ken2K does not work for me.
Edit #2:
Here is a compileable Example - the update methos refreshes my window but i can not edit the textbox - nevermind about the button or what i will do next with the text. I cant even edit the text.
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();
}
}
}
}
Edit #3
I think what i want can not be done. I read up on the Topic and it seems that you need "something" that calls protected override void WndProc(ref Message m);
all the time. That something seems to be Application. I am unaware of any way to do this in an application that does not have an application. Please disagree with me :)
Upvotes: 0
Views: 331
Reputation: 6707
One of the first commenters had it rigth from the start.
Obviously it doesn't work because you don't have an 'application'. The Application.Run() call is required to keep forms alive, it pumps the message loop. Or Form.ShowDialog(). – Hans Passant Mar 9 at 12:34
If you want to use Froms you also have to use application - i you dont want to use application you have to switch to some other framework for visualisation like XNA as an example.
Accept that there is a singleton in your app and use the application.
Upvotes: 0
Reputation: 48975
From what I understand, you're trying to pop-up a Form to the user and ask him to enter some text. Don't do an infinite loop, it won't work. Update
and Refresh
are pointless too.
You could use a modal form (often preferable for a pop-up that prompts a value to the user) and the Cancel
property of the FormClosingEventArgs
:
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;
}
Upvotes: 1
Reputation: 216293
The simple solution will be to use Application.DoEvents();
but I'm a bit confused by your title.
Upvotes: 0