Click on dynamically added UserControls - c#

I was a stubborn WinForms protectionist for years, but I changed my mind and try to get in touch with WPF. I like it so far.
My problem:
I've created a UserControl "SelectableRectangle" which Contains a Rectangle. Well - in one of my application I create hundreds of them, programmatically/dynamically.
And I want to be able to "click" on them and use different tools, for example changing the color. Therefore I need to know in my main window on which exact SelectableRectangle I clicked.
Any ideas?

Basically, if your usercontrol implements an event handler you can link an event-handler to an event like that:
mycontrol1.Click += new RoutedEventHandler(mycontrol_Click);
and then implement the method
private void mycontrol(object sender, RoutedEventArgs e)
{
// do things here
}
Further, if your usercontrol is derived from either UIElement, UIElement3D or ContentElement you can use the AddHandler method:
mycontrol1.AddHandler(Button.ClickEvent, new RoutedEventHandler(mycontrol_Click))

Related

Access a ListView located in a UserControl from the MainForm in Winforms

I have an app that uses a side menu, and for each button (there are 3) on the left side menu, it changes the pages shown.
I tried doing it with multiple panels, but it's a nightmare to maintain in designer, and it's probably not a very good programming habit, I expect.
So I search and found what seemed to be a great idea: UserControl.
But as usual, it's not that simple (for a badly self-taught guy like me)
The general flow of the program is as follows:
a Btn_uc1_Check button that gathers informations and displays them in a uc1_ListView,
a Btn_uc2_Seek button that gathers informations on the net based on the uc1_ListView , and displays them on uc2_ListView,
a Btn_uc3_compile that compiles the info from uc2_ListView into a file,
a Clear button that clears the ListView depending on the UserControl on screen.
Now to the problem:
How on earth do I gain access to a ListView located in a UserControl to be able to read, clear, and add items from the MainFrom or from another UserControl?
I searched and honestly found nothing corresponding to what I needed?
Quite many questions.
You can gain access to any controls in UC. Just change the property "Modifiers" of the ListView in your UC to "Public".
Set that method to public. Do not use keyword "static". Each control
in your form is an instance of a class, not a static class actually. In the main form, create a button and double click on it in VS designer. A method will automatically generated, something like private void button1_Click. When the button is clicked, all of the code lines in button1_Click will run.
Create a public event handler of your user control, then pass the method in main to the handler.
So the UC class will be similar to this:
public event EventHandler button_UC_Click_handler;
public UserControl1()
{
InitializeComponent();
}
private void button_UC_Click(object sender, EventArgs e)
{
button_UC_Click_handler.Invoke(sender, e);
}
In main form:
public MainForm()
{
InitializeComponent();
userControl11.button_UC_Click_handler += UserControl11_button_UC_Click_handler;
}
private void UserControl11_button_UC_Click_handler(object sender, EventArgs e)
{
MessageBox.Show("You have clicked it!");
}
Good luck!

Which control supports Label with Close Button for windows forms

Is it possible to get a Text with Close button option for a windows Forms Application.Please let me know if we have any Dev Express control is available for the below functionality.
If you need this on across multiple interface. I would recommend you to create yourself an UserControl. (Or component if you prefer) Put a label and a pictureBox in the user control. Then implement the two ClickEvent needed.
public partial class UCTextWithImage : UserControl
{
public event EventHandler TextClick;
public event EventHandler ImgClick;
public UCTextWithImage()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (ImgClick != null)
ImgClick(sender, e);
}
private void label1_Click(object sender, EventArgs e)
{
if (TextClick != null)
TextClick(sender, e);
}
}
Once done, build you project, so the new userControl become available in form designer toolbox. Then drag and drop this UserControl into your interface. Finally bind and implement the two click event.
If you need this for only a few area, then the ButtonEdit is the closest to what you need.
https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraEditorsButtonEdittopic
In the properties, you have a button array. Remove the default one and add a new one of type glyph. Set the glyph you want. You can then use the ButtonClick event for the ImageClick and Click event for the LabelClick. However, in LabelClick, you need to check if underlying control isn't a button.
Finally, you need to change the appearance so the ButtonEdit so it become exactly like a label instead of Textbox.
To do so :
1- BackGroundColor must be transparent
2- BorderStyle set to None
Also, with devexpress, in order to have appearance set be taken into consideration, you need to remove the LookNFeel that overwrite everything...
So, it is possible but if you really want to, but userControl solution is easier.

Execute 'Form_Shown' event handler more than once?

