ZedGraph: Check which data point is clicked on line graph? - c#

I am working on a windows form app with a zedgraph and a datagridview. The datagridview has a row for every point in the line graph, and when the user clicks on a point in the graph I want it to highlight the equivalent row in the datagridview.
So how can I find out which point the user has clicked? (I don't need any code for datagridview part).

I figured it out. You can use GraphPane.FindNearestObject to find the point that was clicked.
It seems that nearestObject is null if you do not click a point and is of type LineItem if you do, and then index will tell you which point was clicked.
private void zedGraphControl_MouseClick(object sender, MouseEventArgs e)
{
object nearestObject;
int index;
this.zedGraphControl.GraphPane.FindNearestObject(new PointF(e.X, e.Y), this.CreateGraphics(), out nearestObject, out index);
if (nearestObject != null && nearestObject.GetType() == typeof(LineItem))
{
// 'index' is the index of that data point
dataGridView.CurrentCell = dataGridView.Rows[index].Cells[0];
}
}

Related

C# WPF Syncfusion Selected row doesnt highlight when row is moved to top

I am using Syncfusion WPF Datagrid to display some data. I have implemented features to move selected rows up, down, to the top, and to the bottom of the Datagrid. When the row is moved the row should still be selected, this works for up, down, and to bottom but for some reason does not work for moving the row to the top of the data grid.
I have provided the code below and I should point out that the line of code to keep the selected row highlighted is the same for up, down, and to bottom.
public void PriorityMoveTop()
{
Job? SelectedRow = (Job)this.jobListView.SelectedItem;
if (SelectedRow == null)
{
}
else
{
Job? myJobAfterSelectedRow = (from x in Memory.Jobs where x.Priority == (SelectedRow.Priority - 1) select x).FirstOrDefault();
if (jobListView.SelectedIndex == 0)
{
}
else
{
foreach (Operation operation in SelectedRow.Operations)
{
operation.Priority = SelectedRow.Priority;
}
OrderPriorityColumnDown();
SelectedRow.Priority = 1;
}
}
SetDataGridJobList();
if (SelectedRow == null)
{
}
else
{
//Code to highlight row
jobListView.SelectedIndex = SelectedRow.Priority - 1;
}
}
This is my function method to move the row to the top of the Datagrid. Can anyone point me in the right direction of where I have gone wrong with this line of code?
jobListView.SelectedIndex = SelectedRow.Priority - 1;
We suspect that when a row is moved to the top of the Priority value is zero in SelectedRow. In this case, the SelectedIndex is set as -1. The selected row is not highlighted when SelectedIndex value is -1 in SfDataGrid. This is the behavior of SfDataGrid. So, can you please check the SelectedIndex value when a row is moved to the top of the SelectedRow.Priority.
I managed to get it working with the help of the Syncfusion Team.
I created a method in my Datagrid User control class:
public void MoveToTopRow()
{
var getSelectedIndex = 0;
jobListView.SelectedIndex = getSelectedIndex;
}
I then called this method on the button click in my MainWindow class:
private void btnArrowTop_Click(object sender, RoutedEventArgs e)
{
jobListView.PriorityMoveTop();
jobListView.MoveToTopRow();
jobListView.SetDataGridJobList();
jobListView.MoveToTopRow();
}
Maintaining the selection after resetting the ItemSource fixed it.

C# ListBox or DataGrid double click SELECTED item

I have a problem with returning selected item from listbox or DataGrid. The problem is, I specifically want user to double click the chosen item, not anywhere else on the control (as is with DoubleClick event).
Currently I have this:
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem == null) return;
System.Console.WriteLine(listBox1.SelectedItem.ToString()); //console for testing
}
While it works, I don't like the fact that you can click once on an item and then double click anywhere on blank space of the control and it returns the previously selected item. Is there any easy way to change it?
You should get item index from the location where double click event occur:
int index = listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)
{
System.Console.WriteLine(listBox1.SelectedItem.ToString()); //console for testing
}

datagridview contextmenustrip : showing only for chosen rows?

I have standard datagridview and I have contextmenustrip. My problem is that I need to show this contextmenustrip when user click right mouse button but not on every row! Only on rows I've chosen.
I tried this:
dataGridView1.Rows[1].ContextMenuStrip = contextMenuStrip1;
But it doesn't work.
Sounds to me like you want to open your ContextMenuStrip if your user right clicks the header of your DataGridView's column that satisfies some condition.
In short: use the DataGridView MouseDown event and in that event check for the conditions and if they're met call the Show method of your ContextMenuStrip.
Code sample that you may refer:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
var ht = dataGridView1.HitTest(e.X, e.Y);
// Place your condition HERE !!!
// Currently it allow right click on last column only
if (( ht.ColumnIndex == dataGridView1.Columns.Count - 1)
&& (ht.Type == DataGridViewHitTestType.ColumnHeader)) {
// This positions the menu at the mouse's location
contextMenuStrip1.Show(MousePosition);
}
}
}

Get Mouse click event from Microsoft Chart Control click on data marker

I have a .net 4.0 point chart in my app. I would like to capture the mouse click on a data marker. When the user clicks on a particular point, I'd like to go to the row in the bound table where the data came from.
Is this functionality built-in to the .net chart control?
EDIT: I found that I may have actually wanted the cursor position value rather than requiring the user to click on a specific data point. Once I have the cursor location, that value can be used to find the row in the dataset that is closest to the mouse click. I accepted the answer to my original question below as it was a correct answer to what I initially requested.
The solution to my 'real' problem was found in the post by user quinn in the post
Showing Mouse Axis Coordinates on Chart Control
{
var chartArea = _chart.ChartAreas[0];
var xValue = chartArea.AxisX.PixelPositionToValue(x);
var yValue = chartArea.AxisY.PixelPositionToValue(y);
return new Tuple<double, double>(xValue, yValue);
}
You can try this:
protected void Page_Load(object sender, EventArgs e)
{
foreach (DataPoint dp in this.Chart1.Series["YourSeriesName"].Points)
{
dp.PostBackValue = "#VALX,#VALY";
}
}
protected void Chart1_Click(object sender, ImageMapEventArgs e)
{
string[] pointData = e.PostBackValue.Split(',');
// Add click event code here
}
You need to set OnClick="Chart1_Click" in asp:Chart.
Or if you have multiple series on your chart you can set PostBack on Series directly and pass information about the series.

Clear ListBox Selection when empty area is clicked

At least one item always remain selected in ListBox. I want that when user clicks empty area of ListBox, selection should be cleared. How to do this?
I am trying to replicate Opera Notes as a part of my application. First i was using a binded DataGridView now i am using a binded ListBox on left pane
Handle the ListBox.MouseDown event.
Call ListBox.IndexFromPoint, passing the Location property from the MouseDown event's MouseEventArgs parameter.
This should return the index of the item that was clicked, or ListBox.NoMatches if the click was on an empty area.
If the return value is ListBox.NoMatches, set the ListBox.SelectedIndex property to -1 to clear the selection.
Mr. Avalanchis has answered the question already. I am just adding the code necessary to follow the steps what he has suggested. Hope the explicit code will help.
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
Point pt = new Point(e.X, e.Y);
int index = listBox1.IndexFromPoint(pt);
if (index <= -1)
{
listBox1.SelectedItems.Clear();
}
}

Categories

Resources