Accesing variable/function in Control using (Control)Sender

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2006
Posts: 14
Reputation: karhong is an unknown quantity at this point 
Solved Threads: 0
karhong karhong is offline Offline
Newbie Poster

Accesing variable/function in Control using (Control)Sender

 
0
  #1
Jul 21st, 2008
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 ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Accesing variable/function in Control using (Control)Sender

 
0
  #2
Jul 21st, 2008
Sure, cast it to ShapeControl because Control class doesn't contain Growing property.
  1. private void Grow(object sender, EventArgs e)
  2. {
  3. Shape controlAll = (Shape)sender;
  4. controlAll.Growing = 50;
  5. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: nevoj is an unknown quantity at this point 
Solved Threads: 0
nevoj's Avatar
nevoj nevoj is offline Offline
Newbie Poster

Re: Accesing variable/function in Control using (Control)Sender

 
0
  #3
Aug 13th, 2008
your good idol
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 3
Reputation: nevoj is an unknown quantity at this point 
Solved Threads: 0
nevoj's Avatar
nevoj nevoj is offline Offline
Newbie Poster

Re: Accesing variable/function in Control using (Control)Sender

 
0
  #4
Aug 13th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Accesing variable/function in Control using (Control)Sender

 
0
  #5
Aug 15th, 2008
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...
  1. private void Grow(object sender, EventArgs e)
  2. {
  3. ListBox lstBoxForItems = new ListBox();
  4. lstBoxForItems.Items.Add("1");
  5. lstBoxForItems.Items.Add("2");
  6. lstBoxForItems.Items.Add("3");
  7. lstBoxForItems.Show();
  8. this.Controls.Add(lstBoxForItems);
  9. lstBoxForItems.SelectedIndexChanged += new EventHandler(lstBoxForItems_SelectedIndexChanged);
  10. }
  11.  
  12. void lstBoxForItems_SelectedIndexChanged(object sender, EventArgs e)
  13. {
  14. MessageBox.Show(lstBoxForItems.SelectedItem.ToString());
  15. }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC