Adding Events to user control in Windows forms - c#

I am using a windows form in which i have a build a usercontrol with one textbox and one CheckedList control. What i wanted to do is i want to capture the ListIndexChanged event of CheckListbox control in my form where i use this usercontrol.
Please help me how to do this

You can add this
public event EventHandler SelectedIndexChanged
{
add { checkListBox.SelectedIndexChanged += value; }
remove { checkListBox.SelectedIndexChanged -= value; }
}
to your control and subscribe to this event from the form
take a look here

Related

C# Windows Forms detect BackColourChanged event

So I have a windows forms program I am trying to design and I want the draw panel to be able to change colour based on a colour selected from the built in ColorDialog.
I need to detect the firing of the draw panels BackColorChanged event and then have other code happen then. Can anyone tell me how to create a handler for this, feel I may be missing something simple but cant quite figure it out.
To be notified when the BackColorChanged event is fired, you can subscribe to the BackColorChanged event when initializing the form:
public class YourForm : Form
{
public YourForm()
{
InitializeComponents();
somePanel.BackColorChanged += SomePanel_OnBackColorChanged;
}
public void SomePanel_OnBackColorChanged(object sender, EventArgs e)
{
//Back color has changed, do something
}
}
If you want to change the backcolor of a Panel by choosing a color from a ColorDialog, you don't need no events from that panel.
Open the ColorDialog, wait for it to be closed with "OK" and set the color accordingly:
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
panel.BackColor = colorDialog1.Color;
}
That's what I understood. If you really need to use an event from the Panel, why don't you just use the event every WinForms Control offers: BackColorChanged. See Isma's answer for that.

binding custom control to an external button

I have a custom control that i'm using in an external application. I want to bind a click event of a random button in the application to add data to my control.
Is something like that even possible?? Basically what i was thinking was creating a property in my control that allows a developer to add a button control to it at design time. And then when the application is run, any clicks registered on the button will be forwarded to a method in the custom control to add data.
Is this doable? if so, can someone explain what needs to be done exactly?
You can create a property of type Button in your custom control. Then when you put an instance of custom control on the designer, for that property, you will see a dropdown that shows all Button instances on the form which you can select one of them. It's enough to add a method to click event of the button in setter of the property:
using System;
using System.Windows.Forms;
public class MyControl : UserControl
{
private Button addButton;
public Button AddButton
{
get { return addButton; }
set
{
if (addButton != null)
addButton.Click -= new EventHandler(addButton_Click);
addButton = value;
if (addButton != null)
addButton.Click += new EventHandler(addButton_Click);
}
}
void addButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Add Button Clicked!");
}
}
When you put it on designer, for AddButton property, a list o available buttons on the form will be shown. it's enough to choose one of them, then the behavior which you want (here in the example, showing the message box) will attach to click event of that button.

C# click on control

I'm trying to create a custom control which fires an even on click.
My control is just a panel with a couple of labels and a picturebox inside.
The click works perfectly, the only issue is that I have to click the background of the control and if I press on the picturebox, is not working.
I've added the on click event to the control, but I would like to press in every place of it to trigger the event, not just the background of the panel.
I thought about adding a transparent object that covers entirely the control. I actually don't like this idea, however, I've tried with a picturebox, but i cannot see through it. It's not transparent. I can just see the panel background but It covers the labels and the image.
Thanks for the support.
If you just have a couple of objects in your panel, you can hook the Click event of all objects it contains to the same event handler, there is nothing wrong doing this.
public class MyUserControl : UserControl
{
public event Action<MyUserControl> MyControlClick
public string ID {get; set;}
public MyUserControl()
{
InitializeComponents();
// The same event handler code will be used for the three controls
myPictureBox.Click += global_Click;
myLabel1.Click += global_Click;
myLabel2.Click += global_Click;
this.Click += global_Click;
}
void global_Click(object sender, EventArgs e)
{
if (MyControlClick != null)
MyControlClick(this);
}
}
If you have a more important amount of objects, you can rely on this answer to create a truly transparent panel that handles clicks. The drawback is that you will have to detect which object has been clicked by using HitTest based on the mouse location.
On the form side :
aControl.MyControlClick += aControl_MyControlClick;
// ...
// This code is triggered when a MyUserControl is clicked
void aControl_MyControlClick(MyUserControl ctl)
{
MessageBox.Show(ctl.ID);
}
Actually! You cannot raise any event to the element in the Usercontrol unless you have to apply own method to your usercontrol or you can disable the element in the usercontrol but it will change the color of that element but It will raise the click event when you click your control.

