standard or generic forms

D

dominique

Hi,

I want to write code to manage standard (or generic) forms in Windows
Forms (i don't know the correct word) like that :
- a base form in a class : frmBase with some controls inside it :
labels, textbox, buttons ..
- many forms, each in a class : frmCustomer, frmSupplier, frmProduct ...
Each form inherits from frmBase and have specific controls (labels,
textbox, buttons ..) more inherited controls of frmBase.
Each class (frmBase, frmCustomer ..) is in a file (frmBase.vb,
frmCustomer.vb ..).

I want to access each controls (specific and inherited) as control array
(i know there are not now control arrays in .net).
So i want use the code that Cor Ligthert suggests :

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub

Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
' and many other AddHandler
doSet(ctr)
Next
End Sub

Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
' here code to manage LostFocus
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
' here code to manage GotFocus
End Sub
...

I don't want to put all the code in each form but to have only once this
code.

Now my questions :
1) is it possible to put this code only in frmBase ?
For that, is it possible to access in frmBase at the specific controls
of derived forms ? by reflection ?
by polymorphism ?

2) can the address following the keyword Addressof to be in another
class ? in the same project ?
in an another project of the solution ?

Many thanks for your help, ideas, links to articles ..

Best Regards,

Dominique
 
C

Cor Ligthert

Dominique,

We did a long time not see you.

However answering while not going to deep in your question and hoping I
understand you well. When you see that routine I made is it only using a
reference to a control.

So in my opinion that control can be anywhere. (When I would do what you
want was first making a shared class for it doing those routines). Seeing
your problem the only thing I can think about is why not.

You have to keep in mind that using those routines that you should always
start with the parent control.

I hope this helps?

Cor
 
D

dominique

Cor,

Thanks for your answer.

You said "We did a long time not see you."
.. but it is not me you know, it is the first time i post in this
newsgroup.

"dominique" is a french firstname and several people have this
firstname. Next time i 'll put also my lastname.

Yes i understood that your code is about controls but what i want, it is
to manage controls of derived and base form, with the same code and use
the same events with AddHandler.

I am re-writing many code i wrote in Basic PDS 7.1 and i got used my
users to specific user interface (for example : key arrow down == tab,
arrow up = shift tab ..). I need to keep the same in vb.net then i must
manage events in all the forms and more i want used base form.

If you (or others) can help me more, it will be very well.

Cheers,

dominique gratpain
 
C

Cor Ligthert

Hi Dominique,

There was a Belgian Guy a time in this newsgroup who was helping a lot. I
first made the mistake that I thought he was a woman, so maybe can you make
this clear as well, because in my country Dominique is a name mostly used by
woman. I know that it is in the French language for man and a woman as I
thought can be seen in Asterix.

Beneath I made a sample as I thought you where asking for?

I hope that helps?

Cor

\\\
Public Class Form1
Inherits Form2
#Region " Windows Form Designer generated code "
'.................
End Class
///
\\\\a form2 with by instance 3 textboxes
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Dim myFormhandlers As New FormHandlers(Me)
End Sub
'...............
End Class
///
\\\
Public Class FormHandlers
Sub New(ByVal TopMyform As Control)
doSet(TopMyform)
End Sub
Dim last As String
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
doSet(ctr)
Next
End Sub
Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
last = DirectCast(sender, Control).Name
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).Text = last
End Sub
End Class
////
 
D

dominique

Hi Cor,

Thanks, you solved my problem :

it missed me the instruction :
Dim myFormhandlers As New FormHandlers(Me)
in the constructor of the form.

Three comments :

1) we can put also this instruction in the constructor of the derived
form (form1 in your instance with some textbox in form1). It is ok for
the textbox of form1 (derived class) and the textbox of form2 (base
class)

2) in your code, there is a small problem with the first textbox when it
got the focus (because last = "").
i add that and it is ok :

Dim i as Integer=0

Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
if i=1 then DirectCast(sender, Control).Text = last
i=1
End Sub

3) i tried your code in debug mode with breakpoints on
last = DirectCast(sender, Control).Name

and

if i=1 then DirectCast(sender, Control).Text = last

i thought it'll go once on lostfocus and once on gotfocus but it is not
the case, it goes many times on lostfocus and got focus, strange !!, i
don't understand (even with one form without derived form).


Now about my fistname, yes it is true, in France, Dominique is used both
for men and women.
Normally it is for men because Saint Dominique was a man (around year
1200). Often in France, a firstname which finished by "e", is used by
women. Sometimes also we add a "e" at a name for men to have a name for
women :
Michel, Michèle or Daniel, Danièle or Jean, Jeanne ..

Me, i am a man and i live near Paris.
And you, what country are you from ?

a big thank you for your help

Dominique
 
C

Cor Ligthert

Dominique,

Glad it is solved, this is a sample, so it has to be as small as possible
and you can use it as you want. (Questions 1 and 2).

For the focus I thought that that was that it lost it focus as well to the
debugger, however I did never thought any further than that (Question 3).

I was often in Paris, I had/have family living there.
(Now only still at Glenvilliers)

Cor (from Holland)
 

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

Similar Threads


Top