Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom User Control and Events

Status
Not open for further replies.

danielkelly

IS-IT--Management
Aug 13, 2006
28
AU
Hi All,

I have created my first custom user control which is basically a CheckBox displayed as a button with a few labels on it. Now Im struggling a bit with understanding the concepts of how events should be working.

When I go into my User Control and click on the CheckBox, I created an event which fires on Checked_Changed which depresses the button and changes the color. This works perfectly when I use a standard Windows Checkbox as Ive used it in other projects but not when using my custom control. I drag a few of my controls onto the form and put Visual Studio in Debug mode to start at the CheckedChanged event to see what happens.

The problem appears to be that sometimes I have to click 4 or 5 times to make the event fire and sometimes it doesnt even fire.

Am I correct in assuming that I only need the event created in the custom control and not each instance of the control on the form, as eventually I will have about 50 of these controls on the form. When i look at the custom control on the main form it does not have a CheckedChanged event.

Thanks in advance,

Daniel.

Code Sample :-

private void chkButton_CheckedChanged(object sender, EventArgs e)
{
if (chkButton.Checked == true)
{
chkButton.BackColor = Color.Lime;
lblPager.BackColor = Color.Lime;
lblTimer.BackColor = Color.Lime;
}
else
{
chkButton.BackColor = SystemColors.Control;
lblPager.BackColor = SystemColors.Control;
lblTimer.BackColor = SystemColors.Control;
}
}
 
An update one the above. I realised that the code actually works perfectly but what I was missing was the design of the custom control..

I have a checkbox with 2 labels on it and it I click on the labels nothing happens, obviously as they have no events to fire when clicked.

Im not quite sure how to handle this as the label doesnt really know about the button.

Thanks,

Daniel.
 
Not sure if I understand you correctly. Do you want the checkbox to be checked/unchecked when either of the Label controls are clicked?

if yes you could do something like this:

Add an event handler method to handle when either label is clicked on, and check/uncheck the checkbox like so:
Code:
private void myLabels_Click(object sender, EventArgs e)
{
    myCheckbox.Checked = !myCheckbox.Checked;
}

Then just point the Labels click events to the handler method:

Code:
private void myCustomControl_Load(object sender, EventArgs e)
{
    Label1.Click += new EventHandler(myLabels_Click);
    Label2.Click += new EventHandler(myLabels_Click);
}
 
Daniel,

I've read this point about a dozen times and I can't quite figure out what you are asking. I may be way off but hopefully what follows will help you out.

1. your events work, but are not visible in the designer when you add the custom control to another container (form, user control, etc.). This would make sense all the designer friendly stuff has to do with attributes associated with your custom control. for example
Code:
[DesignerAttribute1("friendly name", ...)]
class MyControl : UserControl
{
   [DefaultProperty]
   public string Text {get;set;}

   [DefaultEvent]
   public event CheckboxChanged {add;remove;}

}
these attributes are fictitious The point is VS would scan your compiled code, recognize the attributes and be able to provide the GUI/Wizard stuff that most .net devs are used to. You can still access the public API from code.

2. you cannot access a control members from outside the control. example
Code:
class MyControl : UserControl
{
   protected CheckBox MyCheckBox;

   protected void Init()
   {
      MyCheckBox = new CheckBox();
      //defined checkbox properties
      container.Add(MyCheckBox);
   }
}

class MyForm : Form
{
  protected MyControl MyControl1;

  private void ButtonClicked(object sender, EventArgs e)
  {
      MyControl1.MyCheckBox.Checked = true;
  }
}
the idea of the custom control is to encapsulate the details of how it works. the Form should not need to know the internal workings of the control, only how to interact with the control. example
Code:
class MyControl : UserControl
{
   public string Text
   {
      get { return TheTextBox.Text; }
      set { TheTextBox.Text = value; }
   }

   public bool Active
   {
      get { return TheCheckBox.Checked; }
      set { TheCheckBox.Checked = value; }
   }
}

class MyForm : Form
{
  protected MyControl MyControl1;

  private void ButtonClicked(object sender, EventArgs e)
  {
      MyControl1.Active = true;
      MyControl1.Text = "Hello World";
  }
}
the same is true for functions and events. MyForm can interact with MyControl, but it doesn't know how MyControl uses the information, only that it does.

i used properties as an example. The same applies to functions and events.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I will try to explain a bit better what I am trying to do and how I am currently doing it

1) I have created a new CustomUserControl called PageButton

2) On this control i have placed the following items :-

Checkbox (chkButton) displayed as a button
Label (lblName)
Label (lblTimer)

3) What I am trying to do is when I click on the Control, the background color of the Checkbiox and labels will changes, the checkbox will reverse state (ie go from checked to unchecked and vica versa chaning colors) and the timer will start / stop.

The issue is when I setup code which should fire on the PageButton Click event, nothing happens and the code never runs. If i create code based on the individual items such as the chkButton_CheckedChangedEvent then I teh code runs. The problem with this one is when I click on the Label (which is part of the control) then the event doesnt fire. hence my original question of whether I need to create the code for each individual component of the control and not the control as a whole.

I hope this makes more sense.

Thanks,

Daniel.
 
so you want the event to fire when any part of the user control is clicked? I don't think there is a single event that would handle that. i could be wrong, my UI's are web pages. you would need to wire up the checkbox, user control and labels to execute. All the code will be contained within the control itself. if any public facing API is needed it would be to get/set the labels.

can you post the code you have? this will give us a better idea of what is happening.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top