converting VB codes to C#

T

Tammy Nejadian

Hi, Could someone please et me know how I can convert the below codes from
Visual Basic to C# using Visual studio. When each button clicked they create
list box and Radio Button. I want to convert those codes into C# doing same
action. The codes are:
Private Sub bAddListbox_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles bAddListbox.Click
Dim lb As New ListBox
lb.Height = 75
lb.Width = 70
lb.Items.Add("listbox")
tlp.Controls.Add(lb)
End Sub

Private Sub bRadiobutton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles bRadiobutton.Click
Dim iNum As Int64
iNum = InputBox("Enter number of options", "Entry", 4)
Dim rbflp As New FlowLayoutPanel
rbflp.AutoSize = True
rbflp.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
rbflp.FlowDirection = FlowDirection.LeftToRight
For i As Int16 = 1 To iNum
Dim rbl As New RadioButton
rbl.Text = "option " + CStr(i)
rbflp.Controls.Add(rbl)
Next
tlp.Controls.Add(rbflp)

End Sub
 
D

David Anton

//TODO: INSTANT C# TODO TASK: Insert the following converted event handlers
at the end of the 'InitializeComponent' method for forms, 'Page_Init' for web
pages, or into a constructor for other classes:
bAddListbox.Click += new System.EventHandler(bAddListbox_Click);
bRadiobutton.Click += new System.EventHandler(bRadiobutton_Click);

private void bAddListbox_Click(object sender, System.EventArgs e)
{
ListBox lb = new ListBox();
lb.Height = 75;
lb.Width = 70;
lb.Items.Add("listbox");
tlp.Controls.Add(lb);
}

private void bRadiobutton_Click(object sender, System.EventArgs e)
{
Int64 iNum = 0;
iNum = Microsoft.VisualBasic.Interaction.InputBox("Enter number of
options", "Entry", 4, -1, -1);
FlowLayoutPanel rbflp = new FlowLayoutPanel();
rbflp.AutoSize = true;
rbflp.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink;
rbflp.FlowDirection = FlowDirection.LeftToRight;
for (Int16 i = 1; i <= iNum; i++)
{
RadioButton rbl = new RadioButton();
rbl.Text = "option " + System.Convert.ToString(i);
rbflp.Controls.Add(rbl);
}
tlp.Controls.Add(rbflp);

}
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Instant C#: VB to C#
Instant VB: C# to VB
Java to .NET: Java to VB or C#
Instant C++: VB or C# to C++/CLI
 
P

Peter Duniho

Hi, Could someone please et me know how I can convert the below codes
from
Visual Basic to C# using Visual studio. When each button clicked they
create
list box and Radio Button.

What are you having trouble with?

The basic syntax for using .NET Framework class methods is essentially the
same in C# as for VB.NET. So I suspect that it's just getting the event
handler initialized that's the issue. If so, then you probably just need
to know that in C#, you need to add the handler explicitly (C# doesn't
have a "Handles"-like syntax). Assuming your variables that hold
references to the buttons are named "bAddListbox" and "bRadiobutton", then
the C# syntax to add the handler would look like this:

bAddlistbox.Click += bAddListbox_Click;
bRadiobutton.Click += bRadiobutton_Click;

This assumes of course that you've already written the C# versions of the
functions you posted here (minus the "Handles" part, naturally).

That code will need to go somewhere that has access to the variables,
obviously. Most commonly you'd find this sort of code in the Form-derived
class that contains the controls. In fact, the actual most common
scenario is that you don't even write this code yourself. You write the
event handler, and then use the VS Designer to select the appropriate
handler for the given control in its Click event entry in the Properties
window.

And in addition to that convenience, if you have not already written the
event handler, you can even just double-click in the event's edit area in
the Properties window, and the Designer will create a new event handler
stub in the containing Form class for you. You just fill it in with
whatever you want. In this case, that would just be the code inside the
functions you posted here.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top