How to convert C# to VB

V

Vu Doan Hung

I want to change this Class to VB.NET Class, can you help me ?

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Data;

using System.Diagnostics;



namespace DataGridDemo

{

// Derive class from DataGridTextBoxColumn

public class DataGridComboBoxColumn : DataGridTextBoxColumn

{

// Hosted ComboBox control

private ComboBox comboBox;

private CurrencyManager cm;

private int iCurrentRow;


// Constructor - create combobox, register selection change event handler,

// register lose focus event handler

public DataGridComboBoxColumn()

{

this.cm = null;

// Create ComboBox and force DropDownList style

this.comboBox = new ComboBox();

this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;

// Add event handler for notification of when ComboBox loses focus

this.comboBox.Leave += new EventHandler(comboBox_Leave);

}


// Property to provide access to ComboBox

public ComboBox ComboBox

{

get { return comboBox; }

}


// On edit, add scroll event handler, and display combo box

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)

{

Debug.WriteLine(String.Format("Edit {0}", rowNum));

base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);

if (!readOnly && cellIsVisible)

{

// Save current row in the datagrid and currency manager associated with

// the data source for the datagrid

this.iCurrentRow = rowNum;

this.cm = source;


// Add event handler for datagrid scroll notification

this.DataGridTableStyle.DataGrid.Scroll += new EventHandler(DataGrid_Scroll);

// Site the combo box control within the bounds of the current cell

this.comboBox.Parent = this.TextBox.Parent;

Rectangle rect = this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();

this.comboBox.Location = rect.Location;

this.comboBox.Size = new Size(this.TextBox.Size.Width, this.comboBox.Size.Height);

// Set combo box selection to given text

this.comboBox.SelectedIndex = this.comboBox.FindStringExact(this.TextBox.Text);

// Make the ComboBox visible and place on top text box control

this.comboBox.Show();

this.comboBox.BringToFront();

this.comboBox.Focus();

}

}

// Given a row, get the value member associated with a row. Use the value

// member to find the associated display member by iterating over bound datasource

protected override object GetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum)

{

Debug.WriteLine(String.Format("GetColumnValueAtRow {0}", rowNum));

// Given a row number in the datagrid, get the display member

object obj = base.GetColumnValueAtRow(source, rowNum);


// Iterate through the datasource bound to the ColumnComboBox

// Don't confuse this datasource with the datasource of the associated datagrid

CurrencyManager cm = (CurrencyManager)

(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);

// Assumes the associated DataGrid is bound to a DataView, or DataTable that

// implements a default DataView

DataView dataview = ((DataView)cm.List);


int i;

for (i = 0; i < dataview.Count; i++)

{

if (obj.Equals(dataview[this.comboBox.ValueMember]))

break;

}


if (i < dataview.Count)

return dataview[this.comboBox.DisplayMember];


return DBNull.Value;

}

// Given a row and a display member, iterating over bound datasource to find

// the associated value member. Set this value member.

protected override void SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum, object value)

{

Debug.WriteLine(String.Format("SetColumnValueAtRow {0} {1}", rowNum, value));

object s = value;

// Iterate through the datasource bound to the ColumnComboBox

// Don't confuse this datasource with the datasource of the associated datagrid

CurrencyManager cm = (CurrencyManager)

(this.DataGridTableStyle.DataGrid.BindingContext[this.comboBox.DataSource]);

// Assumes the associated DataGrid is bound to a DataView, or DataTable that

// implements a default DataView

DataView dataview = ((DataView)cm.List);

int i;

for (i = 0; i < dataview.Count; i++)

{

if (s.Equals(dataview[this.comboBox.DisplayMember]))

break;

}

// If set item was found return corresponding value, otherwise return DbNull.Value

if(i < dataview.Count)

s = dataview[this.comboBox.ValueMember];

else

s = DBNull.Value;


base.SetColumnValueAtRow(source, rowNum, s);

}

// On datagrid scroll, hide the combobox

private void DataGrid_Scroll(object sender, EventArgs e)

{

Debug.WriteLine("Scroll");

this.comboBox.Hide();

}

// On combo box losing focus, set the column value, hide the combo box,

// and unregister scroll event handler

private void comboBox_Leave(object sender, EventArgs e)

{

DataRowView rowView = (DataRowView) this.comboBox.SelectedItem;

string s = (string) rowView.Row[this.comboBox.DisplayMember];

Debug.WriteLine(String.Format("Leave: {0} {1}", this.comboBox.Text, s));

SetColumnValueAtRow(this.cm, this.iCurrentRow, s);

Invalidate();

this.comboBox.Hide();

this.DataGridTableStyle.DataGrid.Scroll -= new EventHandler(DataGrid_Scroll);

}

}

}
 
F

Fergus Cooney

Hi Vu Doan Hung,

Tonight you are very lucky - this much code would not normally be converted by anyone here.

I went to http://www.kamalpatel.net/ConvertCSharp2VB.aspx where there is a C# to VB.NET converter program and ran the code
through it. Then I went through it by hand and converted the bits that the program couldn't do. That means that the code below will
now compile.

Whether it will run and do something useful is entirely a different question!! That part is up to you.

Have fun. ;-)

Regards,
Fergus

================================
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Data
Imports System.Diagnostics

Namespace DataGridDemo
' Derive class from DataGridTextBoxColumn
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn

