I am working with windows form application in c#. I have installed licensed version of TeeChart for.net v3. I am trying to remove some unwanted portion of TeeChart.
Thanks to stackoverflow website users, they helped me to create a mouse click popup on TeeChart. Now i want to implement some functionality for that popup clicks.
using the below code i am able to create mouse right click popup.
double m_dblTempVolFromTo = 0;
double dtFromTo = 0;
private void mainTChart_MouseUp(object sender, MouseEventArgs e)
{
if (!checkBoxIsTime.Checked && e.Button == MouseButtons.Right)
{
m_dblTempVolFromTo = mainTChart.Series[0].XScreenToValue(e.X);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
else if (checkBoxIsTime.Checked && e.Button == MouseButtons.Right)
{
DateTime dt;
dtFromTo = mainTChart.Series[0].XScreenToValue(e.X);
DateTime.TryParse(dtFromTo.ToString(), out dt);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + dt.TimeOfDay.ToString() ));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + dt.TimeOfDay.ToString()));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
}
The above code is creating popup like as shown below.
I am trying to implement functionality for "For 7.6 cc" and "To 7.6 cc".
when i click "To 7.6 cc" then the chart should remove from "0 to 7.6" scale and remaining part should be there. As well the same apply for "From 145 cc" click, it has to remove the chart from "145 to 150(end of scale)".
click on "To" is using to remove starting portion of chart and click on "From" is using to remove end portion of chart.
I have tried like this but i am not able to get what i want.
void From_Click(object sender, EventArgs e)
{
if (!checkBoxIsTime.Checked)
{
var destBitmap = mainTChart.Export.Image.Bitmap.Clone(new Rect(0, 0, 100, 200), sourceBitmap.PixelFormat);
}
}
void To_Click(object sender, EventArgs e)
{
}
even i have tried with this code also
void mainTChart_GetLegendRect(object sender, mainTChart.GetLegendRectEventArgs e)
{
Rectangle cropRect = e.Rectangle;
Bitmap legendImg = new Bitmap(cropRect.Width, cropRect.Height);
using (Graphics g = Graphics.FromImage(legendImg))
{
g.DrawImage(chartBmp, new Rectangle(0, 0, mainTChart.Width, mainTChart.Height),
cropRect,
GraphicsUnit.Pixel);
}
}
nothing is working for me. Can any one help me with this task.
Thanks in advance.
Edited
when i have X-axis as time then i am not able to get display time on mouse click. The code I have tried as shown below
DateTime dt;
dtFromTo = mainTChart.Series[0].XScreenToValue(e.X);
DateTime.TryParse(dtFromTo.ToString(), out dt);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + dt.TimeOfDay.ToString() ));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + dt.TimeOfDay.ToString()));
I am getting like this
I am getting as shown in the above image but want to display the equivalent time on mouse right click. I am getting some value to this variable "dtFromTo" like 41322.9876587965" but i am not able to convert that value into time. please help me.
Known the values you want to "cut" From/To, you can just set the Bottom axis Minimum and Maximum properties.
when i click "To 7.6 cc" then the chart should remove from "0 to 7.6"
scale and remaining part should be there
This would be:
mainTChart.Axes.Bottom.AutomaticMinimum = false;
mainTChart.Axes.Bottom.Minimum = 7.6;
As well the same apply for "From 145 cc" click, it has to remove the
chart from "145 to 150(end of scale)".
This would be done setting the Maximum:
mainTChart.Axes.Bottom.AutomaticMaximum = false;
mainTChart.Axes.Bottom.Maximum = 145;
So I think this should to the trick:
void From_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMaximum = false;
mainTChart.Axes.Bottom.Maximum = m_dblTempVolFromTo;
}
void To_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMinimum = false;
mainTChart.Axes.Bottom.Minimum = m_dblTempVolFromTo;
}
I'd also suggest you to create the ContextMenu in mainTChart_MouseDown event insted of mainTChart_MouseUp because creating it at the mainTChart_MouseUp event is too late, the ContextMenu shown won't show the updated version.
Edit:
For DateTime XValues, the wrong line is this one:
DateTime.TryParse(dtFromTo.ToString(), out dt);
Here it is the full code that seems to work fine for me here:
private void InitializeChart()
{
mainTChart.Aspect.View3D = false;
Line line1 = new Line(mainTChart.Chart);
line1.XValues.DateTime = true;
line1.FillSampleValues();
mainTChart.Axes.Bottom.Labels.DateTimeFormat = "hh:mm";
mainTChart.MouseDown += new MouseEventHandler(mainTChart_MouseDown);
}
double m_dblTempVolFromTo = 0;
double dtFromTo = 0;
void mainTChart_MouseDown(object sender, MouseEventArgs e)
{
if (!mainTChart.Axes.Bottom.IsDateTime && e.Button == MouseButtons.Right)
{
m_dblTempVolFromTo = mainTChart[0].XScreenToValue(e.X);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + m_dblTempVolFromTo + " cc"));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
else if (e.Button == MouseButtons.Right)
{
dtFromTo = mainTChart[0].XScreenToValue(e.X);
String stFromTo = mainTChart.Axes.Bottom.Labels.LabelValue(dtFromTo);
mainTChart.ContextMenu = new ContextMenu();
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("From " + stFromTo));
mainTChart.ContextMenu.MenuItems.Add(new MenuItem("To " + stFromTo));
mainTChart.ContextMenu.MenuItems[0].Click += new EventHandler(From_Click);
mainTChart.ContextMenu.MenuItems[1].Click += new EventHandler(To_Click);
}
}
void From_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMaximum = false;
if (!mainTChart.Axes.Bottom.IsDateTime)
mainTChart.Axes.Bottom.Maximum = m_dblTempVolFromTo;
else
mainTChart.Axes.Bottom.Maximum = dtFromTo;
}
void To_Click(object sender, EventArgs e)
{
mainTChart.Axes.Bottom.AutomaticMinimum = false;
if (!mainTChart.Axes.Bottom.IsDateTime)
mainTChart.Axes.Bottom.Minimum = m_dblTempVolFromTo;
else
mainTChart.Axes.Bottom.Minimum = dtFromTo;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
namespace stock4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.chart1.AxisViewChanged += chart1_AxisViewChanged;
this.chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart1_MouseMove);
}
private void CandleStick_Load(object sender, EventArgs e)
{
CHART();
}
string path = #"C:\Users\1\Documents\Visual Studio 2013\Projects\stock3\stock3\bin\Debug\#S-PG1440.csv";
static int count = System.IO.File.ReadAllLines(#"C:\Users\1\Documents\Visual Studio 2013\Projects\stock3\stock3\bin\Debug\#S-PG1440.csv").Length;
int[] index = new int[count];
DateTime[] nums = new DateTime[count];
double[,] mass = new double[count, 4];
public void CHART()
{
//Here the data from the file is read and entered into the array.
//chart1.Series["price"].Points.AddXY(index[i], mass[i, 1], mass[i, 2], mass[i, 0], mass[i, 3]);
}
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
//Autoscaling the graph
}
public static string str;
private void button1_Click(object sender, EventArgs e)
{
Form newForm = new Form();
newForm.DoubleBuffered = true;//Error 1 Cannot access
//protected member 'System.Windows.Forms.Control.DoubleBuffered'
//via a qualifier of type 'System.Windows.Forms.Form';
//the qualifier must be of type 'stock4.Form1'
//(or derived from it)
newForm.Show();
newForm.Width = 150;
newForm.Height = 230;
newForm.BackColor = Color.White;
newForm.Paint += new PaintEventHandler(MyPaintHandler);
}
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(e.Location, false);
chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(e.Location, false);
int val = (int)chart1.ChartAreas[0].CursorX.Position;
if (val >= 0)
{
double current = chart1.ChartAreas[0].CursorY.Position;
str = "time: " + nums[val] + "\n" +
"current: " + current + "\n" +
"open: " + mass[val, 0] + "\n" +
"high: " + mass[val, 1] + "\n" +
"low: " + mass[val, 2] + "\n" +
"close: " + mass[val, 3];
}
}
static void MyPaintHandler(object objSender, PaintEventArgs pea)
{
Form newForm = (Form)objSender;
Graphics grfx = pea.Graphics;
grfx.DrawString(str, newForm.Font, Brushes.Black, 0, 0);
newForm.Invalidate();
Thread.Sleep(1);
}
}
}
I read the data from the form and pass it to another for display.
I create a form to display when a button event occurs(button1_Click).
Data for "str" is taken from another form.
Perhaps the second form is not needed?
I need a separate window from the main form to display the data.
UPDATED THE CODE! Part of the code is not on the subject was removed from the comments.
How do I place the string " newForm.DoubleBuffered = true " in "button1_Click" without errors? It is possible a code sample?
Forget about painting, doublebuffering etc.
Simply add a Label control to the 2nd form and access it in the chart's MouseMove event!
Make newForm a class level variable:
Form newForm = null;
In your button click write something like this:
newForm = new Form();
..
Label lbl = new Label() { Name = "myLabel", Parent = newForm };
newForm.Show();
In your MouseMove write something like this:
if (newForm != null && newForm.Controls.ContainsKey("myLabel"))
((Label)newForm.Controls["myLabel"]).Text = str;
Very simple, no flicker and you are free to style the Label in any way you like.
I have been tasked with creating a somewhat hierarchical datagridview for my company. I heavily modified one from Syed Shanu that is posted here https://www.codeproject.com/Articles/848637/Nested-DataGridView-in-windows-forms-csharp. I'm almost done (data loads properly, etc.), however I cannot for the life of me figure out how to get the detail grid to move when I scroll. It's a drawn on rectangle and I'm looking for a way to somehow bind it to the master grid so it scrolls up and down with the regular grid. Any help would be appreciated. Here is the code that draws the rectangle:
private void masterDGVs_CellContentClick_Event(object sender, DataGridViewCellEventArgs e)
{
DataGridViewImageColumn cols = (DataGridViewImageColumn)MasterDGVs.Columns[0];
MasterDGVs.Rows[e.RowIndex].Cells[0].Value = Image.FromFile(#"expand.png");
if (e.ColumnIndex == gridColumnIndex)
{
if (ImageName == #"expand.png")
{
DetailDGVs.Visible = true;
ImageName = #"toggle.png";
MasterDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(ImageName);
String FilterExpression = MasterDGVs.Rows[e.RowIndex].Cells[FilterColumnName].Value.ToString();
MasterDGVs.Controls.Add(DetailDGVs);
Rectangle DGVRectangle = MasterDGVs.GetCellDisplayRectangle(1, e.RowIndex, true);
DetailDGVs.Size = new Size(MasterDGVs.Width - 48, DetailDGVs.PreferredSize.Height - 16);
DetailDGVs.Location = new Point(DGVRectangle.X, DGVRectangle.Y + 20);
DataView detailView = new DataView(DetailGridDT);
detailView.RowFilter = FilterColumnName + " = '" + FilterExpression + "'";
foreach (DataGridViewRow row in DetailDGVs.Rows)
{
if (row.Cells[5].Value.ToString() == "Error")
{
row.Cells[5].Style.ForeColor = Color.DarkRed;
}
else if (row.Cells[5].Value.ToString() == "Processed and Complete")
{
row.Cells[5].Style.ForeColor = Color.Green;
}
else
{
row.Cells[5].Style.ForeColor = Color.Yellow;
}
}
}
else
{
ImageName = #"expand.png";
MasterDGVs.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Image.FromFile(ImageName);
DetailDGVs.Visible = false;
}
}
else
{
DetailDGVs.Visible = false;
}
}
I have sort of working by adding:
MasterDGVs.MouseWheel += new MouseEventHandler(DetailDGV_Scroll);
DetailDGVs.MouseWheel += new MouseEventHandler(MasterDGV_Scroll);
and
private void DetailDGV_Scroll(object sender, MouseEventArgs e)
{
int scale = e.Delta * SystemInformation.MouseWheelScrollLines / 5;
DetailDGVs.Top = DetailDGVs.Top + scale;
}
private void MasterDGV_Scroll(object sender, MouseEventArgs e)
{
int scale = e.Delta * SystemInformation.MouseWheelScrollDelta / 5;
MasterDGVs.Top = MasterDGVs.Top - scale;
}
I have text file with one line in it. The line looks like this:
100 300 200 400 658 487 2636 254 245 527
These numbers represent X and Y coordinates of points (first and second are X and Y of point N1, third and fourth are X and Y of point N2,...., ).
I read the file and put it in an array.
My next step is to draw the picture boxes in a container (panel).
The problem is that the panel is only showing the control with last coordinates.
private void CreateBlastHole(string[] pointCoordinate)
{
PictureBox blastHole = new PictureBox();
blastHole.Height = 15;
blastHole.Width = 15;
blastHole.BackColor = Color.Blue;
for (int i = 0; i < pointCoordinate.Length; i++)
{
blastHole.Left = int.Parse(pointCoordinate[i]);
i = i + 1;
blastHole.Top = int.Parse(pointCoordinate[i]);
drawingPanel.Controls.Add(blastHole);
}
blastHole.Click += new EventHandler(BlastHole_Click);
ToolTip tooltip1 = new ToolTip();
// Set up delays for the tooltip
tooltip1.AutoPopDelay = 5000;
tooltip1.InitialDelay = 1000;
tooltip1.ReshowDelay = 500;
// Force the tooltip text to be displayed whether or not the form is active.
tooltip1.ShowAlways = true;
// Set up the tooltip text for the controls
int axisX = blastHole.Location.X;
int axisY = blastHole.Location.Y;
string coordinates = "Точка N " + blastHole.Name + "X = " + axisX.ToString() + " Y = " + axisY.ToString();
tooltip1.SetToolTip(blastHole, coordinates);
}
private void BlastHole_Click(object sender, EventArgs e)
{
MessageBox.Show(MousePosition.X.ToString(), MousePosition.Y.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openButton_Click(object sender, EventArgs e)
{
openFileDialogPoints.ShowDialog();
string name = openFileDialogPoints.FileName;
File.ReadAllLines(name);
string[] points = File.ReadAllText(name).Split( );
CreateBlastHole(points);
}
private void drawingPanel_Paint(object sender, PaintEventArgs e)
{
}
private void buttonDrawHole_Click(object sender, EventArgs e)
{
}
You need to move your code inside the for-loop, to create more than one PictureBox. Currently, you are only creating one instance, reusing it for each blastHole.
Try something like this instead:
private void CreateBlastHole(string[] pointCoordinate)
{
for (int i = 0; i < pointCoordinate.Length; i++)
{
PictureBox blastHole = new PictureBox();
blastHole.Height = 15;
blastHole.Width = 15;
blastHole.BackColor = Color.Blue;
blastHole.Left = int.Parse(pointCoordinate[i]);
i = i + 1;
blastHole.Top = int.Parse(pointCoordinate[i]);
drawingPanel.Controls.Add(blastHole);
blastHole.Click += new EventHandler(BlastHole_Click);
ToolTip tooltip1 = new ToolTip();
// Set up delays for the tooltip
tooltip1.AutoPopDelay = 5000;
tooltip1.InitialDelay = 1000;
tooltip1.ReshowDelay = 500;
// Force the tooltip text to be displayed whether or not the form is active.
tooltip1.ShowAlways = true;
// Set up the tooltip text for the controls
int axisX = blastHole.Location.X;
int axisY = blastHole.Location.Y;
string coordinates = "Точка N " + blastHole.Name + "X = " + axisX.ToString() + " Y = " + axisY.ToString();
tooltip1.SetToolTip(blastHole, coordinates);
}
}
What's the deal, i have an imagePanel (u can look at it as a picturePanel)
called imagePanel1 (i imported it so i don't have to make scroll Bars :))
and i have a treeView on the left side, from witch i can drag a node, and drop it over the imagePanel, where i get a Location of the drop, and on that location i create a normal panel called panel1
And so i do 100 times, so at the end i ll have an imagePanel full of small panels...
Now is the problem, when i click on the imagePanel (where a panel is located)
I want that panel to be selected on MousePress, and moved on mouseMove, and eventualLy deleted on a btnDelete...
Here is the code for the imagePanel:
//***********************************************************************
private void imagePanel1_DragDrop_1(object sender, DragEventArgs e)
{
Type testTip = new TreeNode().GetType();
YLScsImage.ImagePanel dropPicturePanel = (YLScsImage.ImagePanel)sender;
TreeNode movedNode;
_mouseDownSelectedWindow = Rectangle.Empty;
if (e.Data.GetDataPresent(testtype))
{
movedNode= (TreeNode)e.Data.GetData(testType);
dropPicturePanel.Tag = movedNode.Tag;
movedNode.ImageIndex = 1;
movedNode.SelectedImageIndex = 1;
movedNode.ForeColor = Color.Gray;
//**************************************
//HERE IS THE CODE FOR THE CREATED PANEL
Panel panel1 = new Panel();
Point point1 = this.PointToClient(new Point(e.X - 278, e.Y - 19)); //the imagePanel1 is on the form at the point 278,19
panel1.AllowDrop = true;
panel1.Location = point1;
panel1.BackgroundImage = iltest.Images[0]; //nvm
panel1.Height = 16;
panel1.Width = 16;
imagePanel1.Controls.Add(panel1); //am adding it to the imagePanel1
//saving the locations of each panel
string path = #"C:\Users\Cosic\Desktop\ICR\TABELA3_Paneli.txt"; // path to file
if (!File.Exists(path))
File.Create(path);
if (panelBr == 0)
System.IO.File.WriteAllBytes(path, new byte[0]); //brise ceo text iz fajla
TextWriter sw = new StreamWriter(path, true);
sw.WriteLine(e.X + "; " + e.Y + "; " + panel1.Width + "; " + panel1.Height + ";");
sw.Close();
//am done with saving
panelBr++;//nvm
}
}
tell me if u need some more code...i got a lot of it ;)
and sorry for bad english, am not that good as I would like to be...
I solved the problem, like this:
panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
just write panel1.anyEventUWant += and 2 times tab button....
it generate automaticly a new function with a single code line in it
here is an example
void panel1_MouseUp (object sender, EventArgs e)
{
//throw new NotImplementedException();
}
U can access the panel like this: ((object)sender).
i have dynamically generated images. if i click last added image it dragging normally, but when i click to other image and tried to drag it, last added image dragging again.
here is my codes
void setDynamicImages()
{
for (int i = 0; i < 10; i++)
{
RectMeyve = new Rectangle();
IMG_meyve = new Image();
IMG_meyve.Height = 36 * ratioHeight;
IMG_meyve.Width = 51 * ratioWidth;
randMeyveCesit = Rand.Next(5);
IMG_meyve.Source = new BitmapImage(new Uri("ms-appx:///Assets/Sayfa10/dusen_meyveler/cilek_0" + (randMeyveCesit + 1) + ".png"));
IMG_meyve.Tag =randMeyveTur;
pressedIMGTag =randMeyveTur;
RectMeyve.Fill = new SolidColorBrush(Colors.Aqua);
RectMeyve.Height = IMG_meyve.ActualHeight;
RectMeyve.Width = IMG_meyve.Width;
Canvas.SetLeft(RectMeyve, Canvas.GetLeft(IMG_meyve));
Canvas.SetTop(RectMeyve, Canvas.GetTop(IMG_meyve));
RectMeyve.Opacity = 0.6;
RectMeyve.IsHitTestVisible = false;
IMG_meyve.ActualWidth, IMG_meyve.ActualHeight);
this.canvasMeyveleriSayalim.Children.Add(IMG_meyve);
this.canvasMeyveleriSayalim.Children.Add(RectMeyve);
IMG_meyve.PointerPressed += IMG_meyve_PointerPressed;
IMG_meyve.PointerReleased += IMG_meyve_PointerReleased;
}
}
private void IMG_meyve_PointerPressed(object sender, PointerRoutedEventArgs e)
{
isHoldMeyve = true;
positionWithinImage = e.GetCurrentPoint(sender as Image).Position;
meyveX = e.GetCurrentPoint(IMG_meyve).Position.X;
meyveY = e.GetCurrentPoint(IMG_meyve).Position.Y;
}
private void canvasSayfa10_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (isHoldMeyve)
{
if (Convert.ToInt32(IMG_meyve.Tag) == pressedIMGTag) //this part useless
{
PointerPoint pt1 = e.GetCurrentPoint(canvasMeyveleriSayalim);
Canvas.SetLeft(IMG_meyve, pt1.Position.X - meyveX);
Canvas.SetTop(IMG_meyve, pt1.Position.Y - meyveY);
}
}
}
sorry for my english.
where is my mistake.