Passing Data From CustomControls C#

M

MikeY

Hi Everyone,

I have a custom control on my application and I am trying to pass the data
items from the selected custom buttons back to a ListView. I'm sitting here
scratching my head thinking I should know this.

The ListViewnamed ListView1 resides on Host_Horizontal_Btn "Form1".cs and my
Custom Buttons reside on Host_Horizontal_Btn "Ctr_Horizontal_Btn.cs". What I
want is when I press my button, the item name gets added to the ListView.

Thank you all in advance.

MikeY
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

On the custom control you can access the parent by:

this.Parent.Controls[...].xxx(...)

You can go as far up the chain as you want with:

this.Parent.Controls[0].Parent[1]


Here you can locate the ListView and add a new item to it.


Regards,
Lars-Inge Tønnessen
 
M

MikeY

Thanks for your reply Lars-Inge. Much appreciated it. But, I've been trying
your syntax and I'm still not getting it. Manyly how to use your given line
of code. I'm going to repost/ post a new question and see what happens.
Mainly cause I cannot see any from my Form1.cs (Classes, function, etc)

Thanks again

MikeY
 
B

Bruce Wood

If I understand you correctly, the usual design is as follows.

1. Expose an event in your custom control... something like ItemAdded
(I don't know about the name... I would have to know more about your
application). Give the event arguments that carry any information you
want to transmit (like what item should be added).

2. The form that hosts the custom control then handles the event.
Whenever your custom control raises an ItemAdded event, the hosting
form handles it, extracts which thing needs to be added, constructs a
list view item, and adds it to the list view.

The idea is that the custom control doesn't know about the ListView,
and the ListView doesn't know about the custom control. The control
simply raises an event when "something interesting" happens, while the
enclosing form is the one that knows what to do about that.

This allows you to create custom controls and place them on forms
without their having to know about their environment. It's the form
that knows how things are connected.
 

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