VB6 to C# conversion SetIndex issue
-
I was wondering if anyone has run into this problem and gotten around it in any way. Currently I am performing a front end conversion from a VB6 GUI to C#. It is quite a big change and therefore we are performing this in stages. Now in this process we currently are using "Microsoft.VisualBasic.Compatibility.VB6" array objects at times eg.RadioButtonArray, CheckBoxArray, etc. Now when using these, there is a "SetIndex" method that is used throughout the existing code and it seems that the designer does not like the idea of this method being called when EndInit has been called already. It essentially renders the designer useless and we get an error in the designer. What we currently do is in the "InitializeComponent" method we call only "BeginInit" on the various arrays, and then in the form constructor after "InitializeComponent" is called we have another method we created called "InitializeComponentArrays" which we call all methods needing to call "SetIndex" and then we call their "EndInit" methods. This allows designer to load everything fine until we make any adjustments to the designer. When this happens, the windows generated code inserts all the "EndInit" methods into the "InitializeComponent" method which causes the designer to crash, and I then need to go in and delete the lines that it has entered. It is quite annoying and I felt it was time to reach out and see if anyone else has any better resolutions. Thanks everyone! Dan
-
I was wondering if anyone has run into this problem and gotten around it in any way. Currently I am performing a front end conversion from a VB6 GUI to C#. It is quite a big change and therefore we are performing this in stages. Now in this process we currently are using "Microsoft.VisualBasic.Compatibility.VB6" array objects at times eg.RadioButtonArray, CheckBoxArray, etc. Now when using these, there is a "SetIndex" method that is used throughout the existing code and it seems that the designer does not like the idea of this method being called when EndInit has been called already. It essentially renders the designer useless and we get an error in the designer. What we currently do is in the "InitializeComponent" method we call only "BeginInit" on the various arrays, and then in the form constructor after "InitializeComponent" is called we have another method we created called "InitializeComponentArrays" which we call all methods needing to call "SetIndex" and then we call their "EndInit" methods. This allows designer to load everything fine until we make any adjustments to the designer. When this happens, the windows generated code inserts all the "EndInit" methods into the "InitializeComponent" method which causes the designer to crash, and I then need to go in and delete the lines that it has entered. It is quite annoying and I felt it was time to reach out and see if anyone else has any better resolutions. Thanks everyone! Dan
Two suggestions: 1. don't touch the code inside InitializeComponent(), it isn't your code, it is the Designer's. And the Designer will change it any time it feels like changing it. 2. forget about SetIndex. If you need to easily access one or all of your checkboxes (or any other type of Control), create a collection yourself, by storing the references into a List<CheckBox> or something similar after your constructor called InitializeComponent(). A list accepts an integer index, so you can easily access the n-th checkbox if that is what you need. Alternatively you can enumerate all top-level Controls of a Form, and pick the one(s) you need, like so:
foreach(Control c in this.Controls) {
CheckBox cb=c as CheckBox;
if (cb!=null) {
// now cb points to one of your checkboxes
}
}:)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Two suggestions: 1. don't touch the code inside InitializeComponent(), it isn't your code, it is the Designer's. And the Designer will change it any time it feels like changing it. 2. forget about SetIndex. If you need to easily access one or all of your checkboxes (or any other type of Control), create a collection yourself, by storing the references into a List<CheckBox> or something similar after your constructor called InitializeComponent(). A list accepts an integer index, so you can easily access the n-th checkbox if that is what you need. Alternatively you can enumerate all top-level Controls of a Form, and pick the one(s) you need, like so:
foreach(Control c in this.Controls) {
CheckBox cb=c as CheckBox;
if (cb!=null) {
// now cb points to one of your checkboxes
}
}:)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
Luc Pattyn wrote:
foreach(Control c in this.Controls) { CheckBox cb=c as CheckBox; if (cb!=null) { // now cb points to one of your checkboxes } }
This is where you can have the luxury of LINQ. How about
foreach (CheckBox checkbox in from Control c in this.Controls
where c.GetType() == typeof(CheckBox)
select c as CheckBox)
{
}:)
Navaneeth How to use google | Ask smart questions
modified on Thursday, September 17, 2009 11:43 AM
-
Luc Pattyn wrote:
foreach(Control c in this.Controls) { CheckBox cb=c as CheckBox; if (cb!=null) { // now cb points to one of your checkboxes } }
This is where you can have the luxury of LINQ. How about
foreach (CheckBox checkbox in from Control c in this.Controls
where c.GetType() == typeof(CheckBox)
select c as CheckBox)
{
}:)
Navaneeth How to use google | Ask smart questions
modified on Thursday, September 17, 2009 11:43 AM
This may all be true and beautiful, but it is not what the OP needs; he is struggling with old VB code and fiddling with InitializeComponent(), so lets stick to the essentials and postpone the unnecessary magic. BTW: I don't think your and my code are equivalent, mine also matches CheckBox derivatives. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
This may all be true and beautiful, but it is not what the OP needs; he is struggling with old VB code and fiddling with InitializeComponent(), so lets stick to the essentials and postpone the unnecessary magic. BTW: I don't think your and my code are equivalent, mine also matches CheckBox derivatives. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
This may all be true and beautiful, but it is not what the OP needs; he is struggling with old VB code and fiddling with InitializeComponent(), so lets stick to the essentials and postpone the unnecessary magic. BTW: I don't think your and my code are equivalent, mine also matches CheckBox derivatives. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
My mistake. It should be
CheckBox
. Updated the post. :)Navaneeth How to use google | Ask smart questions