Interop WPF in WinForms, how to handle events from WPF control

I'm working with some WPF Interoperability in a WinForms application. I have the following set up.
WinForms UserControl WFControl
WPF UserControl GalleryControl
ListBox GalleryItems
ListBox ItemTemplate GalleryItem
Winforms hosting the GalleryControl, which has GalleryItems (ListBox) that has a ItemTemplate of GalleryItem.
Now in the WFControl I want to see when GalleryItems has it's SelectionChanged Event triggered.
My current attempts have tried to:
Handle the SelectionChanged Event in GalleryControl and have it raise a seperate public event that my winforms can read, but I can't handle the event like that since it's not a routed event. This would work if I could figure out how to handle that. applicable code:
public event ClaimGallery SelectedClaimChanged;
public ViewModels.InsuranceClaimViewModel ClaimViewModel { get; set; }
public int SelectedClaimID
{
get
{
return ((Models.InsuranceClaim) ClaimList.SelectedItem).ID;
}
}
public ClaimGallery()
{
InitializeComponent();
ClaimViewModel = new ViewModels.InsuranceClaimViewModel();
DataContext = ClaimViewModel;
ClaimList.ItemsSource = ClaimViewModel.InsuranceClaims;
ClaimList.SelectionChanged += ClaimSelectionChanged;
}
private void ClaimSelectionChanged(object sender, EventArgs e)
{
//This is the part that doesn't work
ClaimList.RaiseEvent(new RoutedEventArgs(SelectedClaimChanged, this));
}
I've also seen that I could potentially find the ListBox via some control tree browsing the subscribe to the actual event in the WFControl but I can't seem to figure how to do this in an interop'd control.
I have similar problems in my current project, and I'm solving it the way you describe.
The WPF controls re-raises a public (normal) event, that is then handled by the WinForms control.
Honestly I don't get the part where you are stating that is has to be routed in order to be handled by Winforms.
my winforms can read, but I can't handle the event like that since it's not a routed event
you use "+=" to handle this one ...

Arrow keys and changing control's focus hang the application

I have a usercontrol that contains a FlowLayoutPanel (topdown flow) with a bunch of radiobuttons. The control exposes a CheckedChanged event that fires whenever one of the radiobuttons's check changed.
My form contains the usercontrol and a textbox. I subscribe the usercontrol's CheckedChanged event and depending on which radiobutton gets checked, I either disable the textbox or put a focus inside the textbox.
All this works fine with mouseclick when changing the radiobutton's check state. However, this will hang indefinitely when using the arrow keys. I don't understand why the difference.
The following are steps to reproduce the behavior I'm seeing:
Create a usercontrol and drop a FlowLayoutPanel control and set its FlowDirection = TopDown. Then add two radiobuttons to the FlowLayoutPanel.
Provide an event handler in the usercontrol
public event EventHandler CheckedChanged
{
add { radioButton2.CheckedChanged += value; }
remove { radioButton2.CheckedChanged -= value; }
}
Create a windows form and drop the above user control. Add a textbox and set Enabled to False. Subscribe to the usercontrol's CheckedChanged event as follows
private void userControl11_CheckedChanged(object sender, EventArgs e)
{
textBox1.Select();
}
Run. Notice that if you use the mouse to click between the radiobuttons, thing works fine; but it will crash if you use the up/down arrow keys.
public event EventHandler CheckedChanged
{
add {
radioButton2.CheckedChanged += value;
}
remove {
radioButton2.CheckedChanged -= value;
}
}
Hmm, value is uninitialized? Or am I missing something?

Categories

Resources