The code can be converted by with
http://converter.telerik.com/[
^] or using the link given in solution 1 by
Manfred R. Bihy.
The converted code is as follows:
protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
for (int i = 0; i < Menu1.Items.Count; i++) {
if (i == e.Item.Value) {
Menu1.Items[i].ImageUrl = "selectedtab.gif";
} else {
Menu1.Items[i].ImageUrl = "unselectedtab.gif";
}
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false) {
Menu1.Items[0].Selected = true;
}
}
After conversion the
Menu1.Items(i)
is converted as
Menu1.Items(i)
which may be the reason for an error, as the
indexer property Items(i)
is accessed using ( ) parentheses in
VB.NET
where as in
C#
the
indexer property Items[i]
is accessed using [ ].
Accordingly the above code is corrected. The C# does not allow
Handles
keyword, hence these methods have to be assigned to the corresponding events like
Menu1.MenuItemClick += Menu1_MenuItemClick;
[Update]
The Solution is updated in response to the comment of OP below:
The error
Compiler Error Message: CS0019: Operator '==' cannot be applied to operands of type 'int' and 'string'
at the line
if (i == e.Item.Value)---------Here Error coming
is because of the reason that
i
is of
int type and <code>e.Item.Value
is
string type
which cannot be compared.
To resolve this error
e.Item.Value
can be converted to
int
using
Convert.Int32(e.Item.Value)
and then it can be compared
As seen from the code, in the
for loop ImageUrl
property of the
MenuItem
which raised the
click
event is set to
selectedtab.gif
and for the other items it is set to
unselectedtab.gif
, which can be achieved by using the
e.Item
itself as follows
without involving the overhead of conversion of e.Item.Value
foreach (var item in Menu1.Items) {
item.ImageUrl = e.Item == item ?
"selectedtab.gif" : "unselectedtab.gif";
}
instead of the following for loop
for (int i = 0; i < Menu1.Items.Count; i++) {
if (i == e.Item.Value) {
Menu1.Items[i].ImageUrl = "selectedtab.gif";
} else {
Menu1.Items[i].ImageUrl = "unselectedtab.gif";
}
}
in the above code.