Windows Develop Bookmark and Share   
 index > Windows Forms General > how to control in which control i clicked.
 

how to control in which control i clicked.

i created a button using usercontrol
At run time i call the usercontrol twice, meaning i have 2 usercontrol with 1 button respectively.


But how could i identify which button i clicked on the usercontrol? the button in 1st usercontrol i called or button of 2nd usercontrol i called. how to diffrentiate them?


tiau  Thursday, October 08, 2009 7:54 AM
Well, it's created (IOW, the constructor code runs) after the line:

myControl mc = new myControl();

and it's loaded (IOW, the Load event has fired) after the line:

flowLayoutPanel1.Controls.Add (mc);

So now the question is, what properties do you need to access before it's loaded? Whichever properties you need to access before the load event fires should be initialized in the constructor ... place the code after the call to InitializeComponent().

Does this help?
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
  • Marked As Answer bytiau 10 hours 56 minutes ago
  •  
BonnieB  11 hours 9 minutes ago

Are you hooking both of your user control instances up to the same click event handler?  If so, then all you need to do to check which one fired the event would be something like:

private void userControl_Click(object sender, EventArgs e)
{
  if (sender == userControl1)
  {
    // You clicked userControl1
  }
  else if (sender == userControl2)
  {
    // You clicked userControl2
  }
}
Daniel Smith  Thursday, October 08, 2009 10:00 AM

Are you hooking both of your user control instances up to the same click event handler?  If so, then all you need to do to check which one fired the event would be something like:

private


 void


 userControl_Click(object


 sender, EventArgs e)
{
  if


 (sender == userControl1)
  {
    // You clicked userControl1



  }
  else


 if


 (sender == userControl2)
  {
    // You clicked userControl2



  }
}
but clicking on the button which place on the usercontrol, so how the usercontrol_click could be fired?
Nise1990  Thursday, October 08, 2009 10:16 AM
The form knows the difference between the controls.  You'll need to raise your own event in the button's Click event handler so that the form can do the right thing.

Hans Passant.
nobugz  22 hours 36 minutes ago
The form knows the difference between the controls.  You'll need to raise your own event in the button's Click event handler so that the form can do the right thing.

Hans Passant.

how if i call the usercontrol at the run time?

yup i can raise my own event in the button's clcik event BUT when at run time, as i add the usercontrol twice into the form, how could i differentiate which button at which UserControl that i clicking on?
tiau  22 hours 28 minutes ago
I don't know what "call the usercontrol" means.  Explain why this is important, why does a user control need to know that any other user control is present?

Hans Passant.
nobugz  21 hours 36 minutes ago
I don't know what "call the usercontrol" means.  Explain why this is important, why does a user control need to know that any other user control is present?

Hans Passant.

Thanks for your reply.
call the usercontrol is something as below: (sorry for my poor definition)
say i got a flowlayoutpanel, which i use it to create control (here i refer to UserControl) at run time.

In my UserControl, basically i have a chart and a textbox. when i click on the chart the textBox will be updated with the coordinate as well as the title of the chart with the help of the chart_click event
At run time when i call the multiple charts (means multiple userControl) so when i click on the one of the chart say it's 3rd chart (3rd UserControl) so how could the textBox to be filled with the 3rdchart title and its coordinate?



tiau  21 hours 25 minutes ago
This doesn't make sense.  When you have 3 user controls, you'll have 3 text boxes, not one.

Hans Passant.
nobugz  20 hours 8 minutes ago
This doesn't make sense.  When you have 3 user controls, you'll have 3 text boxes, not one.

Hans Passant.

yup.

@ T  ===> UserControl

where @ = graph ; T = textBox.    

--------
@1  T    

@2  T

@3  T
---------

Is it possible when i click on @2, , T will be updated with title of @2 and its coordinate of clicking on the graph?
when i click on @3, , T will be updated with title of @3 and its coordinate of clicking on the graph?
tiau  19 hours 53 minutes ago

In the designer for the user control (not the form that it goes on) just double click the button and put in your code to set the text of the text box to whatever you want.

