Results 1 to 5 of 5

Thread: Try/Catch specfic to each textbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Try/Catch specfic to each textbox

    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!

  2. #2
    Hyperactive Member Hampster's Avatar
    Join Date
    Feb 2001
    Location
    On my hamster wheel.
    Posts
    374
    Why don't you want to use loops?
    PHP Code:
        foreach(TextBox t in this.Controls)
            if(
    t.Text=="")
                
    MessageBox.Show("You need to set a value to "+t.Name+"."); 

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830
    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!

  4. #4
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262
    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

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    HTH
    Code:
    	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 );
    			}
    		}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width