User Control in a Library

G

Guest

Hi

I'm new at this and need some help. I have a "solution" with 2 projects - the main project and a library. The library is simple, it contains a text box that will appear many times in the form in the main project

My problem is that I would like events (leaving the text box) to be recognized in the main project. Alternatively I would like to be able to access classes in the main project -- I would like to be able to have the class with the text box be able to set a property in the main project

I can't seem to figure out how to connect these two projects. I was able to drag the text box from the library into a control on the main form. But beyond that I don't know how to communicate between these things

Any help would be greatly appreciated

Thanks

Art
 
C

Charles Law

Hi Art

A common way for a class to get access to its parent is to pass a reference
to it when it is created:

<code>
Dim ctrl As MyControl

ctrl = New MyControl(Me)
</code>

The constructor in your control would look like this:

<code>
Private m_Parent As Object

Public Sub New(Parent As Object)

MyBase.New()

m_Parent = Parent

End Sub
</code>

If the Parent is always going to be the same type you can be more specific
than declaring it as Object.

However, I would be wary of directly manipulating properties on the parent
object. It really depends on what things you want your control to do. For
example, if you want to maintain a menu item state according to the state of
your control, consider using an event raised by your user control. The user
control then does not need to know any specifics about the parent, but the
parent can handle the event and do what it likes with its own objects.

This method increases cohesion and decreases coupling.

With regard to passing events from the text box to the parent, you need to
handle the textbox events in your user control, and use OnXXXX in the event
handler to raise events to the parent, where XXXX is the event in question
[thanks Herfried].

HTH

Charles


Art said:
Hi!

I'm new at this and need some help. I have a "solution" with 2 projects -
the main project and a library. The library is simple, it contains a text
box that will appear many times in the form in the main project.
My problem is that I would like events (leaving the text box) to be
recognized in the main project. Alternatively I would like to be able to
access classes in the main project -- I would like to be able to have the
class with the text box be able to set a property in the main project.
I can't seem to figure out how to connect these two projects. I was able
to drag the text box from the library into a control on the main form. But
beyond that I don't know how to communicate between these things.
 
G

Guest

Charles

Thanks very much for your help -- while I have to read through it again, I think I may have created more of a problem for myself. I don't instantiate the text box control in the code of the parent. I drag it onto another user control, one that is in the parent's project. That user control is instantiated through code

I was hoping that I could visually arrange these text boxes on the parent's user control -- but by doing that I don't know how to reach back into them

Of course, it's not unlikely that I have absolutely no idea what I'm doing, and somehow I can instantiate the text boxes even after dragging them onto my user control

Also, I don't think I understan

"you need to handle the textbox events in your user control, and use OnXXXX in the event handler to raise events to the parent, where XXXX is the event in question

Is this any different than adding a handler for an event in my main project

Thanks again

Art
 
C

Charles Law

The point about calling OnXXXX is that when the text box raises an event, it
does so on its parent or container. The container in this case is your user
control. In order to get the event raised on the consumer of your user
control, it needs to cause an event to be raised on its parent or container,
and he way to do this is to call OnXXXX in the user control.

As for linking your controls back to the parent, when you drop the control
onto the user control in the parent, you will need to process the event
raised when the control is dropped, and use AddHandler to enable events to
be caught by the user control that you instantiate in code. That sounds like
a mouthful, but hopefully it makes some sense.

If I have your scenario right, you have

Parent -> UserControlA -> UserControlB -> TextBox

where -> indicates "contains".

UserControlA is instantiated in code, but UserControlB is dragged onto
UserControlA. I have a similar situation, where Parent is an MDI
application, UserControlA is actually an MDI child form, UserControlB is a
user control which contains other user controls rather than a textbox; but
the principle is the same.

I drag UserControlB onto my child form (your UserControlA) and in the
DragDrop event of the child form I instantiate UserControlB according to the
data being dragged. I also add handlers (AddHandler) for the key mouse
events so that I can click on the new UserControlB and drag it around the
form.

HTH

Charles


Art said:
Charles,

Thanks very much for your help -- while I have to read through it again, I
think I may have created more of a problem for myself. I don't instantiate
the text box control in the code of the parent. I drag it onto another user
control, one that is in the parent's project. That user control is
instantiated through code.
I was hoping that I could visually arrange these text boxes on the
parent's user control -- but by doing that I don't know how to reach back
into them.
Of course, it's not unlikely that I have absolutely no idea what I'm
doing, and somehow I can instantiate the text boxes even after dragging them
onto my user control.
Also, I don't think I understand

"you need to handle the textbox events in your user control, and use
OnXXXX in the event handler to raise events to the parent, where XXXX is the
event in question"
 
G

Guest

Charles

