C# .NET adding user defined controls to a form

  • Thread starter Thread starter Liz
  • Start date Start date
L

Liz

In VB.NET I add user defined controls to the main panel
of a form :-

Dim controlInfo As MainTemplate
controlInfo = New Details
controlInfo.Dock = DockStyle.Fill
' remove the old one if one already on display
If pnlMain.Controls.Count > 0 Then
pnlMain.Controls.RemoveAt(0)
pnlMain.Controls.Add(controlInfo)
End If

pnlMain.Visible = True

How do I do the same in C# .NET?


Liz
 
Hi Liz,

Here's the C# code. Note that there are lot's of tools out there that will
convert VB.NET code to C# and vice versa.

MainTemplate controlInfo;
controlInfo = new Details();
controlInfo.Dock = DockStyle.Fill;
// remove the old one if one already on display
if (pnlMain.Controls.Count > 0)
{
pnlMain.Controls.RemoveAt(0);
pnlMain.Controls.Add(controlInfo);
}
pnlMain.Visible = true;
 
Hi Liz,

In the same way :)

// Now I'm assuming that Details is a subclass of MainTemplate, otherwise
this give you error at compile time
MainTemplate controlInfo = new Details ();
controlInfo.Dock = DockStyle.Fill;
If ( pnlMain.Controls.Count > 0 )
{
pnlMain.Controls.RemoveAt(0);
pnlMain.Controls.Add(controlInfo);
}
pnlMain.Visible = true;

Hope this help,
 
Many Thanks - Yes, Details is a subclass of MainTemplate -
It's easy when you know how. I'm having some
difficulties reorganising my thoughts from VB to C#. Liz
 
Many Thanks Rob - Can you point me in the direction of
any "free" VB to C# conversion tools? Liz
 
Thanks for your patience. I did discover the ellkay link
yesterday and this is free and very useful for converting
small bits of code. I shall have a look at all the other
links now. Thanks again - Liz
 
Back
Top