I am currently developing a Windows app with several forms. I use Form_Shown in one of those forms to execute some code to initialize (refresh) the form before showing it.
In Form.Shown Event on MSDN, it states that the event is raised only the first time the form is shown. However, I want to be able to execute code to initialize my form every time that I call Form.Show() in some of the forms. Here's an example.
From a form named Game. Contains an event handler Game_Shown and a button that when clicked shows a form named Menu:
private void btnMenu_Click(object sender, EventArgs e)
{
this.Hide();
Formulaires.formMenu.Show();
}
private void Game_Shown(object sender, EventArgs e)
{
Code here...
this.Refresh();
}
From the form named Menu. Contains a button that when clicked shows the form named Game:
private void lblGame_Click(object sender, EventArgs e)
{
this.Hide();
Formulaires.formGame.Show();
}
It is behaving by design.
From the docs:
The Shown event occurs whenever the form is first shown.
Also, you should not handle the Shown event in your class, rather you should override OnShown.
To achieve what you want, you might try overriding the OnVisibleChanged method. Inside the method, if the form is visible, then execute your code.
Like the Shown event, you should not handle it in your form class, instead override the appropriate method:
From the docs:
The OnVisibleChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
What you want requires some detailed knowledge about which event happens when in the WinForm lifecycle. That may be documented somewhere, I don't know.
This is how I would find out:
create a small test project with 2 forms (Main and helper)
add the show and hide buttons and make sure it works.
Add Debug.Print("EventName") to all the candidate events of the helper form.
Look at the log in the output window and pick your event.
Candidate events would be FormClosing, FormClosed, (De)Activated, Enter, Leave, Load, ... go through the list.
When you find the right one, please post it here in an answer.

Windows Forms Binding: is there an event similar to DataBindingComplete, but fired when ALL bindings are completed?

I need to change a certain DataGridView's property (a DataSourceUpdateMode for one of its binding) only when ALL of its initial data bindings are completed.
I tried subscribing to the "DataBindingComplete" event, but it's fired too many times (one or more time for each binding associated to the control); what I need is a more global "AllDataBindingsComplete" event, fired when the control is ready to be displayed to the user.
As a temporary workaround, I'm using the MouseDown event (I've assumed that when the user is able to click the control, it means that the control is displayed... :) and the events I'm playing with - SelectionChanged - are fired after the MouseDown):
protected override void OnMouseDown(MouseEventArgs e)
{
Binding selectedItemsBinding = this.DataBindings["SelectedItems"];
if (selectedItemsBinding != null)
{
selectedItemsBinding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
}
base.OnMouseDown(e);
}
It works, but it smells like an ugly hack A LOT (and it's called too many times, only one time is enough for my needs).
Is there a better way?
(yes, I'm trying to adopt MVVM in a Windows Forms project, and I've added a bindable "SelectedItems" property to the DataGridView...)
What I've done at the Windows Forms form level, and may be improvised down to just the control(s) you want, is to subclass the Windows Forms baseclass into my own. Then, in its constructor, attach an extra event call to the Load() event.
So when everything else is completely loaded, only THEN will it hit my custom method (of the subclass). Since it is the bottom of the call-stack chain being attached to the event queue, I know it's last and everything else is done... Here's a snippet of the concept.
public class MyForm : Form
{
public MyForm()
{
this.Load += AfterEverythingElseLoaded;
}
private void AfterEverythingElseLoaded(object sender, EventArgs e)
{
// Do my own things here...
}
}
This concept can be applied to the Init() function too if that's more appropriate for your control... Let everything else within it get initialized(), then do you the "AfterInitialized()" function.

Design Time viewing for User Control events

I've create a WinForms control that inherits from System.Windows.Forms.UserControl...I've got some custom events on the control that I would like the consumer of my control to be able to see. I'm unable to actually get my events to show up in the Events tab of the Properties window during design time. This means the only way to assign the events is to programmatically write
myUserControl.MyCustomEvent += new MyUserControl.MyCustomEventHandler(EventHandlerFunction);
this is fine for me I guess but when someone else comes to use my UserControl they are not going to know that these events exist (unless they read the library doco...yeah right). I know the event will show up using Intellisense but it would be great if it could show in the properties window too.
Make sure your events are exposed as public. For example...
[Browsable(true)]
public event EventHandler MyCustomEvent;
A solution using delegate. For example i used for a custom ListView which handle item added event :
Declare your delegate :
public delegate void ItemAddedHandler(object sender, ItemEventArgs e)
then declare the event which use the delegate :
[Browsable(true)]
public event ItemAddedHandler ItemAdded;
Note : ItemEventArgs is a custom EventArgs
Hope can help you, works fine for me

Categories