There is a check box in the datetimepicker control of winforms .net.
But I could not find the event that is triggered when the check box is checked or unchecked .
Is there a way out?
It does however trigger the value changed event
You´ll have to store the old "checked" value in order to compare to the new one, so you´ll be able to determine if the "checked" state has changed:
bool oldDateChecked = false; //if it's created as not checked
private void dtp_filtro_date_ValueChanged(object sender, EventArgs e)
{
if (this.dtp_filtro_date.Checked != oldDateChecked)
{
oldDateChecked = this.dtp_filtro_date.Checked;
//do your stuff ...
}
}
Run into the same issue. I needed a CheckedChangedEvent on a winforms DateTimePicker control. So with the inspiration of the answers before me I created an inherited User Control named DateTimePicker2, inheriting from DateTimePicker that implements this event. It looks like it works but no guarantees.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyNameSpace
{
public partial class DateTimePicker2 : DateTimePicker
{
private bool _checked;
public new bool Checked
{
get
{
return base.Checked;
}
set
{
if (value != base.Checked)
{
base.Checked = value;
_checked = base.Checked;
OnCheckedChanged(new CheckedChangedEventArgs { OldCheckedValue = !value, NewCheckedValue = value });
}
}
}
public event CheckedChangedEventHandler CheckedChanged;
public DateTimePicker2()
{
InitializeComponent();
_checked = Checked;
}
protected virtual void OnCheckedChanged(CheckedChangedEventArgs e)
{
if (CheckedChanged != null) CheckedChanged(this, e);
}
private void DateTimePicker2_ValueChanged(object sender, EventArgs e)
{
if (Checked != _checked)
{
_checked = Checked;
CheckedChangedEventArgs cce = new CheckedChangedEventArgs { OldCheckedValue = !_checked, NewCheckedValue = _checked };
OnCheckedChanged(cce);
}
}
}
public delegate void CheckedChangedEventHandler(object sender, CheckedChangedEventArgs e);
public class CheckedChangedEventArgs : EventArgs
{
public bool OldCheckedValue { get; set; }
public bool NewCheckedValue { get; set; }
}
}
And off-course don't forget to subscribe to the DateTimePicker2_ValueChanged event from the designer.
The reason why I used both a new Checked property (to hide the base.Checked one) and a _checked field to keep truck of the old value, is because
the base.Checked property does not fire the ValueChanged event when changed programmatically and therefore needed a new property that could do that.
the this.Checked new property does not fire the ValueChanged event when changed from the UI and therefore needed a flag that would track the base.Checked property.
Basically a combination of both approaches was needed.
I hope this helps.
I know this is super old but this could help someone.
You can capture DataTimePicker.MouseUp event
private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
{
if (((DateTimePicker)sender).Checked)
{
//Do whatever you need to do when the check box gets clicked
}
else
{
//Do another stuff...
}
}
You will need to do the same with KeyUp event in order to get the Space key press that could also activate the checkbox.
Related
What I am attempting to do is create a complex control that has a picture box, track slider and numeric up down controls. In the parent form, when the user clicks on an image, then this composite control appears and the background color is then sent to it and the image in the control is then set with that background color. Then if the user clicks on the image on the composite control, the parent form is then notified of the click event and then subsequently removes that specific composite control from the parent form.
Composite Control code
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ctlClusterControlLib
{
public partial class UserControl1 : UserControl
{
private Color colImageBackground;
private int intThreadCount;
private PictureBox pictureBoxControl; // Compiler informs me that this is never assigned to and will always have its default value null.
private TrackBar trackBar; // Compiler informs me that this is never assigned to and will always have its default value null.
private NumericUpDown numericUpDown; // Compiler informs me that this is never assigned to and will always have its default value null.
private string strImageToolTip1;
private string strImageToolTip2;
private static object EventSubmitKey = new object();
public UserControl1()
{
InitializeComponent();
}
public Color ImageBackground
{
get { return colImageBackground; }
set { colImageBackground = value; Invalidate(); }
}
public int ThreadCount
{
get { return intThreadCount; }
set { intThreadCount = value; }
}
[
Category("Action"),
Description("Raised when the user clicks on the image.")
]
public event EventHandler PictureClick
{
add { Events.AddHandler(EventSubmitKey, value); }
remove { Events.RemoveHandler(EventSubmitKey, value); }
}
public event EventHandler TrackBarScroll
{
add { trackBar.Scroll += value; }
remove { trackBar.Scroll -= value; }
}
public event EventHandler numericUpDownChange
{
add { numericUpDown.ValueChanged += value; }
remove { numericUpDown.ValueChanged -= value; }
}
public string ImageToolTip1
{
get { return strImageToolTip1; }
set { strImageToolTip1 = value; }
}
public string ImageToolTip2
{
get { return strImageToolTip2; }
set { strImageToolTip2 = value; }
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
numericUpDown1.Value = trackBar1.Value;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
trackBar1.Value = Convert.ToInt32(numericUpDown1.Value);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Color c = Color.FromArgb(0xFF, colImageBackground);
pictureBox1.BackColor = c;
}
}
}
Parent Form CS relevant section:
private void newPictureBox_Click(object sender, EventArgs e)
{
UserControl1 _UserControl = new UserControl1();
PictureBox _PictureBox = (PictureBox)sender;
string _NewControlClusterName = "_New" + _PictureBox.Name;
_UserControl.Name = _NewControlClusterName;
_UserControl.ThreadCount = 16;
_UserControl.ImageBackground = _PictureBox.BackColor;
_UserControl.Dock = DockStyle.Top;
_UserControl.PictureClick += new EventHandler(ClusterControl_Click);
//_UserControl.TrackBarScroll += new EventHandler(GetTartanCode);
panel3.Controls.Add(_UserControl);
panel3.Controls.SetChildIndex(_UserControl, 0);
}
And I am having intermittent issues with raising the click event to the parent form using this control.
I have tried everything I can find in Google and Stack Overflow with no joy. My questions are this:
Am I even in the right ballpark?
Is this something that needs to be coded in the parent form cs file?
Is this something that needs to be reconfigured in the composite control cs file?
Is this something that needs to be configured in both files?
I believe I have a solution.
What I was not doing was directly assigning the request to the control I wanted to register the event for. Instead I was assigning it to a new control and therefore nothing would happen.
public event EventHandler PictureClick
{
add { pictureBox1.Click += value; }
remove { pictureBox1.Click -= value; }
}
And so far, It works every time.
I read some about DataBinding, mostly complicated things like SQL or whatever XAML and stuff.
All I want my programm to do is, if the "value" of a variable changes just write it in a textbox or label. (using WindowsForms)
So far what I have:
namespace DataBinding_Test
{
public partial class Form1 : Form
{
BindingSource bs = new BindingSource();
Class1 test = new Class1();
public Form1()
{
InitializeComponent();
test.name = "Hello";
bs.DataSource = test;
label1.DataBindings.Add(new Binding("Text", bs, "name", false, DataSourceUpdateMode.OnPropertyChanged));
}
private void button1_Click(object sender, EventArgs e)
{
test.name = textBox1.Text;
}
}
}
Class1 just has a public property name. On startup lable1 will show my "Hello" string. Then on button click the name property will change. On debug I saw the actual DataSource of "bs" contains the new property value, but the label will not show anything...
Is there any realtivly easy way to do this?
The Backround is: periodically there will be a polling of sensor data throug RS232. If the value of one sensor changes I want to show this in label or textbox. Now a backroundthreaded timer will need invokes and stuff to access the GUI thread; thought this would be easier with databinding but seems not :P
Thanks to all, great site, great work! :)
Another way to make things work without implementing INotifyPropertyChanged
class Class1
{
private string name;
public string Name
{
get { return name; }
set
{
//Check if you are assigning the same value. Depends on your business logic
//here is the simplest check
if (Equals(name, value))
return;
name = value;
OnNameChanged();
}
public event EventHandler NameChanged;
protected virtual void OnNameChanged()
{
var handler = NameChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
}
The trick is to have event with the name combined by name of property and Changed suffix and to raise it whenever value of your property is changed
In order your code would work you should implement INotifyPropertyChanged interface in your binded class. Without it your binding simply doesn't know, when the change occures. There you should implenent the logic, according to which you would notify your subscribers about which when something changed in your class (the setter part) and what has changed (PropertyChangedEventArgs). See example for your class:
class Class1: INotifyPropertyChanged
{
private string name = "";
public string Name
{
get { return name; }
set { name = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged()
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
And change the property name from "name" to "Name" in your binding:
label1.DataBindings.Add(new Binding("Text", bs, "Name", false, DataSourceUpdateMode.OnPropertyChanged));
// create winforms project on form1 drag a textbox (testbox1)
// and a button (button1) with a button click event handler
// this updates the textbox when the button is clicked
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
MyClass Myobj = new MyClass();
public Form1()
{
InitializeComponent();
/* propertyname, datasource, datamember */
textBox1.DataBindings.Add("Text", Myobj, "Unit");
}
public class MyClass : INotifyPropertyChanged
{
private int unit = 3;
/* property change event */
public event PropertyChangedEventHandler PropertyChanged;
public int Unit
{
get
{
return this.unit;
}
set
{
if (value != this.unit)
{
this.unit = value;
NotifyPropertyChanged("Unit");
}
}
}
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Myobj.Unit += 4;
}
}
}
I created an extension method for this that I would like to share
Usage
private void Form1_Load(object sender, EventArgs e)
{
ResultLabel.Bind(NameTextBox);
WarningLabel.Bind(NameTextBox,i => i.Length == 0 ? "field required!" : "");
SendButton.Bind(NameTextBox, i => SendButton.Enabled = !(i.Length == 0));
}
Extension
public static class Extention
{
public static void Bind(this Control owner, Control dataSource)
{
List<EventInfo> fields = dataSource.GetType().GetEvents().ToList();
int index = fields.FindIndex(item => item.Name == "TextChanged");
if (index >= 0)
{
Control sender = dataSource as Control;
owner.Text = dataSource.Text;
dataSource.TextChanged += delegate (Object o, EventArgs e) { owner.Text = sender.Text; };
}
}
public static void Bind(this Control owner, Control dataSource, Func<string,string> onChange)
{
List<EventInfo> fields = dataSource.GetType().GetEvents().ToList();
int index = fields.FindIndex(item => item.Name == "TextChanged");
if (index >= 0)
{
Control sender = dataSource as Control;
owner.Text = onChange(sender.Text);
dataSource.TextChanged += delegate (Object o, EventArgs e) { owner.Text = onChange(sender.Text); };
}
}
public static void Bind(this Control owner, Control dataSource, Action<string> onChange)
{
List<EventInfo> fields = dataSource.GetType().GetEvents().ToList();
int index = fields.FindIndex(item => item.Name == "TextChanged");
if (index >= 0)
{
Control sender = dataSource as Control;
onChange(sender.Text);
dataSource.TextChanged += delegate (Object o, EventArgs e) { onChange(sender.Text); };
}
}
}
I'm not sure if that is what you want but you can can write whatever you variable contains into the Textbox or Label by using the control.Text property.
textBox1.Text ="Some other Text"
or
string variable = "Hello 2";
textBox1.Text = variable;
Why dou you want to use Databinding? Its mutch easier this way.
Say I have two user controls and I want to remove an event handler from one instance of the control.
To illustrate I've just made it a button as user control:
public partial class SuperButton : UserControl
{
public SuperButton()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.BackColor = Color.CadetBlue;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackColor = Color.Gainsboro;
}
}
I've added two super buttons to the form and I want to disable the MouseEnter event firing for SuperButton2.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
superButton2.RemoveEvents<SuperButton>("EventMouseEnter");
}
}
public static class EventExtension
{
public static void RemoveEvents<T>(this Control target, string Event)
{
FieldInfo f1 = typeof(Control).GetField(Event, BindingFlags.Static | BindingFlags.NonPublic);
object obj = f1.GetValue(target.CastTo<T>());
PropertyInfo pi = target.CastTo<T>().GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
EventHandlerList list = (EventHandlerList)pi.GetValue(target.CastTo<T>(), null);
list.RemoveHandler(obj, list[obj]);
}
public static T CastTo<T>(this object objectToCast)
{
return (T)objectToCast;
}
}
The code runs but it doesn't work - the MouseEnter and Leave events still fire. I'm looking to do something like this:
superButton2.MouseEnter -= xyz.MouseEnter;
Update: Read this comments questions...
In your case, you don't need to remove all event handlers at once, just the specific one you're interested in. Use -= to remove a handler in the same way you use += to add one:
button1.MouseEnter -= button1_MouseEnter;
Why not just set superButton2.MouseEnter = null;? That should do the trick until somewhere MouseEnter is assigned a value.
Just for an update, another way to handle it, and perfectly legal :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace TestControls
{
class SimpleButton:Button
{
public bool IgnoreMouseEnter { get; set; }
public SimpleButton()
{
this.IgnoreMouseEnter = false;
}
protected override void OnMouseEnter(EventArgs e)
{
Debug.Print("this.IgnoreMouseEnter = {0}", this.IgnoreMouseEnter);
if (this.IgnoreMouseEnter == false)
{
base.OnMouseEnter(e);
}
}
}
}
I have a simple datagrid, that displays contents of some List variable. I have two buttons related to it, one for adding contents to the List variable and refreshing the datagrid, the other for removing it and refreshing also.
I can add the objects without problems, however, if I change the selected row, I get an error saying "Index -1 does not have a value." No idea why.
The code for setting up the datagrid is as follows:
allTravellersDataGrid.DataSource = allTravellers;
where allTravellers is the List I was talking about above.
The button for adding the content to allTravellers List is as follows:
private void addAttendee_Click(object sender, EventArgs e)
{
if (attendeeName.Text == "" || attendeeSurname.Text == "" || attendeeBirthDate.Text == "" || attendeeIdNumber.Text == "")
{ MessageBox.Show("Not all information regarding the attendee entered"); }
else
{
allTravellers.Add(dt.prepareTraveller(attendeeName.Text, attendeeSurname.Text, attendeeBirthDate.Text, attendeeIdNumber.Text));
allTravellersDataGrid.DataSource = null;
allTravellersDataGrid.DataSource = allTravellers;
allTravellersDataGrid.Refresh();
}
}
and at last the code for button for removal of objects from the list looks like this:
private void removeAttendee_Click(object sender, EventArgs e)
{
traveller travellerToRemove = (traveller)allTravellersDataGrid.CurrentRow.DataBoundItem;
allTravellers.Remove(travellerToRemove);
allTravellersDataGrid.Refresh();
}
Is there anybody here who's not helpless like me in finding out what is causing the index problem?
Thanks.
I suspect that when you get this error, you are not currently on a selected item. (List controls that don't have an item selected indicate that with a '-1'). Checking for a valid item before you try to remove it will probably solve your problem.
I also used a BindingList, not sure what difference it will make for you.
assume a form with a
textbox named attendeeName
add button named addBtn
remove button named removeBtn
grid named allTravellersDataGrid
This works:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using StackOverFlowWinForms.Model;
namespace StackOverFlowWinForms
{
public partial class Form1 : Form
{
private BindingList<Traveller> _allTravellers = new BindingList<Traveller>();
public BindingList<Traveller> allTravellers { get { return _allTravellers; } }
public Form1()
{
InitializeComponent();
allTravellers.Add(new Traveller("Fred"));
allTravellers.Add(new Traveller("George"));
allTravellers.Add(new Traveller("Sam"));
allTravellers.Add(new Traveller("Mary"));
this.allTravellersDataGrid.DataSource = allTravellers;
}
private void addBtn_Click(object sender, EventArgs e)
{
if (attendeeName.Text == "")
{ MessageBox.Show("Not all information regarding the attendee entered"); }
else
{
allTravellers.Add(new Traveller(attendeeName.Text));
}
}
private void removeBtn_Click(object sender, EventArgs e)
{
if (allTravellersDataGrid.CurrentRow != null)
{
Traveller travellerToRemove = (Traveller)allTravellersDataGrid.CurrentRow.DataBoundItem;
allTravellers.Remove(travellerToRemove);
}
}
}
}
using System;
using System.ComponentModel;
namespace StackOverFlowWinForms.Model
{
public class Traveller
{
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
private string _attendeeName;
public string attendeeName
{
get
{
return _attendeeName;
}
set
{
_attendeeName = value;
NotifyPropertyChanged("attendeeName");
}
}
public Traveller()
{
this.attendeeName = "Unknown";
}
public Traveller(string name)
{
this.attendeeName = name;
}
}
}
I am trying to do something when double clicking an item in a ListBox. I have found this code for doing that
void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
MessageBox.Show(index.ToString());
//do your stuff here
}
}
However, when i click on an item, the event isn't fired. The event is fired if i click in the ListBox below all the items.
I set the DataSource property of the ListBox to IList<MyObject>.
Any ideas?
Tried creating a form with a ListBox with MouseDown and DoubleClick events. As far as I can see, the only situation, when DoubleClick won't fire, is if inside the MouseDown you call the MessageBox.Show(...). In other cases it works fine.
And one more thing, I don't know for sure, if it is important, but anyway. Of course, you can get the index of the item like this:
int index = this.listBox1.IndexFromPoint(e.Location);
But this way is fine as well:
if (listBox1.SelectedItem != null)
...
Works for me, so I assume there might be something about the items in the list (custom? intercepting the event?) or the event is not properly wired up.
For example try this (complete Form1.cs):
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public class MyObject {
public MyObject(string someValue) {
SomeValue = someValue;
}
protected string SomeValue { get; set; }
public override string ToString() {
return SomeValue;
}
}
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var list = new List<MyObject> {
new MyObject("Item one"), new MyObject("Item two")
};
listBox1.DataSource = list;
}
private void listBox1_DoubleClick(object sender, EventArgs e) {
Debug.WriteLine("DoubleClick event fired on ListBox");
}
}
}
With the designer source file (Form1.Designer.cs) containing this:
private void InitializeComponent() {
this.listBox1 = new System.Windows.Forms.ListBox();
... // left out for brevity
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
As a test, create a new Forms base application through the templates, then add just the ListBox and define a class MyObject. See whether you observe the same or a different behavior.
Thank you for all replies. It now works. I solved it, like 26071986 said, with handling double click in the MouseDown handler by checking if e.Clicks is 1. If so, I call DoDragDrop, if not, I call the method that handles double click.
private void MouseDown_handler(object sender, MouseEventArgs e)
{
var listBox = (ListBox) sender;
if (e.Clicks != 1)
{
DoubleClick_handler(listBox1.SelectedItem);
return;
}
var pt = new Point(e.X, e.Y);
int index = listBox.IndexFromPoint(pt);
// Starts a drag-and-drop operation with that item.
if (index >= 0)
{
var item = (listBox.Items[index] as MyObject).CommaDelimitedString();
listBox.DoDragDrop(item, DragDropEffects.Copy | DragDropEffects.Move);
}
}
Here's what I used in the MouseDoubleClick event.
private void YourMethodForDoubleClick(object sender, MouseButtonEventArgs e)
{
Type sourceType = e.OriginalSource.GetType();
if (sourceType != typeof(System.Windows.Controls.TextBlock)
&& sourceType != typeof(System.Windows.Controls.Border))
return;
//if you get here, it's one of the list items.
DoStuff();
...
}
John: then it works. But i figured out that the event isn't fired because I am also handling the MouseDown event. I tried to remove the MouseDown handling, and then it works. Is there a smooth way to handle both those events? If not, I just have to find some other way to catch a double click through the MouseDown event.