Very simple attempt of using DrawLine failing - c#

I know this is trivial, but I don't understand why nothing is drawn on the Form1 after I click the button:
namespace GeometryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, PaintEventArgs e)
{
System.Drawing.Graphics gr = this.CreateGraphics();
gr.Clear(Color.White);
Pen pen = new Pen(System.Drawing.Color.Red,3);
gr.DrawLine(pen, 20, 20, 200, 250);
}
}
}

Problem : you are using PaintEventArgs as parameter in Button Click event.
i even doubt that this will compile if you subscribe the button1_Click Event Handler to Click event of the Button.
Solution 1:
You need to Subscribe/Register to Button Click event as below:
button1.Click += new System.EventHandler(button1_Click);
and EventHandler should be as below:
private void button1_Click(object sender, EventArgs e)
{
//event handling code here
}
Complete Code:
namespace GeometryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new System.EventHandler(button1_Click);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Graphics gr = this.CreateGraphics();
gr.Clear(Color.White);
Pen pen = new Pen(System.Drawing.Color.Red,3);
gr.DrawLine(pen, 20, 20, 200, 250);
}
}
}
Solution 2: if you want to draw the line on Form_Paint event you can do subscribe to Form_Paint Event as below:
You need to Subscribe/Register to Form Paint event as below:
this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
and EventHandler should be as below:
private void Form1_Paint(object sender, PaintEventArgs e)
{
//event handling code here
}
Complete Code:
namespace GeometryTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(Form1_Paint);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics gr = this.CreateGraphics();
gr.Clear(Color.White);
Pen pen = new Pen(System.Drawing.Color.Red, 3);
gr.DrawLine(pen, 20, 20, 200, 250);
}
}
}

Related

Show TextBox on every TabPage

I attached a TextBox to the first TabPage of a TabControl. I would like to display the same TextBox object on every TabPage. I tried to add the control to the tabControl Collection but unfortunately it's not working.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
Button b;
public Form1()
{
InitializeComponent();
b = new Button() { Text = "Prueba" };
}
private void Form1_Load(object sender, EventArgs e)
{
AddButtonToTabControl();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
AddButtonToTabControl();
}
public void AddButtonToTabControl()
{
tabControl1.SelectedTab.Controls.Add(b);
}
I missed two methods. It's working now!
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}

C# paint graphics from class in picturebox with mouse click

I want to paint a graphics object from a method (paint) I created in a separate class (Paintball). I want it to paint in a picturebox only when I left-click with my mouse and I want the point where I shoot to be stored in a List. When I try the code below, it doesn't shoot. Below is the class Paintball.
{
private List<Point> myClick;
public Paintball()
{
myClick = new List<Point>();
}
public void add(Point location)
{
myClick.Add(location);
}
public void paint(Graphics g, Point point)
{
g.FillEllipse(Brushes.Blue, point.X, point.Y, 20, 20);
}
}
}
This is form1 below.
namespace AmazingPaintball
{
public partial class Form1 : Form
{
Random positionX = new Random();
Random positionY = new Random();
Target einstein;
int count;
List<Point> ballList = new List<Point>();
Paintball gun;
public Form1()
{
InitializeComponent();
Point point = new Point(positionX.Next(0, 638), positionY.Next(0, 404));
einstein = new Target(point);
ptrEinstein.Location = point;
gun = new Paintball();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
ptrEinstein.Location = einstein.Move(e.KeyData);
pictureBox1.Update();
pictureBox1.Refresh();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
count++;
gun.add(e.Location);
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var Paintball in ballList)
{
gun.paint(e.Graphics, this.PointToClient(Cursor.Position));
pictureBox1.Refresh();
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Refresh();
}
}
}
Please let me know if you know what has to be edited/created. Thank You
Your original code has many mistakes. Let's try to simplify what you are doing and tackle simply storing a list of points and drawing them to the picturebox.
public partial class Form1 : Form
{
List<Point> ballList = new List<Point>();
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
ballList.Add(e.Location);
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (Point pBall in ballList)
{
e.Graphics.FillEllipse(Brushes.Blue, pBall.X, pBall.Y, 20, 20);
}
}
}
Here we have a list, we add the click points to it in the click handler and paint them in the paint handler. Once you get comfortable with this, perhaps move to the next task in your program and ask a new question if you get stuck with the next feature.
Ok, I've got a bit of time, so let's look at your paintball class. I've renamed it Paintballs since it contains many of them and this name is more appropriate. If you want to keep the list of points private that's ok. You are trying to implement a Paint method in the class, but it takes a Point as argument and does not operate on any of the class's instance state - this probably isn't what you want. Consider now :
public class Paintballs
{
private List<Point> myClick;
public Paintballs()
{
myClick = new List<Point>();
}
public void Add(Point location)
{
myClick.Add(location);
}
public void Paint(Graphics g)
{
foreach (Point p in myClick)
{
g.FillEllipse(Brushes.Blue, p.X, p.Y, 20, 20);
}
}
}
Here we have a public Paint method that will draw all of the paintballs in the class to any graphics instance you pass to it. Now your form code would look like :
public partial class Form1 : Form
{
Paintballs pBalls = new Paintballs();
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pBalls.Add(e.Location);
pictureBox1.Refresh();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
pBalls.Paint(e.Graphics);
}
}
So we've simplified the form code by pushing the painting method into the paintballs class itself. This makes the class responsible for knowing what the paintballs look like, how many there are, where they are, and how to draw them to a Graphics object. This is step 1 in encapsulating responsibility.
You're drawing from a list of points stored in that ballList variable. However, you've never added any points to that list.
Make the myClick list in Paintball public and, in the pictureBox1_Paint method, iterate through that list instead of ballList.