When you add the user control you've created to the form, each user control will know to use their own controls.  e.g. the button in your first user control will only be aware of the text box in that control.  Same goes goes for any additional instances of the user control you create - they only know about their own controls.  Hope this makes sense!

Daniel Smith  18 hours 56 minutes ago

In the designer for the user control (not the form that it goes on) just double click the button and put in your code to set the text of the text box to whatever you want.

When you add the user control you've created to the form, each user control will know to use their own controls.  e.g. the button in your first user control will only be aware of the text box in that control.  Same goes goes for any additional instances of the user control you create - they only know about their own controls.  Hope this makes sense!


how about at run time?
tiau  18 hours 53 minutes ago
Is it possible when i click on @2, , T will be updated with title of @2 and its coordinate of clicking on the graph?
when i click on @3, , T will be updated with title of @3 and its coordinate of clicking on the graph?
He is talking about runtime.  You haven't made this clear, there are 3 T's.  Which of those 3 T's should be updated with the coordinate?

Pre-empting this, there should be only one T.  It should be a separate TextBox on the form, not part of the UC.  You'll need to raise an event in your UserControl so that the form can update T.

Hans Passant.
nobugz  18 hours 15 minutes ago
Is it possible when i click on @2, , T will be updated with title of @2 and its coordinate of clicking on the graph?
when i click on @3, , T will be updated with title of @3 and its coordinate of clicking on the graph?
He is talking about runtime.  You haven't made this clear, there are 3 T's.  Which of those 3 T's should be updated with the coordinate?

Pre-empting this, there should be only one T.  It should be a separate TextBox on the form, not part of the UC.  You'll need to raise an event in your UserControl so that the form can update T.

Hans Passant.

Each UC has T itself.

Could you please guide me ...

At designed time, i could place the UC from the toolBox so in my form i could have my object e.g myControl1, and i could access my UC's object using myControl1.XXX
But if i call the UC at run time, how could i pre-define my object in my form first?

I think my way of understanding of using UC is wrong... please guide.
tiau  12 hours 30 minutes ago
The problem with that is that i might need to access some properties of the UC after it's being created but before it's being loaded(where i call it at run time) thus, i might get in trouble as the control might not be initialized when i access it
tiau  12 hours 10 minutes ago
Again, what some of us are having trouble understanding, is what you mean by "call the UC at run time". How about this: could you post some relevant code (not everything, just the relative snippets) and maybe then we can understand what you're doing and what you're asking.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
BonnieB  12 hours 6 minutes ago
Again, what some of us are having trouble understanding, is what you mean by "call the UC at run time". How about this: could you post some relevant code (not everything , just the relative snippets) and maybe then we can understand what you're doing and what you're asking.
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com

Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class myControl : UserControl
    {
        public myControl()
        {
            InitializeComponent();
        }

        public string Text1
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }


    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAddUC_Click(object sender, EventArgs e)
        {
            myControl mc = new myControl();
            flowLayoutPanel1.Controls.Add (mc); // here is what i called adding UC at run time...
        }



    }
}
The problem with that is that i might need to access some properties of the UC after it's being created but before it's being loaded(where i call it at run time) thus, i might get in trouble as the control might not be initialized when i access it
tiau  11 hours 51 minutes ago
Well, it's created (IOW, the constructor code runs) after the line:

myControl mc = new myControl();

and it's loaded (IOW, the Load event has fired) after the line:

flowLayoutPanel1.Controls.Add (mc);

So now the question is, what properties do you need to access before it's loaded? Whichever properties you need to access before the load event fires should be initialized in the constructor ... place the code after the call to InitializeComponent().

Does this help?
~~Bonnie Berent [C# MVP]

(new blog --- not many posts yet --- be patient)
geek-goddess-bonnie.blogspot.com
  • Marked As Answer bytiau 10 hours 56 minutes ago
  •  
BonnieB  11 hours 9 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• VB.NET2003 - Items in ListBox are invisible
• Word automation
• Notification to Flash on taskbar in Vb.net
• 2005 Internet Explorer Wrapper
• How do I do LVM_SETWORKAREAS in a ListView Form?
• Passing information between two mdi child forms
• locating objects
• Excel-like control
• Custom drawn combo box
• ToolBar color and Height problem