PDA

Click to See Complete Forum and Search --> : Try/Catch specfic to each textbox


birthjay
Apr 17th, 2004, 08:11 AM
Hello,

I have a form with two input boxes. I want to catch an error if either text box is empty. I do not want to use loops. I was wondering if there is a way to put these boxes in a Try/Catch and display an error message that is specific to each one. For example, if textbox1 is empty then display "textbox1 is empty" or the equilvilent system message that is generated. Same for textbox2. I am hoping to use the system generated message if there is one.

Thanks!

Hampster
Apr 18th, 2004, 06:11 AM
Why don't you want to use loops?

foreach(TextBox t in this.Controls)
if(t.Text=="")
MessageBox.Show("You need to set a value to "+t.Name+".");

birthjay
Apr 18th, 2004, 06:21 AM
Thank you, but I meant is there is a way to do it in the try/catch. For example, the try/catch will catch an error and even a spefic error such as a format exception, but will it tell you which control caused the problem? Can I set it up to tell me if textbox1 or textbox2 caused the exception?

Thanks!

Phenglai
Apr 18th, 2004, 10:02 PM
You can do what you ask, but what you ask is going to be a lot more code intensive than just checking to see if the user didn't enter anything.

You can create your own exception objects, customize them, then throw them. You can determine where the error occured, but you are going to have to write the code to give you this information.

I think Hampster is giving the best option but if you do not want to use loops, I see lots and lots of custom code to do what you are asking.

Jerel

Mongo
Apr 30th, 2004, 10:12 PM
HTH public class frmYourForm : System.Windows.Forms.Form, IMessageFilter
{
#region Variable Declarations

const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
.
.
.
#endregion


private void InitializeComponent()
{
this.TextBox1 = new System.Windows.Forms.TextBox();
this.TextBox2 = new System.Windows.Forms.TextBox();
.
.
.
//
// TextBox1
//
this.TextBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtbox_KeyDown);
this.TextBox1.TextChanged += new System.EventHandler(this.txt_TextChanged);
//
// TextBox2
//
this.TextBox2.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtbox_KeyDown);
this.TextBox2.TextChanged += new System.EventHandler(this.txt_TextChanged);
.
.
.
}


public bool PreFilterMessage( ref Message m )
{
Keys keyCode = ( Keys )( int ) m.WParam & Keys.KeyCode;
if( m.Msg == WM_KEYDOWN && keyCode == Keys.Tab )
{// Console.WriteLine( "Hit tab..." );
if ( TextBox1.Handle == m.HWnd )
{
exitTxtBox();
return true;
}
else if ( TextBox2.Handle == m.HWnd )
{
exitTxtBox();
return true;
}
}
return false;
}


private void frmYourForm_Load( object sender, System.EventArgs e )
{
try
{
Application.AddMessageFilter(this);
.
.
.
}


private void txtbox_KeyDown( object sender, System.Windows.Forms.KeyEventArgs e )
{ // Console.WriteLine( e.KeyCode );
if ( e.KeyCode == Keys.Enter ) exitTxtBox();
}


private void exitTxtBox()
{
int length = TextBox1.Text.Trim().Length + TextBox2.Text.Trim().Length;
if ( length == 0 )
txtRecordLocator.Text="Oops";
// do something useful here?
}


private void txt_TextChanged( object sender, System.EventArgs e )
{ // check key-by-key
try
{
TextBox tb = (TextBox) sender;
int start = tb.SelectionStart;
tb.Text = tb.Text.ToUpper();
tb.SelectionStart = start;
}
catch ( Exception error )
{ // Console.WriteLine( "txt_TextChanged Error: " + error.Message );
MessageBox.Show( "txt_TextChanged Error: " + error.Message );
}
}