I would like to know how to use the scroll event handler in Datagridview. I have a very big table of around 20 columns. It would fit in the screen.
So, when I scroll in the horizontal direction, when a particular column go out of focus, I need to call some function.
Any idea, how to achieve it ?
Something similar :
private void datagridview_Scroll(object sender, ScrollEventArgs e)
{
//If namecol go out of focus
//foo();
}
Here is what I did :
private void datagridview_Scroll(object sender, ScrollEventArgs e)
{
if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
{
if (e.NewValue > this.check1.Width/2 )
foo();
else
hoo();
}
}
Thanks
Related
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
C1List component is something similar to listBox. I have button and event for that button:
private void btnNavigateLeft_Click(object sender, System.EventArgs e)
{
if (lbRight.SelectedIndex != -1)
{
lbLeft.InsertItem();
lbRight.RemoveItem(lbRight.SelectedIndex);
}
}
Any idea how to do that?
Try this,
lst_2.Items.Add(lst_1.SelectedItem);
lst_1.Items.Remove(lst_1.SelectedItem);
I have a panel in winform. I want to capture both scroll and mouse wheel event for the panel. For both scenario I want to check the scroll bar position.
When scroll bar is at bottom (at the end of scrolling...) the control should fire the event.
I have done this for Panel.Scroll like this:
private void Panel1_Scroll(object sender, ScrollEventArgs e)
{
if (e.NewValue == Panel1.VerticalScroll.Maximum - Panel1.VerticalScroll.LargeChange+1)
{
//do some operation
}
}
But for MouseEventArgs there is no value (e.newvalue) to indicate scrollbar position.
How can I get the Scrollbar position from mouse wheel event ?
Also as per my requirement both event call have same logic implementation, so I want to write the logic once.
How can I achieve this ?
Well upon further analysis I checked out that panel1.VerticalScroll.Value
is equivalent to e.NewValue of ScrollEventArgs.
So for code re-usability below can be used:
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1_scrollcheck(e.NewValue);
}
private void panel1_MouseWheel(object sender, MouseEventArgs e)
{
panel1_scrollcheck(panel1.VerticalScroll.Value);
}
private void panel1_scrollcheck(int currPos)
{
if (currPos == panel1.VerticalScroll.Maximum - panel1.VerticalScroll.LargeChange+1)
{
//Put the logic here
}
}
Try to you VerticalScroll property of Panel.
void MouseWheel(object sender, MouseEventArgs e)
{
if (_panel.VerticalScroll.Value > _panel.VerticalScroll.Maximum - _panel.VerticalScroll.LargeChange)
MessageBox.Show("Bottom");
}
Whenever a user clicks the row header, which selects the whole row (and highlights it blue), the cell in the first column is actually entered. That is, if you start typing stuff, the text goes in that cell. I want to prevent this. Would it be possible to have no datagridview cells being entered when one or many rows are selected?
I also need the solution to prevent cells being entered during multiple row selections caused by clicking and dragging on the row headers.
Any ideas on how I could achieve this?
Thanks
Isaac
Set your DataGridView's ReadOnly property to true or false, depending on whether one or more rows have been selected or if the DGV's 'CellState' has changed.
Add the following two events for your DataGridView:
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e) {
if (e.StateChanged == DataGridViewElementStates.Selected) {
Console.WriteLine("TRUE");
dataGridView1.ReadOnly = true;
}
}
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) {
if (e.StateChanged == DataGridViewElementStates.Selected) {
Console.WriteLine("false");
dataGridView1.ReadOnly = false;
}
}
This worked for me in my tests but I wouldn't be surprised if there were hidden 'gotchas.'
An alternative solution may be something like this:
private void dataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && dataGrid.Rows[e.RowIndex].Selected)
return;
}
Based on the answer of "Jay R" I adjusted the code a bit to not lose readonly flags of cells.
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
e.Row.DataGridView.EditMode = DataGridViewEditMode.EditProgrammatically;
}
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.StateChanged == DataGridViewElementStates.Selected)
// adjust the edit mode to your "default" edit mode if you have to
e.Cell.DataGridView.EditMode = DataGridViewEditMode.EditOnEnter;
}
I'm confused. I am basically trying to tell when the user has clicked something in the listbox, held the button, and left the listbox. Here is a somewhat dumbed down version of what I am doing:
private bool itemHeld;
private void listOriginal_MouseDown(object sender, MouseEventArgs e)
{
itemHeld = true;
}
private void listOriginal_MouseUp(object sender, MouseEventArgs e)
{
itemHeld = false;
}
private void listOriginal_MouseLeave(object sender, EventArgs e)
{
if (itemHeld)
MessageBox.Show("OHH YEAH");
}
To me that seems like it should turn itemHeld true when you press the mousebutton, turn it false only if you lift it, and display ohh yeah if the value is true. If I break on the mouse down event to check the value, it is true and if I continue from there it displays the message. If I do not break, it does nothing. Is there something else at work here?
Edit:
Brief description: It would be difficult to explain what I am really trying to accomplish but imagine something almost like dragging a file off of a window. I need to simply be able to recognize when the user clicks inside of the listbox and then drags out of the listbox if that makes sense
You can not debug windows events by break point because when the Visual Studio get active to debug, the mouse leave event will be fired for the hovered control.
You can use Debug.WriteLine which writes information about the debug to the trace listeners.
private void button1_MouseLeave(object sender, EventArgs e)
{
Debug.WriteLine("Mouse leave");
}
private void button1_MouseEnter(object sender, EventArgs e)
{
Debug.WriteLine("Mouse enter");
}
private void button1_MouseHover(object sender, EventArgs e)
{
Debug.WriteLine("Mouse hover");
}
what about this?
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X > listBox1.Width - 1 || e.Y > listBox1.Height - 1 || e.X < 0 || e.Y < 0)
{
Console.WriteLine("drag out");
}
else
Console.WriteLine("mouse move {0}/{1}", e.X, e.Y);
}
it uses the fact that the Control is not left before the mousebutton is released ... but be aware that the drag out part will occur more than once so you probably will want to have a flag set the first time ... and have that flag cleared on mouse up or leave
For every mouse click, your MouseDown event will fire AND your MouseUp event will fire, so the sequence of operations is equivalent to
itemHeld = true;
itemHeld = false;
if(itemHeld)
MessageBox.Show("yay");
If you press the mouse button on the listbox and move the cursor out without releasing the button, switching focus to another window (e.g. Visual Studio) is what triggers the MouseLeave event to fire. This is why you're seeing the message box pop up when you're debugging.
I'm not sure what you're trying to accomplish, so I can't recommend another solution.