Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to do this, but I don't know how:

In Form1 I have a custom user control "City".
When I click on a square inside City control, a new control is appearing, named "BuildingsPage". From this BuildingsPage, I click on a building.
BuildingsPage is closed, and in square, the building I choosed appear.

Can you give me an example code?
Or just explain.
Thank you.

What I have tried:

i will submit if necessary.i will submit if necessary.
its a mess anyway.
Posted
Updated 16-Dec-16 3:38am
v2

1 solution

Add an event to the BuildingsPage control, that the City control handles. When the BuildingsPage is clicked, it sets a property to show which building it is, and raises the event.
The City Control handles the event, retrieves the building via the property, and closes the BuildingsPage control.
It's the same process as here: Transferring information between two forms, Part 2: Child to Parent[^] but using controls instead of forms. City is the Parent, BuildingsPage is the Child.
 
Share this answer
 
Comments
_Q12_ 16-Dec-16 9:39am    
Thank you mister OriginalGriff ! And happy Christmas !
I think I learn this trick from you a long time ago, but now, after a bit of pause, I just remember it. I will try your original code too. Now I am happy that I make it work and is clean enough. Oh, if you can improve over it, please do it !!!
As always, thank you!

my solution:
//this is the square that I click
void Point_x_MouseClick(Object sender, EventArgs e)
{
buildingsPage1.blueprint_Building1.Click +=new EventHandler(blueprint_Building1_Click);
}

//this is the selected building from the list !
//I did it specific right now to find the way of doing it.
void blueprint_Building1_Click(Object sender, EventArgs e)
{
buildingsPage1.Visible = false;
point_1.BackColor = Color.Blue;
}
OriginalGriff 16-Dec-16 9:41am    
No, that just adds an event handler every time they click on it: so if they do it a dozen times, it will execute the handler a dozen times!

And a very Happy New Year to you and yours as well!
_Q12_ 16-Dec-16 10:25am    
very funky but is working !
Now I avoided making an event every time, from that other event.
Boy, it is hard to explain this ....

My correct thing: (i hope)
public City()
{
InitializeComponent();

...

//for some strange reason I must specifically iterate manually and assign
buildingsPage1.blueprint_Building1.Click+=new EventHandler(blueprint_Building1_Click);
buildingsPage1.blueprint_Building2.Click += new EventHandler(blueprint_Building2_Click);
buildingsPage1.blueprint_Building3.Click += new EventHandler(blueprint_Building3_Click);
}


void Point_x_MouseClick(Object sender, EventArgs e)
{
Point_ Point_x = (Point_)sender;
Point_x.BackColor = Color.Red;
ghostPoint = Point_x;
string c = Point_x.Name = ghostPoint.Name;
buildingsPage1.Visible = true;

// buildingsPage1.blueprint_Building1.Click +=new EventHandler(blueprint_Building1_Click);
}



Point_ ghostPoint = new Point_(); //<<<<<<

void blueprint_Building1_Click(Object sender, EventArgs e)
{
buildingsPage1.Visible = false;
foreach (Control ctrl in Controls)
{
if (ctrl.Name == ghostPoint.Name)
{
ctrl.BackColor = Color.Blue; //and is working ! yay
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900