473,464 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Growing line from a point in C#

17 New Member
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 1948
Plater
7,872 Recognized Expert Expert
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
shimajavar
17 New Member
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 Recognized Expert Expert
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
shimajavar
17 New Member
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 Recognized Expert Expert
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
shimajavar
17 New Member
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 Recognized Expert Expert
You are creating an endless loop in your onPaint() function by moving it and then telling it to paint again.
Oct 25 '07 #8
shimajavar
17 New Member
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 Recognized Expert Expert
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
shimajavar
17 New Member
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 Recognized Expert Expert
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
shimajavar
17 New Member
This code has the same problem as mine..no line is painted on the form!!!
Oct 25 '07 #13
Plater
7,872 Recognized Expert Expert
Works fine for me.
Did you set that paint function to be the paint event handler for the form?
Oct 25 '07 #14
shimajavar
17 New Member
nah...How should i do it?
Oct 25 '07 #15
Plater
7,872 Recognized Expert Expert
in the form_load event add:
this.Paint += new PaintEventHandler(myform_Paint);
Oct 25 '07 #16
shimajavar
17 New Member
I got this error:

No overload for 'myform_Paint' mtaches delegate 'System.Windows.Forms.PaintEventHandler'.
Oct 25 '07 #17
Plater
7,872 Recognized Expert Expert
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
shimajavar
17 New Member
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.