473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Growing line from a point in C#

Hi every one

I have really got stuck in writting a piece of code.I suppose to create a form with a button on it. when ever button is clicked, a line which is stated from a certain point of the form(200,200) should grow upward.
its the code i have written
namespace ConsoleApplication1
{
class Program :Form
{
int x=202;
int y=202;
public Program()
{
this.Size = new Size(400, 400);
this.BackColor = Color.Wheat;
this.StartPosition = FormStartPosition.CenterScreen;
Button b1 = new Button();
this.Controls.Add(b1);
b1.Click+=new EventHandler(b1_Click);


}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(brush, 4);
g.DrawLine(pen, 200, 200, 200, 201);


}
protected void b1_Click(object sender, EventArgs e)
{

x = 200;
y = 20;

}


button doesn't work! any suggestions??
thank u in advanced
Oct 25 '07 #1
18 1947
Plater
7,872 Expert 4TB
There are a few logical/coding errors here.

Lets start with your button click:
Expand|Select|Wrap|Line Numbers
  1. protected void b1_Click(object sender, EventArgs e)
  2. {
  3. x = 200;
  4. y = 20;
  5. }
  6.  
You're setting the adsolute value of your variables instead of incrementing them.
Try:
Expand|Select|Wrap|Line Numbers
  1. protected void b1_Click(object sender, EventArgs e)
  2. {
  3.    y = y-20;
  4. }
  5.  

Now onto your paint function:
Expand|Select|Wrap|Line Numbers
  1. protected override void OnPaint(PaintEventArgs e)
  2. {
  3. Graphics g = e.Graphics;
  4. Brush brush = new SolidBrush(Color.Black);
  5. Pen pen = new Pen(brush, 4);
  6. g.DrawLine(pen, 200, 200, 200, 201);
  7. }
  8.  
You are drawing the same line everytime, at no point do you use your x,y values that you are storing.
Try this:
Expand|Select|Wrap|Line Numbers
  1. protected override void OnPaint(PaintEventArgs e)
  2. {
  3. Graphics g = e.Graphics;
  4. Brush brush = new SolidBrush(Color.Black);
  5. Pen pen = new Pen(brush, 4);
  6. g.DrawLine(pen, 200, 200, x,y);
  7. }
  8.  

That should be enough to get you started.
Oct 25 '07 #2
There are many logical/coding falicies here.

Lets start with your button click:
I tried threads as well, do u wantto take a look at the code?

class Program :Form
{
int x1=202;
int y1=202;

int y2;
private Thread animationThread;

public Program()
{
this.Size = new Size(400, 400);
this.BackColor = Color.Wheat;
this.StartPosition = FormStartPosition.CenterScreen;
Button b1 = new Button();
this.Controls.Add(b1);
b1.Click+=new EventHandler(b1_Click);
animationThread = new Thread(new ThreadStart(growingLine));

}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(brush, 4);
g.DrawLine(pen, 200, 200, x1, y2);


}
protected void b1_Click(object sender, EventArgs e)
{

animationThread.Start();
}

public void growingLine()
{
y1 = y2;
y2++;
Invalidate();
Thread.Sleep(40);
}
Oct 25 '07 #3
Plater
7,872 Expert 4TB
Sorry, I was in the middle of editing my post with the relevant information, please scroll up and have a look.
Oct 25 '07 #4
Sorry, I was in the middle of editing my post with the relevant information, please scroll up and have a look.
I tried ur code, actually when i execute it, a line(200,200,200,180) is created.Program doesn't go throght the "b1_click" method
Oct 25 '07 #5
Plater
7,872 Expert 4TB
If it drew the line up to 180, then it performed the button click.

Is the line supposed to GROW or MOVE? I gave you code for GROWing
Oct 25 '07 #6
I tried ur code, actually when i execute it, a line(200,200,200,180) is created.Program doesn't go throght the "b1_click" method
it should grow by clicking on button , not by opening the form.
Now I have some progress in the code...
class Program :Form
{
int x1=200;
int y1=200;
int y2=200;
private Thread animationThread;

public Program()
{
this.Size = new Size(400, 400);
this.BackColor = Color.Wheat;
this.StartPosition = FormStartPosition.CenterScreen;
Button b1 = new Button();
this.Controls.Add(b1);
b1.Click+=new EventHandler(b1_Click);
animationThread = new Thread(new ThreadStart(growingLine));

}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(brush, 4);
while(y2>50)
{
g.DrawLine(pen, x1, y1, x1, y2);
Thread.Sleep(10);
Invalidate();
y1 = y2;
y2--;
}


}
protected void b1_Click(object sender, EventArgs e)
{
if (animationThread.ThreadState == ThreadState.Unstarted)
{

animationThread.Start();
}
else if (animationThread.ThreadState == ThreadState.Running ||
animationThread.ThreadState == ThreadState.WaitSleepJoin)
{

animationThread.Suspend();
}
else if (animationThread.ThreadState == ThreadState.Suspended)
{

animationThread.Resume();
}

}

public void growingLine()
{
y1 = y2;
y2++;
Invalidate();
Thread.Sleep(40);
}

but , the problem is, i want the line to grow up when i click on the button, but it growa up as soon as the form is open!!
Oct 25 '07 #7
Plater
7,872 Expert 4TB
You are creating an endless loop in your onPaint() function by moving it and then telling it to paint again.
Oct 25 '07 #8
You are creating an endless loop in your onPaint() function by moving it and then telling it to paint again.
There is a while loop in OnPaint:while(y2>20)....
how if i move OnPaint in the other class?

class Program :Form
{

private Thread animationThread;
private GrowingLine growingline;

public Program()
{
this.Size = new Size(400, 400);
this.BackColor = Color.Wheat;
this.StartPosition = FormStartPosition.CenterScreen;
Button b1 = new Button();
this.Controls.Add(b1);
b1.Click+=new EventHandler(b1_Click);
growingline = new GrowingLine();
animationThread = new Thread(new ThreadStart(growingline.StartGrowing));

}

protected void b1_Click(object sender, EventArgs e)
{
if (animationThread.ThreadState == ThreadState.Unstarted)
{

animationThread.Start();
}
else if (animationThread.ThreadState == ThreadState.Running ||
animationThread.ThreadState == ThreadState.WaitSleepJoin)
{

animationThread.Suspend();
}
else if (animationThread.ThreadState == ThreadState.Suspended)
{

animationThread.Resume();
}

}

public static void Main(string[] args)
{
Application.Run(new Program());
Application.Exit();
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// Program
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Program";
this.Load += new System.EventHandler(this.Program_Load);
this.ResumeLayout(false);

}

private void Program_Load(object sender, EventArgs e)
{

}


}
class GrowingLine:Panel
{
int x1;
int y1;
int y2;

public GrowingLine()
{
x1 = 200;
y1 = 200;
y2 = 200;
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Black);
Pen pen = new Pen(brush, 4);
while (y2 > 60)
{
g.DrawLine(pen, x1, y1, x1, y2);
Thread.Sleep(10);
Invalidate();
y1 = y2;
y2--;
g.Dispose();
}
}

public void StartGrowing()
{
y1 = y2;
y2++;
Invalidate();
Thread.Sleep(40);
}


}