' Hosted ComboBox control
Private _ComboBox As ComboBox
Private cm As CurrencyManager
Private iCurrentRow As Integer

' Constructor - create combobox, register selection change event handler,
' register lose focus event handler
Public Sub New()
Me.cm = Nothing
' Create ComboBox and force DropDownList style
Me._ComboBox = New ComboBox()
Me._ComboBox.DropDownStyle = ComboBoxStyle.DropDownList
' Add event handler for notification of when ComboBox loses focus
AddHandler Me._ComboBox.Leave, AddressOf comboBox_Leave
End Sub

' Property to provide access to ComboBox
Public ReadOnly Property ComboBox() As ComboBox
Get
Return _ComboBox
End Get
End Property

' On edit, add scroll event handler, and display combo box
Protected Overrides Overloads Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, _
ByVal tReadOnly As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
Debug.WriteLine(String.Format("Edit {0}", rowNum))

MyBase.Edit(source, rowNum, bounds, tReadOnly, instantText, cellIsVisible)
If Not tReadOnly And cellIsVisible Then
' Save current row in the datagrid and currency manager associated with
' the data source for the datagrid
Me.iCurrentRow = rowNum
Me.cm = source

' Add event handler for datagrid scroll notification
AddHandler Me.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll

' Site the combo box control within the bounds of the current cell
Me._ComboBox.Parent = Me.TextBox.Parent
Dim rect As Rectangle = Me.DataGridTableStyle.DataGrid.GetCurrentCellBounds()
Me._ComboBox.Location = rect.Location
Me._ComboBox.Size = New Size(Me.TextBox.Size.Width, Me._ComboBox.Size.Height)

' Set combo box selection to given text
Me._ComboBox.SelectedIndex = Me._ComboBox.FindStringExact(Me.TextBox.Text)

' Make the ComboBox visible and place on top text box control
Me._ComboBox.Show()
Me._ComboBox.BringToFront()
Me._ComboBox.Focus()
End If
End Sub

' Given a row, get the value member associated with a row. Use the value
' member to find the associated display member by iterating over bound datasource
Protected Overrides Function GetColumnValueAtRow _
(ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer) As Object
Debug.WriteLine(String.Format("GetColumnValueAtRow {0}", rowNum))

' Given a row number in the datagrid, get the display member
Dim obj As Object = MyBase.GetColumnValueAtRow(source,rowNum)

' Iterate through the datasource bound to the ColumnComboBox
' Don't confuse this datasource with the datasource of the associated datagrid
Dim cm As CurrencyManager = CType((Me.DataGridTableStyle.DataGrid.BindingContext _
(Me.comboBox.DataSource)), CurrencyManager)

' Assumes the associated DataGrid is bound to a DataView, or DataTable that
' implements a default DataView
Dim dataview As DataView = (CType(cm.List, DataView))

Dim i As Integer
For i = 0 To dataview.Count- 1 Step i + 1
If obj.Equals(dataview(i)(Me.comboBox.ValueMember)) Then
Exit For
End If
Next

If i < dataview.Count Then
Return dataview(i)(Me.comboBox.DisplayMember)
End If

Return DBNull.Value
End Function

' Given a row and a display member, iterating over bound datasource to find
' the associated value member. Set this value member.
Protected Overrides Sub SetColumnValueAtRow _
(ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer, ByVal value As Object)
Debug.WriteLine(String.Format("SetColumnValueAtRow {0} {1}", rowNum, value))
Dim s As Object = value

' Iterate through the datasource bound to the ColumnComboBox
' Don't confuse this datasource with the datasource of the associated datagrid
Dim cm As CurrencyManager = CType((Me.DataGridTableStyle.DataGrid.BindingContext _
(Me.comboBox.DataSource)), CurrencyManager)

' Assumes the associated DataGrid is bound to a DataView, or DataTable that
' implements a default DataView
Dim dataview As DataView = (CType(cm.List, DataView))
Dim i As Integer
For i = 0 To dataview.Count- 1 Step i + 1
If s.Equals(dataview(i)(Me.comboBox.DisplayMember)) Then
Exit For
End If
Next

' If set item was found return corresponding value, otherwise return DbNull.Value
If i < dataview.Count Then
s = dataview(i)(Me.comboBox.ValueMember)
Else
s = DBNull.Value
End If

MyBase.SetColumnValueAtRow(source, rowNum, s)
End Sub

' On datagrid scroll, hide the combobox
Private Sub DataGrid_Scroll (ByVal sender As Object, ByVal e As EventArgs)
Debug.WriteLine("Scroll")
Me.comboBox.Hide()
End Sub

' On combo box losing focus, set the column value, hide the combo box,
' and unregister scroll event handler
Private Sub comboBox_Leave(ByVal sender As Object, ByVal e As EventArgs)
Dim rowView As DataRowView = CType(Me.comboBox.SelectedItem, DataRowView)
Dim s As String = CType(rowView.Row(Me.comboBox.DisplayMember), String)
Debug.WriteLine(String.Format("Leave: {0} {1}", Me.comboBox.Text, s))
SetColumnValueAtRow(Me.cm, Me.iCurrentRow, s)
Invalidate()
Me.comboBox.Hide()
RemoveHandler Me.DataGridTableStyle.DataGrid.Scroll, AddressOf DataGrid_Scroll
End Sub
End Class
End Namespace

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
' Conversion manually finished by Fergus Cooney.
'----------------------------------------------------------------
 

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