Click here to Skip to main content
16,017,297 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
string InsertedID = textBoxID.Text;
            try
            {
                int IntID = Convert.ToInt32(InsertedID);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Put in a number, please!");

            }
            finally
            {

                textBoxID.Select();
                //this.Close();
                if (textBoxPlace.Text == "")
                {
                    MessageBox.Show("Please fill the Place", "Error");
                    textBoxPlace.Focus();
                }
                string InsertedPlace = textBoxPlace.Text;
            }

When the exception is caught, it should be doing the things in finally, right? But this
C#
textBoxID.Select();
isn't working, it is ignored and compile jumps to the next statement, I have no idea why... Please help. Thanks in advance! (And I'm sorry if it's a stupid question :( )
Posted
Comments
Afzaal Ahmad Zeeshan 9-Oct-14 8:41am    
How do you Select the control? Do you need to make it get the focus of the user or something like that?
Lucky Lam 9-Oct-14 8:42am    
I want to make it get focus on the textBoxID, so that user has a chance to re-type
Afzaal Ahmad Zeeshan 9-Oct-14 8:46am    
I have added the code to make the control get the focus, see my answer. I have made an edit to it. :)
Lucky Lam 9-Oct-14 8:56am    
It didn't work :( in the finaly block textBoxID.Focus(); was still ignored,
Afzaal Ahmad Zeeshan 9-Oct-14 9:01am    
What do you mean by Ignored? How would you like to have the textBox selected (focused)?

In the textBox, you need to be passing points indicating the range of the text to be selected. .Select() selects the text from the TextBox control, which is specified in your parameters.

You can try it like this

C#
textBoxID.Select(0, 25);


The above code would select only first 25 characters in the TextBox. You can add your own desired range to the method.

For more on this, read MSDN document for it. http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select(v=vs.110).aspx[^]

Since you changed the Question in comments, here is second answer

To make the control get the focus you use the following code,

C#
// set the focus to the control
textBoxID.Focus();


Read more about .Focus() function on MSDN, here: http://msdn.microsoft.com/en-us/library/system.windows.uielement.focus(v=vs.110).aspx[^]

Example source code

Always look for the easiest way of doing the stuff, in your code, you can minimize the lines of code, and write it this way

C#
try
{
    // since InsertID is not being used outside this, create it inside 
    // this block, so it gets removed from memory too
    string InsertedID = textBoxID.Text;
    // attempt to convert, if error occurs, catch it
    int IntID = Convert.ToInt32(InsertedID);
}
catch (Exception ex)
{
    // write finally code here, since you're only having one catch block
    // finally is meant to execute the code at any cost, and would be added
    // if you're having multiple catch blocks.
    MessageBox.Show("Put in a number, please!");
    // focus the textBox
    textBoxID.Focus();
    // the remaining code.
    //this.Close();
    if (textBoxPlace.Text == "")
    {
         MessageBox.Show("Please fill the Place", "Error");
         textBoxPlace.Focus();
    }
    string InsertedPlace = textBoxPlace.Text;
}
 
Share this answer
 
v4
Your code is very strange, but, in fact, it will work, and the statements in the finally clause will execute. Put a break-point in the finally clause, deliberately produce an error, and watch what happens when you execute and single-step through the code.

I'd suggest you use

Control.Focus();
Control.Capture = true;

To make sure the Control you wish to active is selected/focused, etc.

When textBoxPlace has no string content, you over-ride the Focus you set on textBoxID by setting the Focus to textBoxPlace.

I suggest you use the Int32.TryParse method to test for the presence of a valid integer; here's an idea you can experiment with:
C#
int Id;

string InsertedPlace = "";

if (Int32.TryParse(textBoxID.Text, out Id))
{
    // you have a valid Id

    if (textBoxPlace.Text != String.Empty)
    {
        // if you're here you have a valid Id, and valid place

        // your code goes here ...

        // you are finished
        return;
    }

    // you don't have a valid place

    // your code goes here ...
}
else
{
    // you don't have a valid Id

    // your code goes here ...
}
The idea of "validation" is to check for errors as soon as possible; relying on the 'finally clause of a try-catch to handle invalid input data is not a good code-practice.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900