Controlling a PictureBox from a class

I have a little project in C#, (Windows Forms Application). I have on the form 77 PictureBoxes (pictureBox1, pictureBox2, pictureBox3, ...) and I want to control them but from a new class (Access.cs), by declaring a new one picturebox in the class to control all the pictures.
Because it would be too long if I pass through each pictureBox and add a click method and Copy + Paste the code and change the pictureBox number each time.
I've set the pictures as public and tried the following code:
Access.cs:
using System.Windows.Forms;
public class Access
{
PictureBox picBox = new PictureBox();
public void PictureClicked()
{
picBox.Image = Properties.Resources.apple;
}
}
Form1.cs:
private void pictureBox1_Click(object sender, EventArgs e)
{
Access ac = new Access();
ac.PictureClicked();
}
but the code didn't work!!
I dont really get what you want to do but you could try to send the object to your Access class:
private void pictureBox1_Click(object sender, EventArgs e)
{
Access ac = new Access();
ac.PictureClicked(sender);
}
public void PictureClicked(Object Sender)
{
picBox = (PictureBox)Sender;
picBox.Image = Properties.Resources.apple;
}
Access.Cs
public void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox pi = (PictureBox)sender;
pi.Image = Properties.Resources.alert__2_;
}
Form1.Cs
private void pictureBox2_Click(object sender, EventArgs e)
{
Form1 c =new Form1();
c.pictureBox1_Click(sender, e);
}
Here pictureBox2_Click this event for all picturebox

Using the OnPaint() method

I am using this library to generate QRcode into a WinForm application, but I don't really know how to take use of the OnPaint() method.
So I have this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode;
encoder.TryEncode("link to some website", out qrCode);
new GraphicsRenderer(new FixedCodeSize(200, QuietZoneModules.Two))
.Draw(e.Graphics, qrCode.Matrix);
base.OnPaint(e);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Invalidate();
}
}
I have a simple pictureBox in the form and I just want to generate the QRcode image in there (if it is possible to generate it in a picturebox).
If you're putting your image in a picturebox and you're only producing your image once, then you don't need to worry about the paint method (you're not doing an animation etc, it's just a QR code)
Just do this in your form load (or where ever you produce your image)
mypicturebox.Image = qrCodeImage;
Update - additional code to facilitate your library
var bmp = new Bitmap(200, 200);
using (var g = Graphics.FromImage(bmp))
{
new GraphicsRenderer(
new FixedCodeSize(200, QuietZoneModules.Two)).Draw(g, qrCode.Matrix);
}
pictureBox1.Image = bmp;
This is what I eventually did:
public partial class Form1 : Form
{
public event PaintEventHandler Paint;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
this.Controls.Add(pictureBox1);
}
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode;
encoder.TryEncode("www.abix.dk", out qrCode);
new GraphicsRenderer(
new FixedCodeSize(200, QuietZoneModules.Two)).Draw(e.Graphics, qrCode.Matrix);
}
}

simple move-after-cursor problem c# winforms

I have a winform app and when i click on a button, i would like to have a string text move after my cursor. I've written some code but it doesn't seem to work . please Help !
Here is the code :
private void corectionBrushToolStripMenuItem_Click(object sender, EventArgs e)
{
this.MouseMove += new MouseEventHandler(On_MouseMove);
this.Paint += new PaintEventHandler(DrawRect);
}
private void DrawRect(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("da",Font,Brushes.Black,new Point(mouseMoveX,mouseMoveY));
}
public void On_MouseMove(object sender, MouseEventArgs mouseEv)
{
mouseMoveX = mouseEv.X;
mouseMoveY = mouseEv.Y;
this.Invalidate();
}
Regards,
Alex Badescu
This is an example that does what you want I think. the offset in the mouse move is so that it shows up not under the mouse.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winap
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool DrawText = false;
private void button1_Click(object sender, EventArgs e)
{
DrawText = !DrawText;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if(DrawText)
{
if (lp != p)
{
this.Invalidate();
}
e.Graphics.DrawString("hi", SystemFonts.DefaultFont, Brushes.Green, p);
lp = p;
}
}
private PointF p;
private PointF lp;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
p = new PointF(e.X -10, e.Y);
this.Invalidate();
}
}
}
How about:
Add the desired text to a label control, and change the position in MouseMove.
Following works fine:
int mX = 0;
int mY = 0;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
mX = e.X;
mY = e.Y;
this.Invalidate();
}
Random rr = new Random(123123);
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("This a test"+rr.Next().ToString(), Font, Brushes.Black, new Point(mX, mY));
}

Categories

Resources