954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accesing variable/function in Control using (Control)Sender

Hi,

I've created a partial class : Control for designing the shape of my polygon.
It works well but there's one problem that I tried to find on net but I get nothing.

I've created one int named 'size'. Then I created a function named 'Growing' for 'get' and 'set'.
When I created an object from that class, I just type on the object name.Growing, then I can 'get' the size and 'set' the size.
This no problem.

The problem exist when I created as follow:


private void Grow(object sender, EventArgs e)
{
Control controlAll = (Control)sender;

}

This 'controlAll' is use to track which polygon I choose.
but when I run this line of code, it seems like controlAll can't get to the function I declared in the class "Growing"

controlAll.Growing = 50; // ERROR WENT HERE

Is there any ways to make the control to identified the function that I created in the class ? Any idea ?

karhong
Newbie Poster
14 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

Sure, cast it to ShapeControl because Control class doesn't contain Growing property.

private void Grow(object sender, EventArgs e)
{
Shape controlAll = (Shape)sender; 
controlAll.Growing = 50;
}
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

your good idol

nevoj
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

but do you know how to access an object that had been done through a instance for example when i click a button it will create a list box then how can i get the selected item of that list box

nevoj
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Very easy, I'll write code, when user presses on the button, it creates object fron ListBox type and create an event for it also...

private void Grow(object sender, EventArgs e)
{
ListBox lstBoxForItems = new ListBox();
lstBoxForItems.Items.Add("1");
lstBoxForItems.Items.Add("2");
lstBoxForItems.Items.Add("3");
lstBoxForItems.Show();
this.Controls.Add(lstBoxForItems);
lstBoxForItems.SelectedIndexChanged += new EventHandler(lstBoxForItems_SelectedIndexChanged);
        }

        void lstBoxForItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(lstBoxForItems.SelectedItem.ToString());
        }
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You