0
我剛開始學習C#和我有一個分配新建分配FY,我必須的DrawLine一直在錯誤的X繪圖和y位置
- 選擇radionbutton「lijn」
- 選擇演員(圖片框1, 2或3,在這裏稱爲actor1/actor2/actor3)。
- 點擊大PictureBox的地方(佔據整個屏幕)
然而,當過我點擊屏幕上的某個地方,它吸引P1是0,0和p2是不合邏輯的地方線(不同對於每個演員,但始終是相同的位置)
我可能犯了一些巨大的錯誤,我知道我的代碼非常糟糕,但請記住我剛開始學習,我遇到了一些困難:)
將是偉大的,如果有人知道如何解決這個問題!
相關部分(全下方代碼):
int x;
int y;
Point p1;
Point p2;
List<Ellipses> ellipselijst = new List<Ellipses>();
List<Textbox> boxlijst = new List<Textbox>();
List<Lijn> lijnlijst = new List<Lijn>();
int actorselected;
int clicklocationx;
int clicklocationy;
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
Ellipses mooieellipse = new Ellipses(x, y);
ellipselijst.Add(mooieellipse);
Textbox mooiebox = new Textbox(x, y);
boxlijst.Add(mooiebox);
Lijn mooielijn = new Lijn(p1, p2);
lijnlijst.Add(mooielijn);
int clicklocationx = e.X;
int clicklocationy = e.Y;
this.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (radiolijn.Checked == true)
{
//Draw vanaf actor 1
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 1)
{
p1 = new Point (actor1.Location.X, actor1.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 2
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 2)
{
p1 = new Point(actor2.Location.X, actor2.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 3
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 3)
{
p1 = new Point(actor3.Location.X, actor3.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
}
}
private void actor1_Click(object sender, EventArgs e)
{
actorselected = 1;
}
private void actor2_Click(object sender, EventArgs e)
{
actorselected = 2;
}
private void actor3_Click(object sender, EventArgs e)
{
actorselected = 3;
}
級 「lijn」:
public class Lijn
{
public Point punt1;
public Point punt2;
public Lijn(Point p1, Point p2)
{
punt1 = p1;
punt2 = p2;
}
}
全碼:
public partial class Formcase : Form
{
int aantalactors = 0;
public Formcase()
{
InitializeComponent();
actor1.Visible = false;
actor2.Visible = false;
actor3.Visible = false;
}
private void addactorbtn_Click(object sender, EventArgs e)
{
aantalactors++;
if (aantalactors == 1)
{
actor1.Visible = true;
}
if (aantalactors == 2)
{
actor2.Visible = true;
}
if (aantalactors == 3)
{
actor3.Visible = true;
}
}
int x;
int y;
Point p1;
Point p2;
List<Ellipses> ellipselijst = new List<Ellipses>();
List<Textbox> boxlijst = new List<Textbox>();
List<Lijn> lijnlijst = new List<Lijn>();
int actorselected;
int clicklocationx;
int clicklocationy;
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
Ellipses mooieellipse = new Ellipses(x, y);
ellipselijst.Add(mooieellipse);
Textbox mooiebox = new Textbox(x, y);
boxlijst.Add(mooiebox);
Lijn mooielijn = new Lijn(p1, p2);
lijnlijst.Add(mooielijn);
int clicklocationx = e.X;
int clicklocationy = e.Y;
this.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (radioellipse.Checked == true)
{
foreach (Ellipses p in ellipselijst)
{
g.DrawEllipse(new Pen(Brushes.Black), new Rectangle(new Point(p.x - 50, p.y - 50), new Size(100, 100)));
}
}
if (radiobtntekst.Checked == true)
{
foreach (Textbox t in boxlijst)
{
TextBox txtbox = new TextBox();
txtbox.Location = new Point(t.x + 125, t.y + 25);
this.Controls.Add(txtbox);
txtbox.BringToFront();
}
}
if (radiolijn.Checked == true)
{
//Draw vanaf actor 1
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 1)
{
p1 = new Point (actor1.Location.X, actor1.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 2
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 2)
{
p1 = new Point(actor2.Location.X, actor2.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
//Draw vanaf actor 3
//p1 moet location van picture box zijn. p2 moet de location van mouseclick zijn
if (actorselected == 3)
{
p1 = new Point(actor3.Location.X, actor3.Location.Y);
p2 = new Point(clicklocationx, clicklocationy);
foreach (Lijn l in lijnlijst)
{
g.DrawLine(new Pen(Brushes.Black), p1, p2);
}
}
}
}
private void actor1_Click(object sender, EventArgs e)
{
actorselected = 1;
}
private void actor2_Click(object sender, EventArgs e)
{
actorselected = 2;
}
private void actor3_Click(object sender, EventArgs e)
{
actorselected = 3;
}
}
啊,你說得對。但是,如果我刪除它,那麼根本就沒有對clicklocationx的分配。在全球範圍內,我無法使用e.x,那麼我將如何去分配一個值? – user3287470
不要刪除賦值部分 - 只是兩個「int」頭 - 這些都是重新聲明變量。 - 是的,這是一個容易犯的錯誤。 –
啊,我現在明白了。所以用int來取消賦值。現在就工作吧,我的永恆感謝! – user3287470