Ambiguous in the namespace - vb.net

I've updated a project from VB6 to VB.NET. Everything seemed to work fine, but now I'm facing errors related to the namespace when trying to build. Besides, I'm getting them on Form's designers, which have been automatically generated by Visual Studio. More specifically, the errors I'm facing involve the System.Drawing namespace and its internal structures (Point, Size, SizeF, etc).
I've checked it and I haven't found any other class that could be causing the conflict. I've also tried to delete the reference to the library and add it again, and to Clean the project, but the error is still there.
It's seems a problem similar to the one asked in this question, but I've tried the solutions answered there and haven't worked either. Any ideas of what could I do? This is driving me crazy and my whole project is stopped until I can fix that.
Thank you very, very much in advance.
EDIT: Added screenshots of the error and the code that's throwing it
Complete message of one of the errors: Error 18 'Point' is ambiguous in the namespace 'System.Drawing'. C:\Users\practiquesINF\Desktop\Projecte01_VBNET_V2\Upgraded\Frm_Files.Designer.vb 152 39 P_IF01
Complete code of one of the files that throws the error (as I've said, it's the designer of one of the forms and it has been generated automatically):
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Frm_Files
#Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Frm_Files
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Frm_Files
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = CreateInstance()
m_InitializingDefInstance = False
End If
Return m_vb6FormDefInstance
End Get
Set(ByVal Value As Frm_Files)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
#Region "Windows Form Designer generated code "
Public Shared Function CreateInstance() As Frm_Files
Dim theInstance As Frm_Files = New Frm_Files()
theInstance.Form_Load()
Return theInstance
End Function
Private visualControls() As String = New String() {"components", "ToolTipMain", "_Text1_4", "DataCombo1", "_Text1_3", "_Text1_2", "_Command1_2", "_Command1_1", "_Text1_1", "_Text1_0", "_Command1_0", "CommonDialog1Open", "_Label1_6", "_Label1_5", "_Label1_4", "_Label1_3", "_Label1_2", "_Label1_1", "_Label1_0", "Command1", "Label1", "Text1"}
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Public ToolTipMain As System.Windows.Forms.ToolTip
Private WithEvents _Text1_4 As System.Windows.Forms.TextBox
Public WithEvents DataCombo1 As AxMSDataListLib.AxDataCombo
Private WithEvents _Text1_3 As System.Windows.Forms.TextBox
Private WithEvents _Text1_2 As System.Windows.Forms.TextBox
Private WithEvents _Command1_2 As System.Windows.Forms.Button
Private WithEvents _Command1_1 As System.Windows.Forms.Button
Private WithEvents _Text1_1 As System.Windows.Forms.TextBox
Private WithEvents _Text1_0 As System.Windows.Forms.TextBox
Private WithEvents _Command1_0 As System.Windows.Forms.Button
Public WithEvents CommonDialog1Open As System.Windows.Forms.OpenFileDialog
Private WithEvents _Label1_6 As System.Windows.Forms.Label
Private WithEvents _Label1_5 As System.Windows.Forms.Label
Private WithEvents _Label1_4 As System.Windows.Forms.Label
Private WithEvents _Label1_3 As System.Windows.Forms.Label
Private WithEvents _Label1_2 As System.Windows.Forms.Label
Private WithEvents _Label1_1 As System.Windows.Forms.Label
Private WithEvents _Label1_0 As System.Windows.Forms.Label
Public Command1(2) As System.Windows.Forms.Button
Public Label1(6) As System.Windows.Forms.Label
Public Text1(4) As System.Windows.Forms.TextBox
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Frm_Files))
Me.ToolTipMain = New System.Windows.Forms.ToolTip(Me.components)
Me._Text1_4 = New System.Windows.Forms.TextBox()
Me.DataCombo1 = New AxMSDataListLib.AxDataCombo()
Me._Text1_3 = New System.Windows.Forms.TextBox()
Me._Text1_2 = New System.Windows.Forms.TextBox()
Me._Command1_2 = New System.Windows.Forms.Button()
Me._Command1_1 = New System.Windows.Forms.Button()
Me._Text1_1 = New System.Windows.Forms.TextBox()
Me._Text1_0 = New System.Windows.Forms.TextBox()
Me._Command1_0 = New System.Windows.Forms.Button()
Me.CommonDialog1Open = New System.Windows.Forms.OpenFileDialog()
Me._Label1_6 = New System.Windows.Forms.Label()
Me._Label1_5 = New System.Windows.Forms.Label()
Me._Label1_4 = New System.Windows.Forms.Label()
Me._Label1_3 = New System.Windows.Forms.Label()
Me._Label1_2 = New System.Windows.Forms.Label()
Me._Label1_1 = New System.Windows.Forms.Label()
Me._Label1_0 = New System.Windows.Forms.Label()
CType(Me.DataCombo1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'_Text1_4
'
Me._Text1_4.AcceptsReturn = True
Me._Text1_4.BackColor = System.Drawing.SystemColors.Window
Me._Text1_4.Cursor = System.Windows.Forms.Cursors.IBeam
Me._Text1_4.ForeColor = System.Drawing.SystemColors.WindowText
Me._Text1_4.Location = New System.Drawing.Point(160, 200)
Me._Text1_4.MaxLength = 1
Me._Text1_4.Name = "_Text1_4"
Me._Text1_4.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Text1_4.Size = System.Drawing.Size(19, 20)
Me._Text1_4.TabIndex = 14
Me._Text1_4.Text = "S"
Me._Text1_4.Visible = False
'
'DataCombo1
'
Me.DataCombo1.Location = New System.Drawing.Point(112, 240)
Me.DataCombo1.Name = "DataCombo1"
Me.DataCombo1.OcxState = CType(resources.GetObject("DataCombo1.OcxState"), System.Windows.Forms.AxHost.State)
' Me.DataCombo1.RowSource = Nothing HI HA QUE AFEGIR UN DATA SOURCE O SINO EL PROBLEMA NO GENERA EL FORMULARI
Me.DataCombo1.Size = New System.Drawing.Size(233, 21)
Me.DataCombo1.TabIndex = 13
'
'_Text1_3
'
Me._Text1_3.AcceptsReturn = True
Me._Text1_3.BackColor = System.Drawing.SystemColors.Window
Me._Text1_3.Cursor = System.Windows.Forms.Cursors.IBeam
Me._Text1_3.ForeColor = System.Drawing.SystemColors.WindowText
Me._Text1_3.Location = New System.Drawing.Point(270, 112)
Me._Text1_3.MaxLength = 0
Me._Text1_3.Name = "_Text1_3"
Me._Text1_3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Text1_3.Size = New System.Drawing.Size(67, 20)
Me._Text1_3.TabIndex = 10
Me._Text1_3.Text = "19"
Me._Text1_3.Visible = False
'
'_Text1_2
'
Me._Text1_2.AcceptsReturn = True
Me._Text1_2.BackColor = System.Drawing.SystemColors.Window
Me._Text1_2.Cursor = System.Windows.Forms.Cursors.IBeam
Me._Text1_2.ForeColor = System.Drawing.SystemColors.WindowText
Me._Text1_2.Location = New System.Drawing.Point(113, 111)
Me._Text1_2.MaxLength = 0
Me._Text1_2.Name = "_Text1_2"
Me._Text1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Text1_2.Size = New System.Drawing.Size(43, 20)
Me._Text1_2.TabIndex = 8
Me._Text1_2.Text = "32"
Me._Text1_2.Visible = False
'
'_Command1_2
'
Me._Command1_2.BackColor = System.Drawing.SystemColors.Control
Me._Command1_2.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_2.ForeColor = System.Drawing.SystemColors.ControlText
Me._Command1_2.Location = New System.Drawing.Point(155, 272)
Me._Command1_2.Name = "_Command1_2"
Me._Command1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_2.Size = New System.Drawing.Size(78, 33)
Me._Command1_2.TabIndex = 6
Me._Command1_2.Text = "Cancel"
Me._Command1_2.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me._Command1_2.UseVisualStyleBackColor = False
'
'_Command1_1
'
Me._Command1_1.BackColor = System.Drawing.SystemColors.Control
Me._Command1_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me._Command1_1.ForeColor = System.Drawing.SystemColors.ControlText
Me._Command1_1.Location = New System.Drawing.Point(245, 266)
Me._Command1_1.Name = "_Command1_1"
Me._Command1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_1.Size = New System.Drawing.Size(97, 40)
Me._Command1_1.TabIndex = 5
Me._Command1_1.Text = "OK"
Me._Command1_1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me._Command1_1.UseVisualStyleBackColor = False
'
'_Text1_1
'
Me._Text1_1.AcceptsReturn = True
Me._Text1_1.BackColor = System.Drawing.SystemColors.Window
Me._Text1_1.Cursor = System.Windows.Forms.Cursors.IBeam
Me._Text1_1.ForeColor = System.Drawing.SystemColors.WindowText
Me._Text1_1.Location = New System.Drawing.Point(114, 82)
Me._Text1_1.MaxLength = 0
Me._Text1_1.Name = "_Text1_1"
Me._Text1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Text1_1.Size = New System.Drawing.Size(225, 20)
Me._Text1_1.TabIndex = 2
Me._Text1_1.Text = "c:\cgc\RUSTICA.TXT"
'
'_Text1_0
'
Me._Text1_0.AcceptsReturn = True
Me._Text1_0.BackColor = System.Drawing.SystemColors.Window
Me._Text1_0.Cursor = System.Windows.Forms.Cursors.IBeam
Me._Text1_0.ForeColor = System.Drawing.SystemColors.WindowText
Me._Text1_0.Location = New System.Drawing.Point(113, 53)
Me._Text1_0.MaxLength = 0
Me._Text1_0.Name = "_Text1_0"
Me._Text1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Text1_0.Size = New System.Drawing.Size(225, 20)
Me._Text1_0.TabIndex = 1
Me._Text1_0.Text = "c:\cgc\240padrus.cnt"
'
'_Command1_0
'
Me._Command1_0.BackColor = System.Drawing.SystemColors.Control
Me._Command1_0.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_0.ForeColor = System.Drawing.SystemColors.ControlText
Me._Command1_0.Location = New System.Drawing.Point(248, 7)
Me._Command1_0.Name = "_Command1_0"
Me._Command1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_0.Size = New System.Drawing.Size(91, 38)
Me._Command1_0.TabIndex = 0
Me._Command1_0.Text = "Buscar Fitxer"
Me._Command1_0.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText
Me._Command1_0.UseVisualStyleBackColor = False
'
'CommonDialog1Open
'
'
'_Label1_6
'
Me._Label1_6.BackColor = System.Drawing.SystemColors.Control
Me._Label1_6.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_6.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_6.Location = New System.Drawing.Point(8, 205)
Me._Label1_6.Name = "_Label1_6"
Me._Label1_6.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_6.Size = New System.Drawing.Size(137, 23)
Me._Label1_6.TabIndex = 15
Me._Label1_6.Text = "Firma Secretari (S/N):"
Me._Label1_6.Visible = False
'
'_Label1_5
'
Me._Label1_5.BackColor = System.Drawing.SystemColors.Control
Me._Label1_5.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_5.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_5.Location = New System.Drawing.Point(8, 248)
Me._Label1_5.Name = "_Label1_5"
Me._Label1_5.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_5.Size = New System.Drawing.Size(97, 23)
Me._Label1_5.TabIndex = 12
Me._Label1_5.Text = "Remesa:"
Me._Label1_5.Visible = False
'
'_Label1_4
'
Me._Label1_4.BackColor = System.Drawing.SystemColors.Control
Me._Label1_4.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_4.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_4.Location = New System.Drawing.Point(8, 24)
Me._Label1_4.Name = "_Label1_4"
Me._Label1_4.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_4.Size = New System.Drawing.Size(91, 19)
Me._Label1_4.TabIndex = 11
Me._Label1_4.Text = "Banc"
Me._Label1_4.Visible = False
'
'_Label1_3
'
Me._Label1_3.BackColor = System.Drawing.SystemColors.Control
Me._Label1_3.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_3.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_3.Location = New System.Drawing.Point(171, 112)
Me._Label1_3.Name = "_Label1_3"
Me._Label1_3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_3.Size = New System.Drawing.Size(99, 19)
Me._Label1_3.TabIndex = 9
Me._Label1_3.Text = "Pes de les Cartes:"
Me._Label1_3.Visible = False
'
'_Label1_2
'
Me._Label1_2.BackColor = System.Drawing.SystemColors.Control
Me._Label1_2.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_2.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_2.Location = New System.Drawing.Point(8, 116)
Me._Label1_2.Name = "_Label1_2"
Me._Label1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_2.Size = New System.Drawing.Size(97, 23)
Me._Label1_2.TabIndex = 7
Me._Label1_2.Text = "Remesa:"
Me._Label1_2.Visible = False
'
'_Label1_1
'
Me._Label1_1.BackColor = System.Drawing.SystemColors.Control
Me._Label1_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_1.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_1.Location = New System.Drawing.Point(8, 85)
Me._Label1_1.Name = "_Label1_1"
Me._Label1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_1.Size = New Drawing.Size(97, 23)
Me._Label1_1.TabIndex = 4
Me._Label1_1.Text = "Fitxer d'Eixida:"
'
'_Label1_0
'
Me._Label1_0.BackColor = System.Drawing.SystemColors.Control
Me._Label1_0.Cursor = System.Windows.Forms.Cursors.Default
Me._Label1_0.ForeColor = System.Drawing.SystemColors.ControlText
Me._Label1_0.Location = New System.Drawing.Point(8, 55)
Me._Label1_0.Name = "_Label1_0"
Me._Label1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Label1_0.Size = New System.Drawing.Size(97, 22)
Me._Label1_0.TabIndex = 3
Me._Label1_0.Text = "Fitxer d'Entrada:"
'
'Frm_Files
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(364, 324)
Me.Controls.Add(Me._Text1_4)
Me.Controls.Add(Me.DataCombo1)
Me.Controls.Add(Me._Text1_3)
Me.Controls.Add(Me._Text1_2)
Me.Controls.Add(Me._Command1_2)
Me.Controls.Add(Me._Command1_1)
Me.Controls.Add(Me._Text1_1)
Me.Controls.Add(Me._Text1_0)
Me.Controls.Add(Me._Command1_0)
Me.Controls.Add(Me._Label1_6)
Me.Controls.Add(Me._Label1_5)
Me.Controls.Add(Me._Label1_4)
Me.Controls.Add(Me._Label1_3)
Me.Controls.Add(Me._Label1_2)
Me.Controls.Add(Me._Label1_1)
Me.Controls.Add(Me._Label1_0)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Location = New System.Drawing.Point(3, 18)
Me.Name = "Frm_Files"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text = "Fitxers d'entrada i Eixida"
CType(Me.DataCombo1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Sub ReLoadForm(ByVal addEvents As Boolean)
InitializeText1()
InitializeLabel1()
InitializeCommand1()
Form_Load()
If addEvents Then
AddHandler MyBase.Closed, AddressOf Me.Frm_Files_Closed
End If
End Sub
Sub InitializeText1()
ReDim Text1(4)
Me.Text1(4) = _Text1_4
Me.Text1(3) = _Text1_3
Me.Text1(2) = _Text1_2
Me.Text1(1) = _Text1_1
Me.Text1(0) = _Text1_0
End Sub
Sub InitializeLabel1()
ReDim Label1(6)
Me.Label1(6) = _Label1_6
Me.Label1(5) = _Label1_5
Me.Label1(4) = _Label1_4
Me.Label1(3) = _Label1_3
Me.Label1(2) = _Label1_2
Me.Label1(1) = _Label1_1
Me.Label1(0) = _Label1_0
End Sub
Sub InitializeCommand1()
ReDim Command1(2)
Me.Command1(2) = _Command1_2
Me.Command1(1) = _Command1_1
Me.Command1(0) = _Command1_0
End Sub
#End Region
End Class
In case this is relevant for the question, the form was upgraded from VB6 to VB.NET using an automatic tool. Specifically, the VBUC by Mobilize.net (I do not intend to do this as spam, but to give as much information as I can. If I shouldn't have done this, I'll edit the post and delete the reference)

Finally, I've managed to solve it! It seems that one of the external libraries I was using was creating a conflict. As Visual Studio didn't tell my which one it was, I simply unreferenced them all and then referenced again only the ones that I needed at that exact moment.
The conflict seemed to be caused by one of the libraries of the GrapeCity's ActiveReport tool. As I had to upgrade the tool and it wasn't code that I had written (and it was quite poorly commented), I didn't know which ones were or will be needed, so I referenced more than necessary. And it seems that precisely one of the extra libraries (I'm not sure at all, but I think that it was one of the Chart ones) was the one causing the conflict.
Thank you all for your help! And sorry for not answering before, but I had to switch to another urgent project and I couldn't try the solutions until today.

Related

Type 'usercontrol' is not defined (Visual Studio 2015)

I have a custom winforms user control I added to a project in VS2015. When I drag the control to a form and compile, I get the error Type 'xxx' is not defined. I can fix if I remove the namespace from the control name, but any changes to the user control overwrites my changes.
Is there a way to prevent this from happening?
Here's the auto-generated control (called "ctrlTestStatus"):
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class ctrlTestStatus
Inherits System.Windows.Forms.UserControl
'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Me.prgLocalDatabase = New System.Windows.Forms.ProgressBar()
Me.prgRemoteDatabase = New System.Windows.Forms.ProgressBar()
Me.lblLocalDatabaseStatus = New System.Windows.Forms.Label()
Me.lblRemoteDatabaseStatus = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.prgAmazon = New System.Windows.Forms.ProgressBar()
Me.Label6 = New System.Windows.Forms.Label()
Me.lblAmazonStatus = New System.Windows.Forms.Label()
Me.prgEbay = New System.Windows.Forms.ProgressBar()
Me.lblEbayStatus = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.lbStatus = New System.Windows.Forms.ListBox()
Me.TableLayoutPanel1.SuspendLayout()
Me.SuspendLayout()
'
'TableLayoutPanel1
'
Me.TableLayoutPanel1.BackColor = System.Drawing.SystemColors.Window
Me.TableLayoutPanel1.ColumnCount = 3
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25.30766!))
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 58.95527!))
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.73707!))
Me.TableLayoutPanel1.Controls.Add(Me.prgLocalDatabase, 1, 0)
Me.TableLayoutPanel1.Controls.Add(Me.prgRemoteDatabase, 1, 1)
Me.TableLayoutPanel1.Controls.Add(Me.lblLocalDatabaseStatus, 2, 0)
Me.TableLayoutPanel1.Controls.Add(Me.lblRemoteDatabaseStatus, 2, 1)
Me.TableLayoutPanel1.Controls.Add(Me.Label4, 0, 0)
Me.TableLayoutPanel1.Controls.Add(Me.Label5, 0, 1)
Me.TableLayoutPanel1.Controls.Add(Me.prgAmazon, 1, 2)
Me.TableLayoutPanel1.Controls.Add(Me.Label6, 0, 2)
Me.TableLayoutPanel1.Controls.Add(Me.lblAmazonStatus, 2, 2)
Me.TableLayoutPanel1.Controls.Add(Me.prgEbay, 1, 3)
Me.TableLayoutPanel1.Controls.Add(Me.lblEbayStatus, 2, 3)
Me.TableLayoutPanel1.Controls.Add(Me.Label1, 0, 3)
Me.TableLayoutPanel1.Location = New System.Drawing.Point(8, 7)
Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
Me.TableLayoutPanel1.RowCount = 4
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle())
Me.TableLayoutPanel1.Size = New System.Drawing.Size(425, 107)
Me.TableLayoutPanel1.TabIndex = 8
'
'prgLocalDatabase
'
Me.prgLocalDatabase.Location = New System.Drawing.Point(110, 3)
Me.prgLocalDatabase.Name = "prgLocalDatabase"
Me.prgLocalDatabase.Size = New System.Drawing.Size(244, 20)
Me.prgLocalDatabase.TabIndex = 0
'
'prgRemoteDatabase
'
Me.prgRemoteDatabase.Location = New System.Drawing.Point(110, 29)
Me.prgRemoteDatabase.Name = "prgRemoteDatabase"
Me.prgRemoteDatabase.Size = New System.Drawing.Size(244, 20)
Me.prgRemoteDatabase.TabIndex = 1
'
'lblLocalDatabaseStatus
'
Me.lblLocalDatabaseStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.lblLocalDatabaseStatus.AutoSize = True
Me.lblLocalDatabaseStatus.Location = New System.Drawing.Point(360, 6)
Me.lblLocalDatabaseStatus.MinimumSize = New System.Drawing.Size(40, 13)
Me.lblLocalDatabaseStatus.Name = "lblLocalDatabaseStatus"
Me.lblLocalDatabaseStatus.Size = New System.Drawing.Size(40, 13)
Me.lblLocalDatabaseStatus.TabIndex = 3
'
'lblRemoteDatabaseStatus
'
Me.lblRemoteDatabaseStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.lblRemoteDatabaseStatus.AutoSize = True
Me.lblRemoteDatabaseStatus.Location = New System.Drawing.Point(360, 32)
Me.lblRemoteDatabaseStatus.MinimumSize = New System.Drawing.Size(40, 13)
Me.lblRemoteDatabaseStatus.Name = "lblRemoteDatabaseStatus"
Me.lblRemoteDatabaseStatus.Size = New System.Drawing.Size(40, 13)
Me.lblRemoteDatabaseStatus.TabIndex = 4
'
'Label4
'
Me.Label4.Anchor = System.Windows.Forms.AnchorStyles.Right
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(4, 6)
Me.Label4.MinimumSize = New System.Drawing.Size(100, 13)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(100, 13)
Me.Label4.TabIndex = 6
Me.Label4.Text = "Local Database:"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'Label5
'
Me.Label5.Anchor = System.Windows.Forms.AnchorStyles.Right
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(4, 32)
Me.Label5.MinimumSize = New System.Drawing.Size(100, 13)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(100, 13)
Me.Label5.TabIndex = 7
Me.Label5.Text = "Remote Database:"
Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'prgAmazon
'
Me.prgAmazon.Location = New System.Drawing.Point(110, 55)
Me.prgAmazon.Name = "prgAmazon"
Me.prgAmazon.Size = New System.Drawing.Size(244, 20)
Me.prgAmazon.TabIndex = 9
'
'Label6
'
Me.Label6.Anchor = System.Windows.Forms.AnchorStyles.Right
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(4, 58)
Me.Label6.MinimumSize = New System.Drawing.Size(100, 13)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(100, 13)
Me.Label6.TabIndex = 8
Me.Label6.Text = "Amazon:"
Me.Label6.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lblAmazonStatus
'
Me.lblAmazonStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.lblAmazonStatus.AutoSize = True
Me.lblAmazonStatus.Location = New System.Drawing.Point(360, 58)
Me.lblAmazonStatus.MinimumSize = New System.Drawing.Size(40, 13)
Me.lblAmazonStatus.Name = "lblAmazonStatus"
Me.lblAmazonStatus.Size = New System.Drawing.Size(40, 13)
Me.lblAmazonStatus.TabIndex = 5
'
'prgEbay
'
Me.prgEbay.Location = New System.Drawing.Point(110, 81)
Me.prgEbay.Name = "prgEbay"
Me.prgEbay.Size = New System.Drawing.Size(244, 20)
Me.prgEbay.TabIndex = 11
'
'lblEbayStatus
'
Me.lblEbayStatus.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.lblEbayStatus.AutoSize = True
Me.lblEbayStatus.Location = New System.Drawing.Point(360, 86)
Me.lblEbayStatus.MinimumSize = New System.Drawing.Size(40, 13)
Me.lblEbayStatus.Name = "lblEbayStatus"
Me.lblEbayStatus.Size = New System.Drawing.Size(40, 13)
Me.lblEbayStatus.TabIndex = 12
'
'Label1
'
Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.Right
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(4, 86)
Me.Label1.MinimumSize = New System.Drawing.Size(100, 13)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(100, 13)
Me.Label1.TabIndex = 10
Me.Label1.Text = "Ebay:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopRight
'
'lbStatus
'
Me.lbStatus.FormattingEnabled = True
Me.lbStatus.Location = New System.Drawing.Point(7, 120)
Me.lbStatus.Name = "lbStatus"
Me.lbStatus.Size = New System.Drawing.Size(425, 56)
Me.lbStatus.TabIndex = 9
'
'ctrlTestStatus
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Controls.Add(Me.TableLayoutPanel1)
Me.Controls.Add(Me.lbStatus)
Me.Name = "ctrlTestStatus"
Me.Size = New System.Drawing.Size(440, 185)
Me.TableLayoutPanel1.ResumeLayout(False)
Me.TableLayoutPanel1.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents TableLayoutPanel1 As TableLayoutPanel
Friend WithEvents prgLocalDatabase As ProgressBar
Friend WithEvents prgRemoteDatabase As ProgressBar
Friend WithEvents lblLocalDatabaseStatus As Label
Friend WithEvents lblRemoteDatabaseStatus As Label
Friend WithEvents Label4 As Label
Friend WithEvents Label5 As Label
Friend WithEvents prgAmazon As ProgressBar
Friend WithEvents Label6 As Label
Friend WithEvents lblAmazonStatus As Label
Friend WithEvents prgEbay As ProgressBar
Friend WithEvents lblEbayStatus As Label
Friend WithEvents Label1 As Label
Friend WithEvents lbStatus As ListBox
End Class
and the designer code for the form I plop the control onto (before I put it in):
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class frmTestStart
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'frmTestStart
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(468, 206)
Me.Name = "frmTestStart"
Me.Text = "Test Connections"
Me.ResumeLayout(False)
End Sub
End Class
What the designer generated after I dragged the control and saved:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class frmTestStart
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.CtrlTestStatus1 = New QBI.ctrlTestStatus()
Me.SuspendLayout()
'
'CtrlTestStatus1
'
Me.CtrlTestStatus1.Location = New System.Drawing.Point(13, 13)
Me.CtrlTestStatus1.Name = "CtrlTestStatus1"
Me.CtrlTestStatus1.Size = New System.Drawing.Size(440, 185)
Me.CtrlTestStatus1.TabIndex = 0
'
'frmTestStart
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(468, 206)
Me.Controls.Add(Me.CtrlTestStatus1)
Me.Name = "frmTestStart"
Me.Text = "Test Connections"
Me.ResumeLayout(False)
End Sub
Friend WithEvents CtrlTestStatus1 As QBI.ctrlTestStatus
End Class
I get the squiggly lines under the QBI.ctrlTestStatus() and it generates the error if I try to compile. I thought it was a namespace bug in the designer, so I immediately wrapped the usercontrol to fix (no joy).The project namespace is QBI (it's not a reserved word as far as I can tell).
Namespace QBI
Public Class ctrlTestStatus
Private Sub ctrlTestStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
End Namespace
Unfortunately, this created more problems and the properties and events for the CtrlTestStatus1 instance are not available (I get squiggly lines).
I found one problem from MS saying to set the compile to Any CPU (which the project build has always been set to this) due to a 64bit / 32bit issue (VS is STILL 32bit!), but no joy either.
Any ideas (settings, things I'm doing wrong) as to how to remedy this?
Old post, but for anyone else having this issue...
I had this happen in a Project where I'd defined a sub-namespace with the same name as my project (root namespace) (in the OP's case, "QBI"). This happens when you have code that looks like this somewhere in your project:
Namespace [Name of your Project/Root Namespace]
[...]
End Namespace
The result of this is that your project contains both a "QBI" namespace AND a "QBI.QBI" namespace (which you can see for yourself if you look at the Object Browser for your project). This situation causes a resolution conflict in the designer file whereby it looks under the wrong namespace for your custom control.
A few possible ways to fix this:
Where the sub-namespace is declared (as in the example code above), remove the namespace declaration altogether. This will move all the code within the declaration to your root "QBI" namespace. You can do this if having that code in a sub-namespace is unnecessary.
Where the sub-namespace is declared, right-click the Namespace name and rename it to something OTHER than your Root Namespace's name.
If you want to keep the sub-namespace named as such, you can instead rename your root namespace to something unique, from My Project --> Application --> Root namespace.
Option 1 is the simplest option and should immediately resolve the error.
Option 2 is the next simplest, but, for example, if your renamed the sub-namespace to "NewName", all references to "ctrlTestStatus" in your Designer files will be mistakenly renamed to "NewName.ctrlTestStatus", and you'll have to repair them to "QBI.ctrlTestStatus".
Option 3 is the most tedious, because you have to resolve all Designer file references to the Root namespace, using Find and Replace, being careful not to mistakenly replace legitimate references to the sub-QBI namespace.

.Net Windows Form Arabic Encoding issue at Runtime

I created a VB.net windows forms project under VS2008 and Windows Vista, now when I take to code to other PC having VS2015 and Windows 10 I faced an encoding problem, the Application has Arabic captions in it, Visual Studio is able to read those captions but when I click start (F5) to compile and run the app I see that all Arabic text are not encoded. knowing that when I took the compiled version of the app from old pc and run it in the new pc it behave as expected and all Arabic texts are displayed well, so the problem here is on compile time of VS2015 under Windows 10.
I searched around this issue but all advises was to try to open the files with code editor and use the compatible encoding, but actually I don't have problem reading the code content, the problem is when compiling the code it will not encode the Arabic text.
The following is the code of the frmWelcome.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmWelcome
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmWelcome))
Me.btnSalesCenter = New System.Windows.Forms.Button
Me.btnAdministrator = New System.Windows.Forms.Button
Me.btnPurchaseCenter = New System.Windows.Forms.Button
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.btnMinimize = New System.Windows.Forms.PictureBox
Me.Label5 = New System.Windows.Forms.Label
Me.btnClose = New System.Windows.Forms.PictureBox
Me.Label3 = New System.Windows.Forms.Label
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.btnMinimize, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.btnClose, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'btnSalesCenter
'
Me.btnSalesCenter.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnSalesCenter.Font = New System.Drawing.Font("Times New Roman", 20.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnSalesCenter.ForeColor = System.Drawing.Color.Navy
Me.btnSalesCenter.Location = New System.Drawing.Point(408, 276)
Me.btnSalesCenter.Name = "btnSalesCenter"
Me.btnSalesCenter.Size = New System.Drawing.Size(262, 70)
Me.btnSalesCenter.TabIndex = 1
Me.btnSalesCenter.TabStop = False
Me.btnSalesCenter.Text = "قسم المبيعات"
Me.btnSalesCenter.UseVisualStyleBackColor = True
'
'btnAdministrator
'
Me.btnAdministrator.BackColor = System.Drawing.Color.White
Me.btnAdministrator.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnAdministrator.Font = New System.Drawing.Font("Times New Roman", 20.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnAdministrator.ForeColor = System.Drawing.Color.Navy
Me.btnAdministrator.Location = New System.Drawing.Point(408, 200)
Me.btnAdministrator.Name = "btnAdministrator"
Me.btnAdministrator.Size = New System.Drawing.Size(262, 70)
Me.btnAdministrator.TabIndex = 0
Me.btnAdministrator.TabStop = False
Me.btnAdministrator.Text = "الادارة"
Me.btnAdministrator.UseVisualStyleBackColor = False
'
'btnPurchaseCenter
'
Me.btnPurchaseCenter.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnPurchaseCenter.Font = New System.Drawing.Font("Times New Roman", 20.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnPurchaseCenter.ForeColor = System.Drawing.Color.Navy
Me.btnPurchaseCenter.Location = New System.Drawing.Point(408, 352)
Me.btnPurchaseCenter.Name = "btnPurchaseCenter"
Me.btnPurchaseCenter.Size = New System.Drawing.Size(262, 70)
Me.btnPurchaseCenter.TabIndex = 21
Me.btnPurchaseCenter.TabStop = False
Me.btnPurchaseCenter.Tag = ""
Me.btnPurchaseCenter.Text = "قسم المشتريات"
Me.btnPurchaseCenter.UseVisualStyleBackColor = True
'
'PictureBox1
'
Me.PictureBox1.Cursor = System.Windows.Forms.Cursors.Hand
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
Me.PictureBox1.Location = New System.Drawing.Point(78, 240)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(146, 138)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage
Me.PictureBox1.TabIndex = 151
Me.PictureBox1.TabStop = False
'
'btnMinimize
'
Me.btnMinimize.BackColor = System.Drawing.Color.Navy
Me.btnMinimize.Cursor = System.Windows.Forms.Cursors.Hand
Me.btnMinimize.Image = CType(resources.GetObject("btnMinimize.Image"), System.Drawing.Image)
Me.btnMinimize.Location = New System.Drawing.Point(676, 5)
Me.btnMinimize.Name = "btnMinimize"
Me.btnMinimize.Size = New System.Drawing.Size(50, 50)
Me.btnMinimize.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.btnMinimize.TabIndex = 231
Me.btnMinimize.TabStop = False
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.BackColor = System.Drawing.Color.Navy
Me.Label5.Font = New System.Drawing.Font("Times New Roman", 27.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.ForeColor = System.Drawing.Color.White
Me.Label5.Location = New System.Drawing.Point(464, 9)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(206, 42)
Me.Label5.TabIndex = 230
Me.Label5.Text = "القائمة الرئيسية"
'
'btnClose
'
Me.btnClose.BackColor = System.Drawing.Color.Navy
Me.btnClose.Cursor = System.Windows.Forms.Cursors.Hand
Me.btnClose.Image = CType(resources.GetObject("btnClose.Image"), System.Drawing.Image)
Me.btnClose.Location = New System.Drawing.Point(732, 5)
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(50, 50)
Me.btnClose.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
Me.btnClose.TabIndex = 229
Me.btnClose.TabStop = False
'
'Label3
'
Me.Label3.BackColor = System.Drawing.Color.Navy
Me.Label3.Font = New System.Drawing.Font("Times New Roman", 27.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.ForeColor = System.Drawing.Color.Navy
Me.Label3.Location = New System.Drawing.Point(0, -3)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(800, 67)
Me.Label3.TabIndex = 228
'
'frmWelcome
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(794, 568)
Me.ControlBox = False
Me.Controls.Add(Me.btnMinimize)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.btnClose)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.btnPurchaseCenter)
Me.Controls.Add(Me.btnSalesCenter)
Me.Controls.Add(Me.btnAdministrator)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "frmWelcome"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "القائمة الرئيسية"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.btnMinimize, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.btnClose, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents btnAdministrator As System.Windows.Forms.Button
Friend WithEvents btnSalesCenter As System.Windows.Forms.Button
Friend WithEvents btnPurchaseCenter As System.Windows.Forms.Button
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents btnMinimize As System.Windows.Forms.PictureBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents btnClose As System.Windows.Forms.PictureBox
Friend WithEvents Label3 As System.Windows.Forms.Label
End Class
This is the expected screen:
And this is the screen I get after compile:
Please Advise.
change windows locale in adminstrative language settings to arabic
I managed to solve the problem, but it took with me a lot of time to finish it.
The solution is as the following:
Open each file containing arabic with Visual Basic Editor With Encoding and choose Arabic Windows Codepage 1256.
Click on save as and in the save screen choose the arrow on the save button to select save with encoding and select utc-8.
It will replace the old file with new one with good format, after I finished all files I build the solution and run it and everything worked as expected. (more then 70 files and for each of them one for design and other for code so basically 140 files)
I hope my experience with this problem can help someone who faced the same issue.

How can I add multiple titles on a gridview

I'm working with a gridview on vb.net for webforms, I have to add titles to specific groups of columns in this table.. but I have not a clue of how to do it, heres an example of what i want:
"Student info", "Subject info" and "Partial Grades" ARE the titles I need to add.
Default Datagridview hasnt got an option as u wish.
You can try devexpress (It has got tones of properties in gridcontrol)
You can create your own control acting like your "TITLE LABELS"
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim DataGridView1 = New System.Windows.Forms.DataGridView()
Dim Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Dim Label1 = New System.Windows.Forms.Label()
Dim Label2 = New System.Windows.Forms.Label()
'DataGridView1
DataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None
DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Column1, Column2, Column3})
DataGridView1.Location = New System.Drawing.Point(13, 78)
DataGridView1.Name = "DataGridView1"
DataGridView1.RowHeadersVisible = False
DataGridView1.Size = New System.Drawing.Size(397, 228)
DataGridView1.TabIndex = 0
DataGridView1.Name = "dgv"
AddHandler DataGridView1.ColumnWidthChanged, AddressOf DataGridView1_ColumnWidthChanged
'
'Column1
'
Column1.HeaderText = "Column1"
Column1.Name = "Column1"
'
'Column2
'
Column2.HeaderText = "Column2"
Column2.Name = "Column2"
'
'Column3
'
Column3.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill
Column3.HeaderText = "Column3"
Column3.Name = "Column3"
'
'TableLayoutPanel1
'
TableLayoutPanel1.ColumnCount = 2
TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 53.98936!))
TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 195.0!))
TableLayoutPanel1.Controls.Add(Label2, 1, 0)
TableLayoutPanel1.Controls.Add(Label1, 0, 0)
TableLayoutPanel1.Location = New System.Drawing.Point(12, 51)
TableLayoutPanel1.Name = "TableLayoutPanel1"
TableLayoutPanel1.RowCount = 1
TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
TableLayoutPanel1.Size = New System.Drawing.Size(398, 29)
TableLayoutPanel1.TabIndex = 1
TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
TableLayoutPanel1.Name = "title"
'
'Label1
'
Label1.Anchor = System.Windows.Forms.AnchorStyles.None
Label1.AutoSize = True
Label1.Location = New System.Drawing.Point(82, 8)
Label1.Name = "Label1"
Label1.Size = New System.Drawing.Size(39, 13)
Label1.TabIndex = 0
Label1.Text = "Label1"
'
'Label2
'
Label2.Anchor = System.Windows.Forms.AnchorStyles.None
Label2.AutoSize = True
Label2.Location = New System.Drawing.Point(281, 8)
Label2.Name = "Label2"
Label2.Size = New System.Drawing.Size(39, 13)
Label2.TabIndex = 1
Label2.Text = "Label2"
'
'Form1
'
Me.Controls.Add(TableLayoutPanel1)
Me.Controls.Add(DataGridView1)
End Sub
Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs)
Dim title As TableLayoutPanel = CType(Me.Controls("title"), TableLayoutPanel)
Dim dgv As DataGridView = CType(Me.Controls("dgv"), DataGridView)
title.ColumnStyles(0).Width = dgv.Columns(0).Width + dgv.Columns(1).Width
title.ColumnStyles(1).Width = dgv.Columns(2).Width
End Sub
End Class

