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);
}
}
Related
I am making a Scorebar from 1-10 where every number is one picture of a small grey cube.
Like this:
🟩🟩🟩🟩🟩⬛⬛⬛⬛⬛ (This would be a score of 5)
Now I want to change the cube images to green ones, when the MouseDown Event is triggered, but I don't know who to tell the program.
Pic of Scorebar
private void BtnDebug_Click(object sender, EventArgs e)
{
int xPos = 200;
int yPos = 100;
PictureBox[] ScoreGameplay = new PictureBox[100];
for (int i = 0; i < 10; i++)
{
ScoreGameplay[i] = new PictureBox();
ScoreGameplay[i].Name = "ScoreGameplay" + i;
ScoreGameplay[i].Size = new Size(18, 18);
ScoreGameplay[i].Location = new Point(xPos, yPos);
ScoreGameplay[i].Image = Image.FromFile(#"img\icons\score_empty.png");
ScoreGameplay[i].MouseEnter += new EventHandler(Score_MouseEnter);
ScoreGameplay[i].MouseLeave += new EventHandler(Score_MouseLeave);
ScoreGameplay[i].MouseDown += new MouseEventHandler(Score_MouseDown);
this.Controls.Add(ScoreGameplay[i]);
xPos += 18;
}
This part works without an issue, but here we go:
private void Score_MouseDown(object sender, EventArgs e)
{
if (sender is PictureBox pBox)
{
// ???
}
}
How do I tell know which Index of the Array in BtnDebug_Click has triggered the MouseDown?
For example:
The 7th PictureBox has been clicked; now I want to change the images from PictureBoxes 1-7 to the green ones.
Anyone has a smart solution for this?
Simply you can do that
private void BtnDebug_Click(object sender, EventArgs e)
{
int xPos = 200;
int yPos = 100;
PictureBox[] ScoreGameplay = new PictureBox[100];
for (int i = 0; i < 10; i++)
{
ScoreGameplay[i] = new PictureBox();
ScoreGameplay[i].Name = $"ScoreGameplay{i}";
ScoreGameplay[i].Size = new Size(18, 18);
ScoreGameplay[i].Location = new Point(xPos, yPos);
ScoreGameplay[i].Image = Image.FromFile(#"img\icons\score_empty.png");
ScoreGameplay[i].MouseEnter += new EventHandler(Score_MouseEnter);
ScoreGameplay[i].MouseLeave += new EventHandler(Score_MouseLeave);
ScoreGameplay[i].MouseDown += new MouseEventHandler(Score_MouseDown);
this.Controls.Add(ScoreGameplay[i]);
xPos += 18;
}
}
and in Score_MouseDown
private void Score_MouseDown(object sender, EventArgs e)
{
string imageName = ((PictureBox)sender).Name; // get the name of the clicked image
string imageIndex = imageName.Substring(13); // get the text after 13 chars
}
I'm trying to create a cross-hair out of two lines on a WPF canvas.
I'm finding that when I initialize the lines they draw correctly.
But when I update the coordinates of the lines using the mouse move event, the lines are not updating.
Why are the lines not updating when the coordinates of the point `currentMousePoint are updated?
public CADControl()
{
InitializeComponent();
}
//(500, 500) added just to test the lines appear which they do.
private Point currentMousePoint = new Point(500,500);
private void CadCanvas_MouseMove(object sender, MouseEventArgs e)
{
currentMousePoint = e.GetPosition(this);
//coordinates appear to update correctly in the console
Console.WriteLine("x: " + currentMousePoint.X + " y: " + currentMousePoint.Y);
InvalidateVisual();
UpdateLayout();
}
private double screenWidth;
private double screenHeight;
private void DrawCursorCrosshair()
{
screenWidth = CadCanvas.ActualWidth;
screenHeight = CadCanvas.ActualHeight;
Line horizontalLine = new Line();
horizontalLine.Stroke = Brushes.White;
horizontalLine.X1 = 0;
horizontalLine.X2 = screenWidth;
horizontalLine.Y1 = currentMousePoint.X;
horizontalLine.Y2 = currentMousePoint.X;
CadCanvas.Children.Add(horizontalLine);
Line verticalLine = new Line();
verticalLine.Stroke = Brushes.White;
verticalLine.X1 = currentMousePoint.X;
verticalLine.X2 = currentMousePoint.X;
verticalLine.Y1 = 0;
verticalLine.Y2 = screenHeight;
CadCanvas.Children.Add(verticalLine);
}
private void CadCanvas_Loaded(object sender, RoutedEventArgs e)
{
DrawCursorCrosshair();
}
You need to store the reference to your lines and update them in the MouseMove event handler:
private Line _v;
private Line _h;
add in DrawCursorCrosshair:
_h = horizontalLine;
_v = verticalLine;
and in CadCanvas_MouseMove (after currentMousePoint is updated):
Canvas.SetLeft(_v, currentMousePoint.X);
Canvas.SetTop(_h, currentMousePoint.Y);
and set a static position to the lines in DrawCursorCrosshair:
verticalLine.X1 = 0;
verticalLine.X2 = 0;
horizontalLine.Y1 = 0;
horizontalLine.Y2 = 0;
I'm creating an application which plots data from a serial port. My problem is that sometimes chart size increases when values go below the X axis and, as a result, some values on X axis disappear.
I've set min and max Y axis value, so I wonder how it is possible for the horizontal axis to moves gently downward for a while and then disappear - possibly due to a lack of space?.
It occurs only for first and last label, and sometimes the last grid line disappears too.
Here is piece of code which generate chart values:
int minStress = -200, maxStress=200;
double maxTime=20.0, minTime=0.0;
private void timer1_Tick(object sender, EventArgs e)
{
Stress = serialPort1.ReadLine();
label6.Text = Stress;
StressChart.ChartAreas[0].AxisX.Minimum = minTime;
StressChart.ChartAreas[0].AxisX.Maximum = maxTime;
StressChart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
StressChart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
this.StressChart.Series[0].Points.AddXY((minTime + maxTime) / 2, Stress);
minTime=minTime + TimerInt_2/1000;
maxTime=maxTime + TimerInt_2/1000;
serialPort1.DiscardInBuffer();
}
private void cmbTimerInt_SelectedIndexChanged(object sender, EventArgs e)
{
TimerInt = int.Parse(cmbTimerInt.Text);
TimerInt_2=double.Parse(cmbTimerInt.Text);
timer1.Interval = TimerInt;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
minStress = int.Parse(textBox1.Text);
}
catch { }
if (minStress < maxStress)
{
StressChart.ChartAreas[0].AxisY.Minimum = minStress;
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
try
{
maxStress = int.Parse(textBox2.Text);
}
catch { }
if (maxStress > minStress)
{
StressChart.ChartAreas[0].AxisY.Maximum = maxStress;
}
}
Here is code which sets chart properties:
chartArea1.AxisX.LabelStyle.Format = "0.0";
chartArea1.Name = "ChartArea1";
this.StressChart.ChartAreas.Add(chartArea1);
legend1.Name = "Legend1";
this.StressChart.Legends.Add(legend1);
this.StressChart.Location = new System.Drawing.Point(225, 12);
this.StressChart.Name = "StressChart";
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
series1.Legend = "Legend1";
series1.Name = "Series1";
this.StressChart.Series.Add(series1);
this.StressChart.Size = new System.Drawing.Size(1175, 426);
this.StressChart.TabIndex = 5;
this.StressChart.Text = "chart1";
chartArea1.AxisX.LabelAutoFitStyle = 0;
chartArea1.AxisY.LabelAutoFitStyle = 0;
chartArea1.AxisX.MajorTickMark.Size = 0;
chartArea1.AxisX.IsMarginVisible = true;
chartArea1.AxisY.IsMarginVisible = true;
The reason of such behaviour was decimal places number of X axis labels. Solution is to round these values down to one decimal place.
minTime = Math.Round(minTime,1) + Math.Round(TimerInt_2/1000,1);
maxTime = Math.Round(maxTime,1) + Math.Round(TimerInt_2/1000,1);
I wanna change the Text properties of Label using Buttons just like in hangman; but after I created the Label, I became confused when I try to access the specific Label
// creating label
for (int i = 0; i < numericUpDown1.Value; i++)
{
Label l = new Label();
l.Text = "_";
l.Width = 20;
l.Height = 25;
l.Left = i * 20 + 510;
l.Top = 20;
l.BackColor = Color.Transparent;
groupBox2.Controls.Add(l);
}
// function to change the label text
// if I clicked the button
// the first label text will be changed to the text in the button i clicked
private void B_Click(object sender, EventArgs e)
{
var thsBtn = (Button)sender;
bool benar = false;
if (benar == false)
{
thsBtn.Text = " ";
thsBtn.Enabled = false;
}
else
{
thsBtn.Enabled = false;
}
}
You can organize created Labels into a collection, say, List<Label>:
private List<Label> m_CreatedLabels = new List<Label>();
...
// Remove all previous labels
foreach (Label lbl in m_CreatedLabels)
lbl.Dispose();
m_CreatedLabels.Clear();
// Create new ones
for (int i = 0; i < numericUpDown1.Value; i++) {
m_CreatedLabels.Add(new Label() {
Text = "_",
Width = 20,
Height = 25,
Left = i * 20 + 510,
Top = 20,
BackColor = Color.Transparent,
Parent = groupBox2
});
}
Now you have m_CreatedLabels collection to work with created Labels, e.g.
private void B_Click(object sender, EventArgs e) {
var thsBtn = sender as Button;
// you may want to add a condition into FirstOrDefault(), e.g.
// .FirstOrDefault(lbl => lbl.Text == "_")
// - first label with "_" Text
Label lblToProcess = m_CreatedLabels
.FirstOrDefault();
if (null != lblToProcess)
lblToProcess.Text = thsBtn.Text;
thsBtn.Enabled = false;
}
One option here is to give your dynamically created Label instances a Name. From there, you should be able to use ControlCollection.Find to find your Label instances by name.
private void CreateLabels()
{
for (int i = 0; i < numericUpDown1.Value; i++)
{
Label l = new Label();
l.Name = $"DynamicLabel{i}";
l.Text = "_";
l.Width = 20;
l.Height = 25;
l.Left = i * 20 + 510;
l.Top = 20;
l.BackColor = Color.Transparent;
groupBox2.Controls.Add(l);
}
}
private void DoSomethingWithADynamicLabel(int dynamicLabelIndex)
{
Label l = groupBox2.Controls.Find($"DynamicLabel{i}", true).FirstOrDefault() as Label;
if (l is null)
{
// Couldn't find the label...
return;
}
// Do something with l
}
When creating the Label instances inside CreateLabels, I'm simply appending the for loop's counter to the string "DynamicLabel". This gives you a bunch of Labels with names like "DynamicLabel0", "DynamicLable1", "DynamicLabel2", etc...
Then in DoSomethingWithADynamicLabel, assuming you have the index of the Label you want to deal with, you can use groupBox2.Controls.Find to actually find the Label you're interested in. ControlCollection.Find returns Control[], so calling FirstOrDefault will take the first item from the array or null if no Control with the given name exists.
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.