I'm writing this before I fully understand your latest answer, mostly to give you more time, (if you have it). As a brief aside... even though I'm still having difficulty, your answers so far have helped me to understand more of this new vb.net world -- so thanks very much for all of the info so far

One thing I didn't make clear, and perhaps I shouldn't be doing this anyway, is that I'm dragging the text box over during the design of the main form. From your description it sounds like you are envisioning that this drag happens during the execution

Also (and I said I was new to this) you mention that you're working on an MDI application. I had to look that up -- if I found the right thing, that's a multiple document interface? It may be that there's something in that area that I should spend some time learning

Your diagram of my setup is correct. Again, the only difference -may be- that I'm dragging during the design. Normally when I put an AddHandler statement I have had to dim the control WithEvents for my parent to respond to the events. I don't see how to do this at design time. I'll have to spend a little time going over what you've described, but I'm guessing that the DragDrop event isn't going to have effect when I'm designing my form

Ar
 
C

Charles Law

I think I see now what you are doing.

I am not sure though when you want the communication to take place. Do you
want it to take place during design as well, or during execution?

If your user control declares any public events then it will be declared
WithEvents when you drop it onto the parent. You can then write code at
design time to handle the events that it raises, and hook it up with the
Handles keyword, for example

<code>
Public Class UserControlB

Inherits UserControl

' Windows Form Designer Generated Code
' Includes the following:
Private WithEvents TextBox1 As TextBox

Public Event DoStuff(sender As Object, e As DoStuffEventArgs) ' **
this is a shortcut; look at delegates **

' ...

Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles
TextBox1.Click()

RaiseEvent DoStuff(Me, New DoStuffEventArgs)

End Sub

End Class

Public Class UserControlA

Inherits UserControl

' Windows Form Designer Generated Code
' Includes the following:
Private WithEvents UserControlB1 As UserControlB

Private Sub UserControlB1_DoStuff(sender As Object, e As
DoStuffEventArgs) Handles UserControlB1.DoStuff
' Code here to handle event from UserControlB
End Sub

End Class
</code>

I haven't tried to compile the code above, but the principle should be ok.
If I have still misunderstood, perhaps you could post some of the code you
have so far. It might be easier to help by filling in the gaps of a concrete
example.

HTH

Charles


Art said:
Charles,

I'm writing this before I fully understand your latest answer, mostly to
give you more time, (if you have it). As a brief aside... even though I'm
still having difficulty, your answers so far have helped me to understand
more of this new vb.net world -- so thanks very much for all of the info so
far.
One thing I didn't make clear, and perhaps I shouldn't be doing this
anyway, is that I'm dragging the text box over during the design of the main
form. From your description it sounds like you are envisioning that this
drag happens during the execution.
Also (and I said I was new to this) you mention that you're working on an
MDI application. I had to look that up -- if I found the right thing,
that's a multiple document interface? It may be that there's something in
that area that I should spend some time learning.
Your diagram of my setup is correct. Again, the only difference -may be-
that I'm dragging during the design. Normally when I put an AddHandler
statement I have had to dim the control WithEvents for my parent to respond
to the events. I don't see how to do this at design time. I'll have to
spend a little time going over what you've described, but I'm guessing that
the DragDrop event isn't going to have effect when I'm designing my form.
 
G

Guest

Charles,

You do appear to understand what I'm trying to do. In tryiing to respond to what you've written I've found (I think) that much of my problem is due to my poor understanding (still!) of how and when classes are instantiated and become objects. The text boxes, as they appear in the main project, have different functions, but are very very similar. I wanted to add some of the functionality for that similar part to the user control in the library.

I think I need to take some of what you've already written and spend some time staring at my application. However, in case you're interested, the basic details of what I'm doing are:

I work for a reinsurance company (much like an insurance company). One of our products is called a "treaty", which is an agreement with an insurance company (our customer). A treaty has associated with it 3 numbers -- the amount of coverage, the attachment point (when it comes into play), and an occurrence limit. Those 3 numbers are my text boxes.

Actually, a treaty can consist of a variable number of layers where each layer has that set of three numbers associated with it.

The main form will let a user enter all of the layers for a treaty. Each layer will look like a row. The row is a user control that I created visually. My library consists of one text box which I dragged into the row three times. As the user clicks a button to create a new layer, the form will instantiate a new row (and a new layer - a seperate class) and display it.

There are different types of layers and in one, for example, the occurrence text box is disabled. This project then has to do some work on the layers that get entered.

I'm doing this part as a real project here, but also as a learning exercise. I've got a working version in Excel, so I'm not under pressure to get it done. If I can do this using VB.net I can add quite a bit of functionaility - I think.

Anyway, thank you for all of your help.

Art
 

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