Am 31.07.2010 12:06, schrieb C:
My main question was what I should do about
FormN.Label30.Text = progTitle
This does not work any more. Is there a good workaround?
FormN is an instanced class of a form. Each form in my program will
have a Label30 which should get the title of the program. I know there
are million ways of doing this, but to make the forms look similar, I
have a subroutine which will make several things on each form similar.
This subroutine formsize is called by every Form*_Load event in VB6,
and I am thinking of doing it in a similar way in VB.net also.
You have a solution for a problem in VB6. If you want to solve the problem
in VB.Net, you should find a VB.Net solution. It's not always good to
use the VB6 solution in VB.Net. Often, the solutions are the same, sometimes
they are similiar, sometimes they are completely different.
This is only a general hint for upgrading.
VB6 programming was unsafe programming. IMO, nowadays, this is unacceptable.
In this case, unsafe means using late binding. The type "Form" does not have
a member called Label30. Therefore you can not compile this code. Full stop.
In VB.Net, if you don't enable late binding for very rare cases, the compiler
checks the existence of type members. You are not allowed to access type
members that do not exist for that type. Not every Form has a "Label30".
The big difference is that we now have inheritance and (explicit) interfaces.
This is the VB.Net solution to your problem:
If all your forms have a "Label30", define a common base Form and make use
of Form inheritance. This makes all inherited Forms look like the base form,
and you can add additional controls to the inherited Forms. Put your Resize
code into the base form and all your inherited forms will automatically have
that feature. There is no need to explicitly call that FormSize sub in any of
your derived Forms.
The other way is declare and implement an Interface in all your
Forms, so you will be able to pass them to your FormSize sub that works
on that interface. I guess the Interface solution is the quicker way because
you don't have to change the design of all your Forms. Would be a big
construction zone. In the long run, and for new projects anyway, you should
choose the inheritance solution if possible. So, the Interface solution is:
Interface IForm
Property Width() As Integer
Property Height() As Integer
Property BackColor() As Color
End Interface
Interface IBaseForm
Inherits IForm
ReadOnly Property Label30() As Label
ReadOnly Property Label31() As Label
End Interface
Shared Sub FormSize(ByVal Form As IBaseForm)
Form.Height = 4711
Form.Width = 4712
Form.BackColor = Color.FromArgb(1, 2, 3)
Form.Label30.Width = 800
Form.Label31.Width = 800
Form.Label30.Text = "sdfsdf"
Form.Label31.Text = "sdfsdf"
Form.Label31.ForeColor = Color.White
End Sub
In addition, enter "Implements IBaseForm" (+press Enter-Key) in all your
Forms. Maybe you'll be shocked initially. ;-) All the members for implementing
the interface will be created. As some of the members already exist in the Form
or in the base class, you'll get strange names like Width1 instead of Width.
You can change the signature, for example, change
Public Property Width1() As Integer Implements IForm.Width
to
Private Property IForm_Width() As Integer Implements IForm.Width
Then you'll have to fill the members with meaning:
Private Property IForm_Width() As Integer Implements IForm.Width
Get
Return Width
End Get
Set(ByVal value As Integer)
Width = value
End Set
End Property
In general, class/interface modelling should be done more thoroughly.
I've only inferred the interfaces from the code in your sub. Depending
on the exact design of your Forms, other solutions are possible. I guess
the Forms _are_ different, otherwise you wouldn't need different Form
classes. For example, you could write a Usercontrol putting some of the
common controls into it, then place the Usercontrol on the Forms
(what you already know from VB6). It all depends. To find the best
solution, we would have to discuss what exactly your Forms have in
common, and so on.
I didn't refer to other subjects like ScaleMode etc. These are different
topics.
You may wonder: "Why all this? It was soooo simple in VB6."
This is because of shifted priorities. Late binding like in VB6 is very
slow and it does not make sense to make the run time search for a member
at run time as the compiler is able to see if it exists at compile time.
In addition, if the member is not found, an exception occours (and
maybe your customer complains instead of the compiler enabling you
to fix it)
So, the efforts as suggested above are usually only necessary if you
upgrade a project. If you create a new one and do the right design
steps, it's easier than before, thanks to inheritance.