Help Translating Code Snippet from C# to MC++
-
Since nobody responded to my previous question[^], I did some more research and thought I'd try a different approach, but the solution I found was only given in C# and VB, and I do not know how to translate this to MC++ since MC++ doesn't have the
foreach
keyword. I want to try to implement the code given at http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q1008q[^]... the link won't take you directly to the question in Firefox, so it is question 5.87 (the same snippet is given on MSDN[^] but the navigation on that page is broken). Basically, I am using a DataGrid as a display-only control, so I don't want to be able to do any editing using the DataGrid. The above-cited question has the suggestion of removing all controls from the DataGrid (except for the scrollbars):ArrayList al = new ArrayList();
foreach (Control c in this.dataGrid1.Controls) {
if (c.GetType() == typeof(VScrollBar) || c.GetType() == typeof(HScrollBar)) {
al.Add(c);
}
}
this.dataGrid1.Controls.Clear();
this.dataGrid1.Controls.AddRange((Control[]) al.ToArray(typeof(Control)));How do I translate this into Managed C++? Specifically, the
foreach
is tripping me up. I think I can implement theGetType/typeof
stuff with__try_cast
s.-- Marcus Kwok
-
Since nobody responded to my previous question[^], I did some more research and thought I'd try a different approach, but the solution I found was only given in C# and VB, and I do not know how to translate this to MC++ since MC++ doesn't have the
foreach
keyword. I want to try to implement the code given at http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q1008q[^]... the link won't take you directly to the question in Firefox, so it is question 5.87 (the same snippet is given on MSDN[^] but the navigation on that page is broken). Basically, I am using a DataGrid as a display-only control, so I don't want to be able to do any editing using the DataGrid. The above-cited question has the suggestion of removing all controls from the DataGrid (except for the scrollbars):ArrayList al = new ArrayList();
foreach (Control c in this.dataGrid1.Controls) {
if (c.GetType() == typeof(VScrollBar) || c.GetType() == typeof(HScrollBar)) {
al.Add(c);
}
}
this.dataGrid1.Controls.Clear();
this.dataGrid1.Controls.AddRange((Control[]) al.ToArray(typeof(Control)));How do I translate this into Managed C++? Specifically, the
foreach
is tripping me up. I think I can implement theGetType/typeof
stuff with__try_cast
s.-- Marcus Kwok
there is no magic involved in foreach; it expects a enumerable list of items of the correct type (it is not ignoring list elements that ar not of the type you want, they would throw!). So you can replace:
foreach (Control c in Controls) {
c.something();
}by
int n=Controls.Count;
for (int i=0; i<n; i++) {
Control c=Controls[i];
c.something();
}because in this case Controls is a Control array; you could generalize with the enumerator interface if necessary. :) -- modified at 10:21 Thursday 8th February, 2007
Luc Pattyn
-
there is no magic involved in foreach; it expects a enumerable list of items of the correct type (it is not ignoring list elements that ar not of the type you want, they would throw!). So you can replace:
foreach (Control c in Controls) {
c.something();
}by
int n=Controls.Count;
for (int i=0; i<n; i++) {
Control c=Controls[i];
c.something();
}because in this case Controls is a Control array; you could generalize with the enumerator interface if necessary. :) -- modified at 10:21 Thursday 8th February, 2007
Luc Pattyn
-
Thanks... I think your less-than sign got eaten by the HTML, but I think I got the idea. I'll give it a shot.
-- Marcus Kwok
Sorry for that, I changed it so it doesnt get eaten anymore. :)
Luc Pattyn
-
Sorry for that, I changed it so it doesnt get eaten anymore. :)
Luc Pattyn