Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having an issue that when my Control code isn't done loading and tries to use an uninstantiated array, I get an IndexOutOfRangeException. I want to enable the button that will let them use the array after it's instantiated, but I can't figure out how. Since it's a control, I can't use OnLoadComplete, because that's used in a Form. I have passed the form into the control, but can't figure out what to do with itsParent (the form) (ie. "protected override void OnLoadComplete" and use itsParent somehow). I tried using "protected override void OnTextChanged", but it doesn't get called because when it's instantiated, the text isn't changing, as below:

C#
public partial class GenericPrControl : UserControl
{
        protected override void OnTextChanged(System.EventArgs e)
        {
            try //this isn't getting hit
            {
                //see if not null or 0 length
                if(tb_Shift != null || tb_Shift.Text.Length  != 0)
                   btn_Lock_Config.Enabled = true; //if its defined we can let them press button without array out of bounds exception
            }
            catch(Exception ex)
            {
            }
        }
}


It's getting the OutOfRangeException on this line in another method, after the button is pressed:

C#
if (tb_Shift != null)
                shift_char = tb_Shift.Text[0];


If I wait until the form is done loading and I see some background colors, I know I won't get this exception. I don't want to tie button enabled to the background color.

Does anyone have any ideas how to only enable my lock button when my tb_Shift array is defined? Thanks!
Posted

Well, don't define and array of buttons (or whatever controls you need to have in array) in designer. If you have many similar controls, designer does not help you at all, it forces you to do a lot of tedious manual error-prone work. Using code is much better.

—SA
 
Share this answer
 
Are you sure you want to use the || and not && in your condition? In your condition's logic you would set the btn_Lock_Config.Enabled property to true if your object wasn't null but was empty. The empty string would then cause an error if you tried to reference a specific index, as it does in your code on this line tb_Shift.Text[0];.

Try this...
C#
//see if not null AND not 0 length
    if(tb_Shift != null && tb_Shift.Text.Length  != 0)
        btn_Lock_Config.Enabled = true; //if its defined we can let them press button without array out of bounds exception

You may also want to change the line where you get your error to also check for a non-empty string. Like so...
C#
if (tb_Shift != null && tb_Shift.Text.Length > 0)
                shift_char = tb_Shift.Text[0];
 
Share this answer
 
Comments
MichCl 13-Nov-13 9:13am    
@Idenizeni - I tried using && instead, but it looks like it never gets into the OnTextChanged method when the value is assigned to the tb_Shift.Text. I added a breakpoint above the try and it doesn't get hit. I need this to get hit when it has a value upon initialization. I'm not sure how to get it to do that.

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



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