CalendarViewDayItem event not firing - c#

I'm trying to add a RightTapped event to each CalendarViewDayItem. DoubleTapped event works fine, but RightTapped is not raised despite of that it's created.
This is how I have created them:
private void calviewSun_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
args.Item.DoubleTapped += CalendarViewDayItem_DoubleTapped;
args.Item.RightTapped += CalendarViewDayItem_RightTapped;
}
I have tried setting breakpoints to check if they were raised but they had any other problems, but they are never raised. I have check that args.Item.IsRightTapEnbaled is set to true.
I don't know why is not raising the event.

Please try to use UIElement.AddHandler() Method to add right tapped event for the CalendarViewDayItem.
The code looks like this:
private void CalendarView_CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
{
args.Item.IsRightTapEnabled = true;
args.Item.DoubleTapped += Item_DoubleTapped;
// args.Item.RightTapped += Item_RightTapped;
args.Item.AddHandler(UIElement.RightTappedEvent, new RightTappedEventHandler(Item_RightTapped), true);
}
private void Item_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
Debug.WriteLine("RightTapped");
}

Related

DataGridView.Click vs RowHeaderMouseClick

Just out of curiosity, how do you stop the DataGridView.Click event from stealing the "thunder" from the RowHeaderMouseClick event?
When I click the ROW HEADER, the DataGridView.Click event is fired.
When I DISABLE the DataGridView.Click event, the RowHeaderMouseClick event is fired, as desired.
Apparently, the ROW HEADER is part of the DataGridView--therefore, I guess, technically, it is performing as it should.
Instead of the Click event, you may use the MouseClick event, which will allow you to easily perform a HitTest() to get information about where the click has occurred.
Here's an example:
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
Console.WriteLine("RowHeaderMouseClick");
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.RowHeader) return;
// Row header clicks won't trigger the following line.
Console.WriteLine("MouseClick");
}
Technically, you could do the same thing with the Click event:
private void dataGridView1_Click(object sender, EventArgs e)
{
var cursorPos = dataGridView1.PointToClient(Cursor.Position);
var hitTestInfo = dataGridView1.HitTest(cursorPos.X, cursorPos.Y);
if (hitTestInfo.Type == DataGridViewHitTestType.RowHeader) return;
Console.WriteLine("Click");
}
But there's really no reason to since MouseClick already gives you the location of the cursor out of the box.

What is the event of a DataGridView for 'Highlighting a Row Number'

I have populated a datagrid but would like an event to fire when the user double clicks the row and highlights it. I have looked through all the properties but cannot seem to work out which one it is.
I've also looked on MSDN but also cannot see anything.
Many Thanks,
Sam
Try to use the code below:
this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
}
The problem with CellDoubleClick is it won't fire an event for keyboard users when navigating with arrows.
Instead, some other events you can handle are:
RowEnter
SelectionChanged
RowStateChanged
You can poke around when each fires by attaching handlers to each event and writing to the output window:
dataGridView1.RowEnter += dgv_RowEnter;
dataGridView1.SelectionChanged += dgv_SelectionChanged;
dataGridView1.RowStateChanged += dgv_RowStateChanged;
dataGridView1.CellContentClick += dgv_CellContentClick;
private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)
{
Debug.WriteLine("RowEnter");
}
private void dgv_SelectionChanged(object sender, EventArgs e)
{
Debug.WriteLine("SelectionChanged");
}
private void dgv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
Debug.WriteLine("RowStateChanged");
}
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Debug.WriteLine("CellContentClick");
}

Sometime Except Combo Box's SelectedIndexChanged

I have one Combo box and it has SelectedIndexChanged Event but i want Ignore that event in some case how can i achieve that functionality.
describe code is below
private void Form1_Load(object sender, EventArgs e)
{
List<string> lstString = new List<string>();
lstString.Add("One");
lstString.Add("Two");
lstString.Add("Three");
foreach (string str in lstString)
cBox.Items.Add(str);
//Here I want Ignore cbox_SelectedIndexChanged Event
cBox.SelectedIndex = 0;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}
You can choose either of the 2 approaches.
Have a bool flag which will be set for those conditions when you want to ignore the event handler from running. And use that flag inside your SelectedIndexChanged method
Subscribe to the event only after you have set cBox.SelectedIndex=0 if that is the only case.
Instead of subscribing the event within the designer (I expect you to do this at the moment) you can subscribe to the event in code after the initialization is done.
private void Form1_Load(object sender, EventArgs e)
{
// Init stuff
cBox.SelectedIndex = 0;
// Event subscription
cBox.SelectedIndexChanged += cBox_SelectedIndexChanged;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}

MouseUp Event Error

I am new to WinForms events and I am getting a strange error.Well I write when I start my control:
this.MouseUp += MouseUpMethod;
But the problem is, when I release the mouse button out of my control, the program recognize as I release the mouse over the Control. I am not able to understand this error.
Did ever someone got this error?
It's because, by default, your control captures mouse. Just set Control.Capture to false somewhere in your MouseDown event handler, for example:
void MouseDown(object sender, MouseEventArgs e) {
this.Capture = false;
}
As alternative just check in MouseUp that mouse is still inside your control:
void MouseUp(object sender, MouseEventArgs e) {
if (ClientRectangle.Contains(PointToClient(Cursor.Position))) {
// Your code here
}
}
see, you need to associate event with event handler just after your InitializeComponent()
public Form1()
{
InitializeComponent();
this.button2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button2_MouseUp);
}
then your event handler should be
private void button2_MouseUp(object sender, MouseEventArgs e)
{
//Do stuff here
}

Issue with LostFocus event of a TextBox

I'm trying to use this function:
private void IDCustTextBox_LostFocus(object sender, System.EventArgs e)
{
if (CustName.Text == "abc")
MessageBox.Show("Error");
}
When I type abc in CustName textbox, and then leave the textbox, I dont get any message. In the textbox properties I can see that "textbox.Changed" is using the event LostFocus.
How can I get this to show the Error message above?
There is no LostFocus event for textbox in property Window,if you want to use this then you must need to add event handler, there is textbox leave event in property window, that could be used as below:
private void textBox1_Leave(object sender, EventArgs e)
{
// do your stuff
}
for adding event handler you need to write the following:
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
then you can use it as below:
private void textBox1_LostFocus(object sender, EventArgs e)
{
// do your stuff
}
You will need to let the field know that there is a handler for the event LostFocus
Since this is not part of the properties window you will have attach the handler as such
CustTextBox.LostFocus += new EventHandler(IDCustTextBox_LostFocus);

Categories