Why do I need a function for this Visual Basic code?

I am trying to figure out the functions for an application that you order donuts and coffee through. There is no box to input quantities. The type of doughnuts are selected using radio buttons grouped in combo boxes as is the type of coffee. The user ordering does not input any information. That application calculates according to the selected radio buttons. There is also a 3% sales tax.
The donuts are as follows:
Glazed are $0.65, Sugar $0.65, Chocolate are $0.85, and Filled are $1.00.
The coffee costs:
Regular is $1.00 and cappuccino is $2.50.
How do I write a function for each one? I would think I would just write one function that calculates the donut, coffee and sales tax. I am not sure what I am supposed to include if there is only one possible choice for each case. I thought that I could just write a bunch of constants since nothing changed and do an If statement but the assignment is asking for functions.
This is what I have so far for the doughnuts.
Private Function GetDonutPrice(ByVal strDonut As String) As Double
Dim dblDonutPrice As Double
'Calculates and returns the price of donuts
If strDonut = "G" Or strDonut = "S" Then
dblDonutPrice = 0.65
ElseIf strDonut = "C" Then
dblDonutPrice = 0.85
Else
strDonut = "F"
dblDonutPrice = 1.0
End If
Return dblDonutPrice
End Function
And then for the btnCalc_Click I coded this:
Private Sub btnCalc_Click(sender As Object, e As EventArgs)
'Displays donut and cappucino subtotal, taxes and price
Dim strDonutPrice As String
Dim dblDonutPrice As Double
If radGlazed.Checked Then
strDonutPrice = "G"
ElseIf radSugar.Checked Then
strDonutPrice = "S"
ElseIf radChoc.Checked Then
strDonutPrice = "C"
ElseIf radFilled.Checked strDonutPrice = "F"
End If
' get the donut price '
dblDonutPrice = GetDonutPrice(strDonutPrice)
End Sub
And I get an error in dblDonutPrice = GetDonutPrice(strDonutPrice)
It sounds like you need a function that receives the type and quantity of doughnuts and does the math and returns the cost. And the same for the coffee. So you will have three functions total. The coffee, the doughnuts, and the main one that take the input of what is order and how many, calls the other functions, adds it up, and then does the sales tax
Something like this maybe.
Private Sub cmdAddItUp_Click(sender As System.Object, e As System.EventArgs) Handles cmdAddItUp.Click
Dim iQuantity1 as integer
Dim dDoughnutCost as double
'Get the data to send to doughnuts
iQuantity1 = val(TextBox1.Text)
iQuantity2 = val(TextBox2.Text)
etc.
'Get the doughnuts cost
dDoughnutCost = Doughtnuts(iQuantity1, iQuantity2, iQuantity3)
'Do the same for the coffee
dCoffeeCost = Coffees(iQuantity1, iQuantity2, iQuantity3)
'Add it up
dSubTotal = dDoughnutCost + dCoffeeCost
'Calculate tax
dTotal = Tax(dSubTotal)
'Now you have the total, do something with it. Display it maybe!
End Sub
Private Function Doughtnuts(iQuantity1 As Integer, iQuantity2 As Integer, iQuantity3 As Integer) As Double
'Do the math
End Function
Private Function Coffees(iQuantity1 As Integer, iQuantity2 As Integer, iQuantity3 As Integer) As Double
'Do the math
End Function
Private Function Tax(dSubtotal As Double) As Double
'Calculate tax
End Function
Hope that helps get you started. Good luck with your schooling.
I made some assumptions on your UI. Rather than placing the items as radio buttons in a ComboBox, I placed all of the doughnuts and coffees into a respective GroupBox. The concepts of this answer will still apply, however you'll just have to be mindful that this is not directly drop-in and go.
Here's the way I laid out the UI:
I have placed the RadioButtons for the items inside a FlowLayoutPanel inside of a GroupBox. I also placed labels into a TableLayoutPanel to provide the total amounts.
The most important part is how I named the RadioButton controls for the items being listed. In the code behind file I used the names of the RadioButton controls to check which Item was being modified. I could have populated the RadioButton controls at run time to ensure that everything lined up, but that was more work and it's not difficult to do if you use GetType(MyEnum).GetNames() in the Me.Load event of the form to create new RadioButtons with the correct name, text, and adding the proper event handlers to the radio buttons.
If you'd like to see how this works, add a new form to your Project and call it DoughnutsAndCoffee. Here is the designer file and the Code behind:
Code Behind:
Public Class DoughnutsAndCoffee
Private SelectedItems As New List(Of Items)
Public Event ItemsChanged()
Public Sub UpdateTotal() Handles Me.ItemsChanged
Dim subTotal As Decimal = 0D
For Each item As Items In SelectedItems
subTotal += Prices.GetPrice(item)
Next
Dim TaxTotal As Decimal = subTotal * Prices.Tax_Rate
Dim total As Decimal = subTotal + TaxTotal
Me.SubTotal_Label.Invoke(Sub() Me.SubTotal_Label.Text = subTotal.ToString("C2"))
Me.Tax_Label.Invoke(Sub() Me.Tax_Label.Text = TaxTotal.ToString("C2"))
Me.Total_Label.Invoke(Sub() Me.Total_Label.Text = total.ToString("C2"))
End Sub
Private Sub RadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles Cappuccino_RadioButton.CheckedChanged, Chocolate_RadioButton.CheckedChanged, Filled_RadioButton.CheckedChanged, Glazed_RadioButton.CheckedChanged, Regular_RadioButton.CheckedChanged, Sugar_RadioButton.CheckedChanged
Dim senderRB As RadioButton = DirectCast(sender, RadioButton)
Dim selectedItem As String = (From item As String In GetType(Items).GetEnumNames Where senderRB.Name.Contains(item) Select item).FirstOrDefault
If selectedItem <> String.Empty Then
Dim Item As Items = DirectCast([Enum].Parse(GetType(Items), selectedItem), Items)
If senderRB.Checked Then
Me.SelectedItems.Add(Item)
RaiseEvent ItemsChanged()
Else
If Me.SelectedItems.Contains(Item) Then
Me.SelectedItems.Remove(Item)
RaiseEvent ItemsChanged()
End If
End If
End If
End Sub
Private Sub DoughnutsAndCoffee_Shown(sender As Object, e As EventArgs) Handles Me.Shown
RaiseEvent ItemsChanged()
End Sub
End Class
Public Structure Prices
Public Const Tax_Rate As Decimal = 0.03D
Public Const Glazed As Decimal = 0.65D
Public Const Sugar As Decimal = 0.65D
Public Const Chocolate As Decimal = 0.85D
Public Const Filled As Decimal = 1D
Public Const Regular As Decimal = 1D
Public Const Cappuccino As Decimal = 2.5D
Public Shared Function GetPrice(item As Items) As Decimal
Dim itemStr As String = [Enum].GetName(GetType(Items), item)
Return GetType(Prices).GetField(itemStr).GetValue(Nothing)
End Function
End Structure
Public Enum Items
Glazed
Sugar
Chocolate
Filled
Regular
Cappuccino
End Enum
Designer File:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class DoughnutsAndCoffee
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Glazed_RadioButton = New System.Windows.Forms.RadioButton()
Me.Sugar_RadioButton = New System.Windows.Forms.RadioButton()
Me.Chocolate_RadioButton = New System.Windows.Forms.RadioButton()
Me.Filled_RadioButton = New System.Windows.Forms.RadioButton()
Me.Regular_RadioButton = New System.Windows.Forms.RadioButton()
Me.Cappuccino_RadioButton = New System.Windows.Forms.RadioButton()
Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Me.Total_Label = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.Tax_Label = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.SubTotal_Label = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.FlowLayoutPanel2 = New System.Windows.Forms.FlowLayoutPanel()
Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
Me.Coffee_GroupBox = New System.Windows.Forms.GroupBox()
Me.Doughnuts_GroupBox = New System.Windows.Forms.GroupBox()
Me.TableLayoutPanel1.SuspendLayout()
Me.FlowLayoutPanel2.SuspendLayout()
Me.FlowLayoutPanel1.SuspendLayout()
Me.Coffee_GroupBox.SuspendLayout()
Me.Doughnuts_GroupBox.SuspendLayout()
Me.SuspendLayout()
'
'Glazed_RadioButton
'
Me.Glazed_RadioButton.AutoSize = True
Me.Glazed_RadioButton.Location = New System.Drawing.Point(3, 3)
Me.Glazed_RadioButton.Name = "Glazed_RadioButton"
Me.Glazed_RadioButton.Size = New System.Drawing.Size(58, 17)
Me.Glazed_RadioButton.TabIndex = 9
Me.Glazed_RadioButton.Text = "Glazed"
Me.Glazed_RadioButton.UseVisualStyleBackColor = True
'
'Sugar_RadioButton
'
Me.Sugar_RadioButton.AutoSize = True
Me.Sugar_RadioButton.Location = New System.Drawing.Point(3, 26)
Me.Sugar_RadioButton.Name = "Sugar_RadioButton"
Me.Sugar_RadioButton.Size = New System.Drawing.Size(53, 17)
Me.Sugar_RadioButton.TabIndex = 10
Me.Sugar_RadioButton.Text = "Sugar"
Me.Sugar_RadioButton.UseVisualStyleBackColor = True
'
'Chocolate_RadioButton
'
Me.Chocolate_RadioButton.AutoSize = True
Me.Chocolate_RadioButton.Location = New System.Drawing.Point(3, 49)
Me.Chocolate_RadioButton.Name = "Chocolate_RadioButton"
Me.Chocolate_RadioButton.Size = New System.Drawing.Size(73, 17)
Me.Chocolate_RadioButton.TabIndex = 11
Me.Chocolate_RadioButton.Text = "Chocolate"
Me.Chocolate_RadioButton.UseVisualStyleBackColor = True
'
'Filled_RadioButton
'
Me.Filled_RadioButton.AutoSize = True
Me.Filled_RadioButton.Location = New System.Drawing.Point(3, 72)
Me.Filled_RadioButton.Name = "Filled_RadioButton"
Me.Filled_RadioButton.Size = New System.Drawing.Size(49, 17)
Me.Filled_RadioButton.TabIndex = 12
Me.Filled_RadioButton.Text = "Filled"
Me.Filled_RadioButton.UseVisualStyleBackColor = True
'
'Regular_RadioButton
'
Me.Regular_RadioButton.AutoSize = True
Me.Regular_RadioButton.Location = New System.Drawing.Point(3, 3)
Me.Regular_RadioButton.Name = "Regular_RadioButton"
Me.Regular_RadioButton.Size = New System.Drawing.Size(62, 17)
Me.Regular_RadioButton.TabIndex = 13
Me.Regular_RadioButton.Text = "Regular"
Me.Regular_RadioButton.UseVisualStyleBackColor = True
'
'Cappuccino_RadioButton
'
Me.Cappuccino_RadioButton.AutoSize = True
Me.Cappuccino_RadioButton.Location = New System.Drawing.Point(3, 26)
Me.Cappuccino_RadioButton.Name = "Cappuccino_RadioButton"
Me.Cappuccino_RadioButton.Size = New System.Drawing.Size(82, 17)
Me.Cappuccino_RadioButton.TabIndex = 14
Me.Cappuccino_RadioButton.Text = "Cappuccino"
Me.Cappuccino_RadioButton.UseVisualStyleBackColor = True
'
'TableLayoutPanel1
'
Me.TableLayoutPanel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TableLayoutPanel1.AutoSize = True
Me.TableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
Me.TableLayoutPanel1.ColumnCount = 2
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
Me.TableLayoutPanel1.Controls.Add(Me.Total_Label, 1, 2)
Me.TableLayoutPanel1.Controls.Add(Me.Label5, 0, 2)
Me.TableLayoutPanel1.Controls.Add(Me.Tax_Label, 1, 1)
Me.TableLayoutPanel1.Controls.Add(Me.Label3, 0, 1)
Me.TableLayoutPanel1.Controls.Add(Me.SubTotal_Label, 1, 0)
Me.TableLayoutPanel1.Controls.Add(Me.Label1, 0, 0)
Me.TableLayoutPanel1.Location = New System.Drawing.Point(67, 133)
Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
Me.TableLayoutPanel1.RowCount = 3
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
Me.TableLayoutPanel1.Size = New System.Drawing.Size(140, 57)
Me.TableLayoutPanel1.TabIndex = 9
'
'Total_Label
'
Me.Total_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Total_Label.AutoSize = True
Me.Total_Label.Location = New System.Drawing.Point(80, 41)
Me.Total_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Total_Label.Name = "Total_Label"
Me.Total_Label.Size = New System.Drawing.Size(39, 13)
Me.Total_Label.TabIndex = 5
Me.Total_Label.Text = "Label6"
'
'Label5
'
Me.Label5.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(10, 41)
Me.Label5.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(31, 13)
Me.Label5.TabIndex = 4
Me.Label5.Text = "Total"
'
'Tax_Label
'
Me.Tax_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Tax_Label.AutoSize = True
Me.Tax_Label.Location = New System.Drawing.Point(80, 22)
Me.Tax_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Tax_Label.Name = "Tax_Label"
Me.Tax_Label.Size = New System.Drawing.Size(39, 13)
Me.Tax_Label.TabIndex = 3
Me.Tax_Label.Text = "Label4"
'
'Label3
'
Me.Label3.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(10, 22)
Me.Label3.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(25, 13)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Tax"
'
'SubTotal_Label
'
Me.SubTotal_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.SubTotal_Label.AutoSize = True
Me.SubTotal_Label.Location = New System.Drawing.Point(80, 3)
Me.SubTotal_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.SubTotal_Label.Name = "SubTotal_Label"
Me.SubTotal_Label.Size = New System.Drawing.Size(39, 13)
Me.SubTotal_Label.TabIndex = 1
Me.SubTotal_Label.Text = "Label2"
'
'Label1
'
Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(10, 3)
Me.Label1.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(50, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "SubTotal"%0

Join multiple DataTables

I'm reading an XML file and storing the results into a DataSet. This dataset contains 10 DataTables. I would like to join all DataTables into one datatable and use this table to set as a datasource for a datagridview so I can later export the gridview into csv or excel. Currently I'm displaying each datatable in a separate gridview. I have tried datatable.merge() but that didn't work.
Any suggestions on how I can display this 10 datatables in a single datagridview?
Thanks
Imports System.Xml
Public Class Form1
Public tbl0 As New DataTable
Public tbl1 As New DataTable
Public tbl2 As New DataTable
Public tbl3 As New DataTable
Public tbl4 As New DataTable
Public tbl5 As New DataTable
Public tbl6 As New DataTable
Public tbl7 As New DataTable
Public tbl8 As New DataTable
Public tbl9 As New DataTable
Public result As New DataTable
Private Sub btnreadxml_Click(sender As Object, e As EventArgs) Handles btnreadxml.Click
Try
Dim filePath As String
filePath = "C:/Win/XMLReader/822396000069521.xml"
AuthorsDataSet.ReadXml(filePath)
tbl0 = AuthorsDataSet.Tables(0)
tbl1 = AuthorsDataSet.Tables(1)
tbl2 = AuthorsDataSet.Tables(2)
tbl3 = AuthorsDataSet.Tables(3)
tbl4 = AuthorsDataSet.Tables(4)
tbl5 = AuthorsDataSet.Tables(5)
tbl6 = AuthorsDataSet.Tables(6)
tbl7 = AuthorsDataSet.Tables(7)
tbl8 = AuthorsDataSet.Tables(8)
tbl9 = AuthorsDataSet.Tables(9)
DataGridView1.DataSource = tbl1
DataGridView2.DataSource = tbl2
DataGridView3.DataSource = tbl3
DataGridView4.DataSource = tbl4
DataGridView5.DataSource = tbl5
DataGridView6.DataSource = tbl6
DataGridView7.DataSource = tbl7
DataGridView8.DataSource = tbl8
DataGridView9.DataSource = tbl9
Catch ex As Exception
End Try
End Sub
There isn't one simple answer to this question but there are things you can do to make this more trivial. The following code are based on the following data schema.
Our goal is to solve this task using LINQ.
The first thing we should do is to create custom classes reflecting the data schema.
Public Class Author
Public Property pk_author As Integer
Public Property firstname As String
Public Property lastname As String
End Class
Public Class Book
Public Property pk_book As Integer
Public Property fk_author As Integer
Public Property title As String
End Class
Public Class Review
Public Property pk_review As Integer
Public Property fk_book As Integer
Public Property [text] As String
End Class
Next we'll need to create two extension/helper method. (You'll find the full source code at the bottom of this post.)
ToDataTable
This function will turn a List(Of T) into a DataTable based on the properties defined in T.
<Extension()>
Public Function ToDataTable(Of T)(source As List(Of T)) As DataTable
ToList
This function will turn a DataTable into a List(Of T) based on the properties defined in T and columns contained by the source table.
<Extension()>
Public Function ToList(Of T As {Class, New})(source As DataTable) As List(Of T)
Usage
Read the XML file into a DataTable
Dim [set] As DataSet = ReadXmlIntoDataSet()
"Convert" the tables into lists.
Dim authors As List(Of Author) = [set].Tables("authors").ToList(Of Author)()
Dim books As List(Of Book) = [set].Tables("books").ToList(Of Book)()
Dim reviews As List(Of Review) = [set].Tables("reviews").ToList(Of Review)()
Use LINQ to join the lists and then "convert" the result back to a DataTable.
Dim results As DataTable = (
From a In authors
From b In books.Where(Function(item) ((Not a Is Nothing) AndAlso item.fk_author = a.pk_author)).DefaultIfEmpty()
From r In reviews.Where(Function(item) ((Not b Is Nothing) AndAlso item.fk_book = b.pk_book)).DefaultIfEmpty()
Select New With {
.pk_author = If((Not a Is Nothing), New Integer?(a.pk_author), Nothing),
.pk_book = If((Not b Is Nothing), New Integer?(b.pk_book), Nothing),
.pk_review = If((Not r Is Nothing), New Integer?(r.pk_review), Nothing),
.firstname = If((Not a Is Nothing), a.firstname, Nothing),
.lastname = If((Not a Is Nothing), a.lastname, Nothing),
.title = If((Not b Is Nothing), b.title, Nothing),
.[text] = If((Not r Is Nothing), r.[text], Nothing)
}
).ToList().ToDataTable()
Source code
References
Imports System.Runtime.CompilerServices
Imports System.ComponentModel
Imports System.Reflection
Imports <your namespace goes here>.Extensions
Author
Public Class Author
Public Property pk_author As Integer
Public Property firstname As String
Public Property lastname As String
End Class
Book
Public Class Book
Public Property pk_book As Integer
Public Property fk_author As Integer
Public Property title As String
End Class
Review
Public Class Review
Public Property pk_review As Integer
Public Property fk_book As Integer
Public Property [text] As String
End Class
Extensions
<Extension()>
Public Module Extensions
<Extension()>
Public Function ToDataTable(Of T)(source As List(Of T)) As DataTable
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
Dim table As New DataTable()
Dim descriptor As PropertyDescriptor = Nothing
Dim column As DataColumn = Nothing
Dim values As Object() = Nothing
Dim length As Integer = Nothing
Dim index As Integer = Nothing
Dim item As T = Nothing
Dim type As Type = Nothing
table.BeginInit()
For Each descriptor In properties
type = Nullable.GetUnderlyingType(descriptor.PropertyType)
column = New DataColumn()
column.ColumnName = descriptor.Name
column.Caption = descriptor.DisplayName
column.DataType = If((type Is Nothing), descriptor.PropertyType, type)
column.ReadOnly = descriptor.IsReadOnly
table.Columns.Add(column)
Next
table.BeginLoadData()
length = (properties.Count - 1)
values = New Object(length) {}
For Each item In source
For index = 0 To length
values(index) = properties(index).GetValue(item)
Next
table.Rows.Add(values)
Next
table.EndLoadData()
table.EndInit()
Return table
End Function
<Extension()>
Public Function ToList(Of T As {Class, New})(source As DataTable) As List(Of T)
If (source Is Nothing) Then
Throw New ArgumentNullException("source")
End If
Dim list As New List(Of T)
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
Dim descriptor As PropertyDescriptor = Nothing
Dim index As Integer = Nothing
Dim row As DataRow = Nothing
Dim item As T = Nothing
For index = (properties.Count - 1) To 0 Step -1
If (Not source.Columns.Contains(properties(index).Name)) Then
properties.RemoveAt(index)
End If
Next
For Each row In source.Rows
item = New T()
For Each descriptor In properties
descriptor.SetValue(item, row.Item(descriptor.Name))
Next
list.Add(item)
Next
Return list
End Function
End Module
Sample application
Public Class Form1
Public Sub New()
Me.InitializeControls()
Try
Dim [set] As DataSet = Form1.ReadXmlIntoDataSet()
Dim authors As List(Of Author) = [set].Tables("authors").ToList(Of Author)()
Dim books As List(Of Book) = [set].Tables("books").ToList(Of Book)()
Dim reviews As List(Of Review) = [set].Tables("reviews").ToList(Of Review)()
Dim results As DataTable = (
From a In authors
From b In books.Where(Function(item) ((Not a Is Nothing) AndAlso item.fk_author = a.pk_author)).DefaultIfEmpty()
From r In reviews.Where(Function(item) ((Not b Is Nothing) AndAlso item.fk_book = b.pk_book)).DefaultIfEmpty()
Select New With {
.pk_author = If((Not a Is Nothing), New Integer?(a.pk_author), Nothing),
.pk_book = If((Not b Is Nothing), New Integer?(b.pk_book), Nothing),
.pk_review = If((Not r Is Nothing), New Integer?(r.pk_review), Nothing),
.firstname = If((Not a Is Nothing), a.firstname, Nothing),
.lastname = If((Not a Is Nothing), a.lastname, Nothing),
.title = If((Not b Is Nothing), b.title, Nothing),
.[text] = If((Not r Is Nothing), r.[text], Nothing)
}
).ToList().ToDataTable()
Me.resultsGrid.DataSource = results
Me.authorsGrid.DataSource = authors
Me.booksGrid.DataSource = books
Me.reviewsGrid.DataSource = reviews
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub InitializeControls()
Me.Label1 = New System.Windows.Forms.Label() With {.AutoSize = True, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 213), .Name = "Label1", .Size = New System.Drawing.Size(60, 18), .TabIndex = 0, .Text = "Reviews"}
Me.Label2 = New System.Windows.Forms.Label() With {.AutoSize = True, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 142), .Name = "Label2", .Size = New System.Drawing.Size(45, 18), .TabIndex = 4, .Text = "Books"}
Me.Label3 = New System.Windows.Forms.Label() With {.AutoSize = True, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 71), .Name = "Label3", .Size = New System.Drawing.Size(57, 18), .TabIndex = 7, .Text = "Authors"}
Me.Label4 = New System.Windows.Forms.Label() With {.AutoSize = True, .Dock = System.Windows.Forms.DockStyle.Top, .Font = New System.Drawing.Font("Calibri", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)), .Location = New System.Drawing.Point(0, 0), .Name = "Label4", .Size = New System.Drawing.Size(51, 18), .TabIndex = 10, .Text = "Result:"}
Me.Splitter1 = New System.Windows.Forms.Splitter() With {.BackColor = System.Drawing.Color.Gray, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 203), .Name = "Splitter1", .Size = New System.Drawing.Size(613, 10), .TabIndex = 2, .TabStop = False}
Me.Splitter2 = New System.Windows.Forms.Splitter() With {.BackColor = System.Drawing.Color.Gray, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 132), .Name = "Splitter2", .Size = New System.Drawing.Size(613, 10), .TabIndex = 5, .TabStop = False}
Me.Splitter3 = New System.Windows.Forms.Splitter() With {.BackColor = System.Drawing.Color.Gray, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 61), .Name = "Splitter3", .Size = New System.Drawing.Size(613, 10), .TabIndex = 8, .TabStop = False}
Me.resultsGrid = New System.Windows.Forms.DataGridView() With {.AllowUserToAddRows = False, .AllowUserToDeleteRows = False, .BackgroundColor = System.Drawing.Color.White, .ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 18), .Name = "resultsGrid", .ReadOnly = True, .Size = New System.Drawing.Size(613, 280), .TabIndex = 9}
Me.authorsGrid = New System.Windows.Forms.DataGridView() With {.AllowUserToAddRows = False, .AllowUserToDeleteRows = False, .BackgroundColor = System.Drawing.Color.White, .ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 89), .Name = "authorsGrid", .ReadOnly = True, .Size = New System.Drawing.Size(613, 115), .TabIndex = 6}
Me.booksGrid = New System.Windows.Forms.DataGridView() With {.AllowUserToAddRows = False, .AllowUserToDeleteRows = False, .BackgroundColor = System.Drawing.Color.White, .ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize, .Dock = System.Windows.Forms.DockStyle.Top, .Location = New System.Drawing.Point(0, 160), .Name = "booksGrid", .ReadOnly = True, .Size = New System.Drawing.Size(613, 200), .TabIndex = 3}
Me.reviewsGrid = New System.Windows.Forms.DataGridView() With {.AllowUserToAddRows = False, .AllowUserToDeleteRows = False, .BackgroundColor = System.Drawing.Color.White, .ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize, .Dock = System.Windows.Forms.DockStyle.Fill, .Location = New System.Drawing.Point(0, 231), .Name = "reviewsGrid", .ReadOnly = True, .Size = New System.Drawing.Size(613, 415), .TabIndex = 1}
Me.SuspendLayout()
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 18.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(613, 900)
Me.Controls.AddRange({Me.reviewsGrid, Me.Label1, Me.Splitter1, Me.booksGrid, Me.Label2, Me.Splitter2, Me.authorsGrid, Me.Label3, Me.Splitter3, Me.resultsGrid, Me.Label4})
Me.Font = New System.Drawing.Font("Calibri", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Private Shared Function ReadXmlIntoDataSet() As DataSet
Dim [set] As New DataSet()
Dim authors As DataTable = [set].Tables.Add("authors")
With authors
.Columns.Add("pk_author", GetType(Integer))
.Columns.Add("firstname", GetType(String))
.Columns.Add("lastname", GetType(String))
.BeginLoadData()
.Rows.Add(1, "William", "Shakespeare")
.Rows.Add(2, "Henrik", "Ibsen")
.Rows.Add(3, "Ernest", "Hemingway")
.EndLoadData()
.AcceptChanges()
End With
Dim books As DataTable = [set].Tables.Add("books")
With books
.Columns.Add("pk_book", GetType(Integer))
.Columns.Add("fk_author", GetType(Integer))
.Columns.Add("title", GetType(String))
.BeginLoadData()
.Rows.Add(1, 1, "Timon of Athens")
.Rows.Add(2, 1, "Titus Othello")
.Rows.Add(3, 1, "The Comedy of Errors")
.Rows.Add(4, 2, "Peer Gynt")
.Rows.Add(5, 2, "A Doll's House")
.Rows.Add(6, 2, "Emperor and Galilean")
.EndLoadData()
.AcceptChanges()
End With
Dim reviews As DataTable = [set].Tables.Add("reviews")
With reviews
.Columns.Add("pk_review", GetType(Integer))
.Columns.Add("fk_book", GetType(Integer))
.Columns.Add("text", GetType(String))
.BeginLoadData()
.Rows.Add(1, 1, "The book 'Timon of Athens' blabla ...")
.Rows.Add(2, 2, "The book 'Titus Othello' blabla ...")
.Rows.Add(3, 4, "The book 'Peer Gynt' blabla 1...")
.Rows.Add(4, 4, "The book 'Peer Gynt' blabla 2...")
.Rows.Add(5, 4, "The book 'Peer Gynt' blabla 3...")
.Rows.Add(6, 5, "The book 'A Doll's House' blabla ...")
.EndLoadData()
.AcceptChanges()
End With
[set].Relations.Add(New DataRelation("books_author", authors.Columns("pk_author"), books.Columns("fk_author")))
[set].Relations.Add(New DataRelation("reviews_books", books.Columns("pk_book"), reviews.Columns("fk_book")))
Return [set]
End Function
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
Friend WithEvents Splitter2 As System.Windows.Forms.Splitter
Friend WithEvents Splitter3 As System.Windows.Forms.Splitter
Friend WithEvents resultsGrid As System.Windows.Forms.DataGridView
Friend WithEvents authorsGrid As System.Windows.Forms.DataGridView
Friend WithEvents booksGrid As System.Windows.Forms.DataGridView
Friend WithEvents reviewsGrid As System.Windows.Forms.DataGridView
End Class