?
Solved

convert code from C# to VB

Posted on 2005-05-03
7
Medium Priority
?
236 Views
Last Modified: 2012-05-05
Hi,
 I'm on a tight deadline and would appreaciate if someone can convert the following code from C# to VB.

Thanks

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
KnownColor enumColor = new KnownColor();
Array Colors = Enum.GetValues(enumColor.GetType());
ArrayList ALColor = new ArrayList();
foreach(object clr in Colors)
if (!Color.FromKnownColor((KnownColor)clr).IsSystemColor)
ALColor.Add(clr.ToString());
cboColor.DataSource = ALColor;
cboColor.DataBind();
}
for (int i= 0 ;i < cboColor.Items.Count;i++)
{
cboColor.Items[i].Attributes.Add("style", "color:" + cboColor.Items[i].Text);
}

}
private void cboColor_ServerChanged(object sender, System.EventArgs e)
{
if (cboColor.SelectedIndex > -1)
strSelectedColor = cboColor.Items[cboColor.SelectedIndex].Text;
else
strSelectedColor = cboColor.Items[0].Text;
}
0
Comment
Question by:BrijBhasin
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 800 total points
ID: 13921233
     Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

            If Not IsPostBack Then

                  Dim enumColor As KnownColor = New KnownColor
                  Dim Colors As Array = enumColor.GetValues(enumColor.GetType())
                  Dim ALColor As ArrayList = New ArrayList
                  Dim clr As Object
                  For Each clr In Colors
                        If Not CType(clr, Color).IsSystemColor Then
                              ALColor.Add(clr.ToString())
                              cboColor.DataSource = ALColor
                              cboColor.DataBind()

                              Dim i As Integer
                              For i = 0 To cboColor.Items.Count - 1
                                    cboColor.Items(i).Attributes.Add("style", "color:" & cboColor.Items(i).Text)
                              Next i
                        End If
                  Next clr
      End Sub

      Private Sub cboColor_ServerChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            If (cboColor.SelectedIndex > -1) Then
                  strSelectedColor = cboColor.Items(cboColor.SelectedIndex).Text
            Else
                  strSelectedColor = cboColor.Items(0).Text
            End If
      End Sub
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 1200 total points
ID: 13921329
There isn't much difference between the two languages:
Just change the order of declarations, lose the semicolons, and change {...} to op ... end op constructs

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim enumColor As KnownColor = New KnownColor
            Dim Colors As System.Array = enumColor.GetValues(enumColor.GetType())
            Dim ALColor As ArrayList = New ArrayList
            For Each clr As Object In Colors
                If Not Color.FromKnownColor(clr).IsSystemColor Then
                    ALColor.Add(clr.ToString())
                End If
            Next

            cboColor.DataSource = ALColor
            cboColor.DataBind()
            For i As Integer = 0 To cboColor.Items.Count - 1
                cboColor.Items(i).Attributes.Add("style", "color:" + cboColor.Items(i).Text)
            Next
        End If
    End Sub

    Public strSelectedColor As String

    Private Sub cboColor_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboColor.SelectedIndexChanged
        If cboColor.SelectedIndex > -1 Then
            strSelectedColor = cboColor.Items(cboColor.SelectedIndex).Text
        Else
            strSelectedColor = cboColor.Items(0).Text
        End If
    End Sub
0
 

Author Comment

by:BrijBhasin
ID: 13921356
I'm getting an error System.InvalidCastException: Specified cast is not valid.
 on:
 If Not CType(clr, Color).IsSystemColor Then
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:allanau20
ID: 13921374
BrijBhasin,

Here's a link that will allow you to paste C# code and it will convert it to VB.NET:

http://authors.aspalliance.com/aldotnet/examples/translate.aspx

HtH.
0
 
LVL 5

Expert Comment

by:allanau20
ID: 13921388

Private Sub Page_Load(sender As Object, e As System.EventArgs)
   If Not IsPostBack Then
      Dim enumColor As New KnownColor()
      Dim Colors As Array = [Enum].GetValues(enumColor.GetType())
      Dim ALColor As New ArrayList()
      Dim clr As Object
      For Each clr In  Colors
         If Not Color.FromKnownColor(CType(clr, KnownColor)).IsSystemColor Then
            ALColor.Add(clr.ToString())
         End If
      Next clr
      cboColor.DataSource = ALColor
      cboColor.DataBind()
   End If
   Dim i As Integer
   For i = 0 To cboColor.Items.Count - 1
      cboColor.Items(i).Attributes.Add("style", "color:" + cboColor.Items(i).Text)
   Next i
End Sub 'Page_Load


Private Sub cboColor_ServerChanged(sender As Object, e As System.EventArgs)
   If cboColor.SelectedIndex > - 1 Then
      strSelectedColor = cboColor.Items(cboColor.SelectedIndex).Text
   Else
      strSelectedColor = cboColor.Items(0).Text
   End If
End Sub 'cboColor_ServerChanged
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 13921402
Why didn't you use
                 "If Not Color.FromKnownColor(clr).IsSystemColor Then"
like in the code I posted?
0
 

Author Comment

by:BrijBhasin
ID: 13921432
jensfiederer I didnt see your post when I posted my comment, it was meant for Chaosian. I'm splitting the points and giving you both credit.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question