在表单上创建坐标? [英] Creating coordinates on the form?

查看:73
本文介绍了在表单上创建坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个有两条线的形状,就像两个坐标x轴和y轴一样

现在我想在这些坐标上添加点。



例如在这一点上(2,3)我想在点(-4,5)上添加点

另一点
$ b点上的$ b(-5,-7)另一点

点(4,-6)另一点



如何我可以这样做吗?



这里有两行



I am creating a form which have two lines on it just like the two coordinates x-axis and y-axis
Now i want to add points on these coordinates.

e.g on this point(2,3) i want to add a point
on the point (-4,5) another point
on the point (-5,-7) another point
on the point (4,-6) another point

How can i do this?

here are two lines

e.Graphics.DrawLine(Pens.Black, 190, 210, 350, 210)
    e.Graphics.DrawLine(Pens.Black, 330, 90, 330, 300)

推荐答案

使用简单的数学。您必须将原始的0,0点位置存储在表单上。假设你的表单有尺寸:300x200,你的轴正好在表格的中间,你可以这样做:



Use simple mathematics. You must store your original 0,0 point location on form. Assuming your form has dimensions: 300x200 and you have your axis exactly in the middle of the form you can do like this:

private int X0 = 150;
private int Y0 = 100;

// Pass your values to calculation method
private Point CalculatePoint(int x, int y)
{
    return new Point(X0 + x, Y0 + y);
}





然后在您的代码中,您可以这样调用此方法:





Then in your code you call this method like this:

var point1 = CalculatePoint(2, 3);    // 152, 103
var point2 = CalculatePoint(-4, 5);   // 146, 105
var point3 = CalculatePoint(-5, -7);  // 145, 93
var point4 = CalculatePoint(4, -6);   // 154, 94





您可以使用表单的OnClientSizeChanged来确定何时更改表单大小。然后只需更新原始点位置:





You can use form's OnClientSizeChanged to determine when form size changed. Then simply update your original point location:

private void Form1_ClientSizeChanged(object sender, EventArgs e)
{
    X0 = this.Size.Width / 2;
    Y0 = this.Size.Height / 2;
}





不要忘记重绘你的图表。



干杯!



And don't forget to redraw your chart.

Cheers!


最简单的方法是按照我的想法执行以下步骤



1)添加标签的控制数组(如果是数字)点数不固定。

2)添加两个文本框用于坐标输入,例如textX,textY

3)添加textchanged事件,输入以下代码

easiest way is following step as I think

1) Add control array of label if number of dots are not fixed.
2) Add two textbox for co-ordinates entry e.g. textX, textY
3) Add in there textchanged event, enter following code
textX.location = New System.Drawing.Point(Val(txtX.Text) , textX.location.Y)




textY.location = New System.Drawing.Point(textX.location.X,Val(txtY.Text))





试试这个并回复。



try this and reply.


这篇关于在表单上创建坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