Guest_imported
New member
- Jan 1, 1970
- 0
I am trying to write a simple static dialog box similar to Microsoft's MessageBox, but which has a textbox in it for the user to type in a string (much like JOptionPane.showInputDialog, which apparently doesn't have a counterpart in the .NET framework like most java classes do) It returns the string typed in, and null if the user clicked cancel. I have overloaded the method to also take a reference to an integer in case you want to know which button was clicked and still get the string. Here's the problem: Since it's static (it has to be in order to use it without having to instantiating a class) it can't access any global or class variables, it's stuck with whatever is within the method, and can also call external static methods. It creates an instance of the dialog form and then it needs to wait for the response from the dialog form before it can return the result. I can't get the thing to wait. I have tried a timer to check every 500 ms to see if the button has been clicked, I have tried using a while(theForm.getStatus()!=(int)status.clicked) I have tried everything and whenever I have it try to wait in such a fashion, the whole program locks up. Since it's not instantiated, it's not an object and cannot be referenced by the form as such. How does one construct a static dialog box like this? I just want to be able to call string result = InputBox("Please type in a string" and get the string they typed in the textbox.
Here's the code I have.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Timers;
using System.IO;
using System.Threading;
public class InputBox
{
enum state {notinit, waitingforclick, gotclick};//the three states the dialog box can be in
public static string Show(string question)
{
return Show(question, "Message"
}
public static string Show(string question, string title)
{
int unusedInt=0;
return Show(question, title, "OK", "CANCEL", ref unusedInt);
}
public static string Show(string question, string title, string button0, string button1, ref int result)
{
IBForm theForm;
System.Timers.Timer myTimer;
myTimer = new System.Timers.Timer();
myTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval=200;
theForm = new IBForm();
theForm.Hide();
theForm.Text=title;
theForm.setQuestion(question);
theForm.setButton(button0,0);
theForm.setButton(button1,1);
theForm.Show();
myTimer.SynchronizingObject=theForm;
myTimer.AutoReset=true;
myTimer.Enabled=true;
result = theForm.getButton();//Get which button has been clicked
return theForm.getString();
}
public static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(((IBForm)(((System.Timers.Timer)source).SynchronizingObject)).getState()==(int)state.gotclick)
((System.Timers.Timer)source).Enabled=false;
}
}
public class IBForm : System.Windows.Forms.Form
{
enum state {notinit, waitingforclick, gotclick};
public int currState = (int) state.notinit;
int buttonClicked=-1;
private System.Windows.Forms.Label lblQuestion;
private System.Windows.Forms.TextBox txtAnswer;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn1;
private System.ComponentModel.Container components = null;
public IBForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
if(components != null)
components.Dispose();
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.lblQuestion = new System.Windows.Forms.Label();
this.txtAnswer = new System.Windows.Forms.TextBox();
this.btn0 = new System.Windows.Forms.Button();
this.btn1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblQuestion
//
this.lblQuestion.Location = new System.Drawing.Point(16, 8);
this.lblQuestion.Name = "lblQuestion";
this.lblQuestion.Size = new System.Drawing.Size(200, 40);
this.lblQuestion.TabIndex = 0;
this.lblQuestion.Text = "lblQuestion";
this.lblQuestion.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtAnswer
//
this.txtAnswer.Location = new System.Drawing.Point(16, 56);
this.txtAnswer.Name = "txtAnswer";
this.txtAnswer.Size = new System.Drawing.Size(200, 20);
this.txtAnswer.TabIndex = 0;
this.txtAnswer.Text = "";
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(32, 88);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(72, 24);
this.btn0.TabIndex = 1;
this.btn0.Text = "button1";
this.btn0.Click += new System.EventHandler(this.btn0_Click);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(128, 88);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(72, 24);
this.btn1.TabIndex = 2;
this.btn1.Text = "button1";
this.btn1.Click += new System.EventHandler(this.btn1_Click);
//
// IBForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 125);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.btn1, this.btn0, this.txtAnswer, this.lblQuestion});
this.Name = "IBForm";
this.Text = "IBForm";
this.ResumeLayout(false);
}
private void btn0_Click(object sender, System.EventArgs e)
{
buttonClicked=0;
currState = (int)state.gotclick;
}
private void btn1_Click(object sender, System.EventArgs e)
{
buttonClicked=1;
currState = (int)state.gotclick;
}
public void setQuestion(string text)//sets question text
{
this.lblQuestion.Text=text;
}
public void setButton(string text, int button)//sets button text
{
if(button==0) this.btn0.Text=text;
else this.btn1.Text=text;
}
public int getState()//returns current state
{
return currState;
}
public int getButton()//returns button value
{
if(currState == (int)state.notinit)
currState = (int)state.waitingforclick;
while(currState != (int)state.gotclick);
return buttonClicked;
}
public string getString()//returns string in text box
{
return this.txtAnswer.Text;
}
}
Here's the code I have.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Timers;
using System.IO;
using System.Threading;
public class InputBox
{
enum state {notinit, waitingforclick, gotclick};//the three states the dialog box can be in
public static string Show(string question)
{
return Show(question, "Message"
}
public static string Show(string question, string title)
{
int unusedInt=0;
return Show(question, title, "OK", "CANCEL", ref unusedInt);
}
public static string Show(string question, string title, string button0, string button1, ref int result)
{
IBForm theForm;
System.Timers.Timer myTimer;
myTimer = new System.Timers.Timer();
myTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval=200;
theForm = new IBForm();
theForm.Hide();
theForm.Text=title;
theForm.setQuestion(question);
theForm.setButton(button0,0);
theForm.setButton(button1,1);
theForm.Show();
myTimer.SynchronizingObject=theForm;
myTimer.AutoReset=true;
myTimer.Enabled=true;
result = theForm.getButton();//Get which button has been clicked
return theForm.getString();
}
public static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(((IBForm)(((System.Timers.Timer)source).SynchronizingObject)).getState()==(int)state.gotclick)
((System.Timers.Timer)source).Enabled=false;
}
}
public class IBForm : System.Windows.Forms.Form
{
enum state {notinit, waitingforclick, gotclick};
public int currState = (int) state.notinit;
int buttonClicked=-1;
private System.Windows.Forms.Label lblQuestion;
private System.Windows.Forms.TextBox txtAnswer;
private System.Windows.Forms.Button btn0;
private System.Windows.Forms.Button btn1;
private System.ComponentModel.Container components = null;
public IBForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
if(components != null)
components.Dispose();
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.lblQuestion = new System.Windows.Forms.Label();
this.txtAnswer = new System.Windows.Forms.TextBox();
this.btn0 = new System.Windows.Forms.Button();
this.btn1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblQuestion
//
this.lblQuestion.Location = new System.Drawing.Point(16, 8);
this.lblQuestion.Name = "lblQuestion";
this.lblQuestion.Size = new System.Drawing.Size(200, 40);
this.lblQuestion.TabIndex = 0;
this.lblQuestion.Text = "lblQuestion";
this.lblQuestion.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtAnswer
//
this.txtAnswer.Location = new System.Drawing.Point(16, 56);
this.txtAnswer.Name = "txtAnswer";
this.txtAnswer.Size = new System.Drawing.Size(200, 20);
this.txtAnswer.TabIndex = 0;
this.txtAnswer.Text = "";
//
// btn0
//
this.btn0.Location = new System.Drawing.Point(32, 88);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(72, 24);
this.btn0.TabIndex = 1;
this.btn0.Text = "button1";
this.btn0.Click += new System.EventHandler(this.btn0_Click);
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(128, 88);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(72, 24);
this.btn1.TabIndex = 2;
this.btn1.Text = "button1";
this.btn1.Click += new System.EventHandler(this.btn1_Click);
//
// IBForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 125);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.btn1, this.btn0, this.txtAnswer, this.lblQuestion});
this.Name = "IBForm";
this.Text = "IBForm";
this.ResumeLayout(false);
}
private void btn0_Click(object sender, System.EventArgs e)
{
buttonClicked=0;
currState = (int)state.gotclick;
}
private void btn1_Click(object sender, System.EventArgs e)
{
buttonClicked=1;
currState = (int)state.gotclick;
}
public void setQuestion(string text)//sets question text
{
this.lblQuestion.Text=text;
}
public void setButton(string text, int button)//sets button text
{
if(button==0) this.btn0.Text=text;
else this.btn1.Text=text;
}
public int getState()//returns current state
{
return currState;
}
public int getButton()//returns button value
{
if(currState == (int)state.notinit)
currState = (int)state.waitingforclick;
while(currState != (int)state.gotclick);
return buttonClicked;
}
public string getString()//returns string in text box
{
return this.txtAnswer.Text;
}
}