本文介绍了数组范围之外的索引 - 在.designer中定义数组时如何启用我的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我遇到的问题是,当我的控制代码未完成加载并尝试使用未实例化的数组时,我得到一个IndexOutOfRangeException。我想启用按钮,让它们在实例化后使用数组,但我无法弄清楚如何。由于它是一个控件,我不能使用OnLoadComplete,因为它在Form中使用。我已将表单传递给控件,​​但无法弄清楚如何处理itsParent(表单)(即protected override void OnLoadComplete并以某种方式使用itsParent)。我尝试使用protected override void OnTextChanged,但它没有被调用,因为当它被实例化时,文本没有改变,如下所示: public partial class GenericPrControl:UserControl { protected override void OnTextChanged(System.EventArgs e) { try / / 这没有被击中 { // 查看是否为空或0长度 如果(tb_Shift!= null || tb_Shift.Text.Length!= 0 ) btn_Lock_Config.Enabled = true ; // 如果定义了我们可以让他们按下按钮而没有数组越界异常 } catch (例外情况) {} } } 按下按钮后,在另一个方法中获取OutOfRangeException: if (tb_Shift!= null ) shift_char = tb_Shift。文字[ 0 ]; 如果我等到表格完成加载,我看到一些背景颜色,我知道我不会得到这个例外。我不想将按钮启用到背景颜色。 有没有人有任何想法如何在我的tb_Shift数组定义时才启用我的锁定按钮?谢谢!解决方案 好吧,不要在设计器中定义按钮(或数组中需要的控件)的数组和数组。如果您有许多类似的控件,设计师根本不会帮助您,它会迫使您进行大量繁琐的手动错误操作。使用代码要好得多。 -SA 您确定要使用||而不是&&在你的条件?在条件逻辑中,如果对象不为null但为空,则将btn_Lock_Config.Enabled属性设置为true。如果您尝试引用特定索引,则空字符串将导致错误,因为它在此行上的代码中执行tb_Shift.Text [0] ;. 试试这个... // 看看是否null而不是0长度 如果(tb_Shift!= null && ; tb_Shift.Text.Length!= 0 ) btn_Lock_Config.Enabled = true ; // 如果已定义我们可以让他们按下按钮而不会超出数组范围 您可能还想更改出错的行以检查非空字符串。像这样... if (tb_Shift!= null && tb_Shift.Text.Length > 0 ) shift_char = tb_Shift.Text [ 0 ]; 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: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: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! 解决方案 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.—SAAre 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...//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 exceptionYou may also want to change the line where you get your error to also check for a non-empty string. Like so...if (tb_Shift != null && tb_Shift.Text.Length > 0) shift_char = tb_Shift.Text[0]; 这篇关于数组范围之外的索引 - 在.designer中定义数组时如何启用我的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 05:35