OnPaint doesn't work in this case.
Oct 25 '07 #9
Plater
7,872 Expert 4TB
I think you really need to go back and read some tutorials on the basics, event handling and the onPaint event...
Oct 25 '07 #10
I think you really need to go back and read some tutorials on the basics, event handling and the onPaint event...
do u have any suggestions? (tutorials?)
Oct 25 '07 #11
Plater
7,872 Expert 4TB
I was able to accomplish the task with the code I provided (slightly modified to have it grow upward automatically)

Expand|Select|Wrap|Line Numbers
  1. private int x = 200;
  2. private int y = 202;
  3.  
  4.         private void myform_Load(object sender, EventArgs e)
  5.         {
  6.             Button bt = new Button();
  7.             bt.Name = "bt";
  8.             bt.Text = "Up";
  9.             bt.Click += new EventHandler(bt_Click);
  10.             this.Controls.Add(bt);
  11.         }
  12.  
  13.         void bt_Click(object sender, EventArgs e)
  14.         {
  15.             AnimateUp();
  16.         }
  17.  
  18.         private void AnimateUp()
  19.         {
  20.             int NumSteps = 10;
  21.             int StepSize = 10;
  22.             for (int i = 0; i < NumSteps; i++)
  23.             {
  24.                 y = y - StepSize;
  25.                 this.Invalidate();
  26.                 this.Refresh();
  27.                 System.Threading.Thread.Sleep(100); 
  28.  
  29.             }
  30.         }
  31.  
  32.         private void myform_Paint(object sender, PaintEventArgs e)
  33.         {
  34.             Graphics g=e.Graphics; 
  35.             Pen blackpen = new Pen(Color.Black,4);
  36.             g.DrawLine(blackpen, 200, 200, x, y);
  37.         }
  38.  

msdn has lots of great tutorials and help on .NET
Oct 25 '07 #12
This code has the same problem as mine..no line is painted on the form!!!
Oct 25 '07 #13
Plater
7,872 Expert 4TB
Works fine for me.
Did you set that paint function to be the paint event handler for the form?
Oct 25 '07 #14
nah...How should i do it?
Oct 25 '07 #15
Plater
7,872 Expert 4TB
in the form_load event add:
this.Paint += new PaintEventHandler(myform_Paint);
Oct 25 '07 #16
I got this error:

No overload for 'myform_Paint' mtaches delegate 'System.Windows.Forms.PaintEventHandler'.
Oct 25 '07 #17
Plater
7,872 Expert 4TB
Are you sure you're in .net?
What version of visual studio are you using?
These are pretty basic tasks.

Here's what I used:
Attached Files
File Type: zip animateExample.zip (2.9 KB, 81 views)
Oct 25 '07 #18
I use visual c# 2008
Oct 25 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

41
by: Nitin Bhardwaj | last post by:
Hi all, I wanted to know whether the stack in a C program is growing upwards or downwards.So I wrote a little code to see that.Please guide me as to whether this code is correct in telling this...
1
by: Ivan Vinogradov | last post by:
Hello All, this seems like a trivial problem, but I just can't find an elegant solution neither by myself, nor with google's help. I'd like to be able to keep an array representing coordinates...
4
by: mankolele | last post by:
Hi all I am working on a new project where it has to have a dynanically growing table I guess I am going to have to use loops or two array function . Every time a project is added it must appear in...
1
by: shimajavar | last post by:
Hi every one I have really got stuck in writting a piece of code.I suppose to create a form with a button on it. when ever button is clicked, a line which is stated from a certain point of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.