473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

User Control in a Library

Art
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
Nov 20 '05 #1
6 1661
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" <ar*****@yahoo.com> wrote in message
news:98**********************************@microsof t.com...
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

Nov 20 '05 #2
Art
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
Nov 20 '05 #3
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" <ar*****@malchi.com> wrote in message
news:9E**********************************@microsof t.com...
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"
Is this any different than adding a handler for an event in my main project?
Thanks again,

Art

Nov 20 '05 #4
Art
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
Nov 20 '05 #5
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" <arthlan> wrote in message
news:AD**********************************@microsof t.com...
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.
Art

Nov 20 '05 #6
Art
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
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
5
by: Marcel Gelijk | last post by:
Hi, I am trying to create a User Control that is located in a seperate class library. The User Control contains a textbox and a button. The page generates an exception when it tries to access...
4
by: thomson | last post by:
Hi all, i do have a user control with 4 buttons, and all the events are firing properly, My problem is that i need to right an event handler in the user control, which gets fired after a...
3
by: Joe | last post by:
Hello All, I am developing a webform which creates ArrayLists of people's names and addresses (the values are retrieved from an xml file) and dynamically drops a user control onto the webform...
4
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
5
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
1
by: Tony Johansson | last post by:
Hello!! I use VS 2003 and C#. I have sent several mail even tried with crossgroup because I want to find out if my problem is a bug in .NET or if .NET doesn't support what I do. It's only one...
1
by: Tony Johansson | last post by:
Hello!! I use VS 2003 and C#. I have sent several mail even tried with crossgroup because I want to find out if my problem is a bug in .NET or if .NET doesn't support what I do. It's only one...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
5
by: =?Utf-8?B?U2FsYW1FbGlhcw==?= | last post by:
Hi, I have a user control which I like to use in several projects (winforms) in the same solution. Inside lets say Winapp1 , When adding a reference to this control, I can dynamically or statically...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.