I am trying to do a logical gates program. I'm trying to create a PictureBox with the class NOT, the problem is that it doesn't appear when I call the create method inside form1 and the PictureBoxwon't appear when I click the list item. The problem is (I think) that it doesn't know that it is in form1 even though I use the FindForm() method.
And call it from forms
---Source Code for NoT class---
class NOT: Shape
{
PictureBox px = new PictureBox();
Image img = Image.FromFile(#"C:\NOT.png");
public NOT(int x, int y) : base(x,y)
{
px.FindForm();
px.Visible = true;
px.Enabled = true;
}
public override void CreatePicture()
{
Point p1 = new Point(xx, yy);
px.Image = img;
px.Location = p1;
px.Show();
}
}
---Source code for the SHape Class---
abstract class Shape
{
protected int xx, yy; //private Point location;
public Shape(int X, int Y)
{
xx = X;
yy = Y;
}
public abstract void CreatePicture();
}
private void nOTToolStripMenuItem_Click(object sender, EventArgs e)
{
nt.CreatePicture();
}
NOT nt = new NOT(12,23);
You need to associate the picture box with a form by adding it to the forms Controls collection. Calling FindForm() only returns the currently assigned form; in your case it will be returning null.
public override void CreatePicture(Form form)
{
Point p1 = new Point(xx, yy);
px.Image = img;
px.Location = p1;
form.Controls.Add(px);
px.Show();
}
You must add the pictureBox. For example, if the PictureBox is in a panel:
panel.Controls.Add();
if it is in the form you just put Controls.Add();
Hope it helps.
You must place PictureBox to the form for draw it:
PictureBox px = new PictureBox();
....
px.Parent = YouFormForExample;//Component who is draw this picture box
Related
I need to change the image by clicking on pictureBox, but when I click it again I can't return former picture
Here is my code:
private void PictureBox_Click(object sender, EventArgs e)
{
if (pictureBox.Image == Properties.Resources.openeye)
pictureBox.Image = Properties.Resources.closeeye;
else
pictureBox.Image = Properties.Resources.openeye;
}
How can I fix it?
Here's an example that demonstrates it with two images. One of an "x" another of an "o".
As you can see, the form has two instance variables x and o to store each of the Image objects. There is another flag field called isX which the ClickHandle event handler uses to check which image is currently displayed and switch to the other image, the event handler then toggles the isX field so the next click responds properly.
public void Main(string[] args)
{
var f1 = new Form1(); // form instance that holds the PictureBox
Task.Run(() => Application.Run(f1)); //I'm running this from LINQPad, but this would also work in a console application.
}
public class Form1 : Form // Derives from the Form class
{
private bool isX; // private instance variable to indicate which image is diplayed
private Image x; // private instance variable storing the x image
private Image o; // private instance variable storing the o image
// the picture box this form uses
private PictureBox p;
public Form1()
{
// load the images from wherever they are stored.
// I do this at construction time to avoid doing disk IO when clicking
x = Image.FromFile(#"C:\image\path\x.png");
o = Image.FromFile(#"C:\image\path\o.png");
// Initialize the picture box
p = new PictureBox {
Name = "p1",
Size = new Size(100,100),
Location = new Point(100,100),
Image = o //Initialize with the o image
};
// register the click event handler
p.Click += this.ClickHandle;
// set the flag to false, since the o image is what we start with
this.isX = false;
// add PictureBox p to the form
this.Controls.Add(p);
}
// handles the click action, registered to the PictureBox.Click event
private void ClickHandle(object sender, EventArgs e)
{
// use the flag to check which image is shown, and display the other image
if(this.isX) // this might work with your image == check, I didn't test it
{
p.Image = this.o;
}
else
{
p.Image = this.x;
}
// set the flag to the opposite of whatever the flag currently is
this.isX = ! isX;
}
}
You can set value of picturebox image, but you cant get it that way.
U can use global variable out of method and go this way.
int i = 0;
private void PictureBox1_Click(object sender, EventArgs e)
{
if (i == 0)
{
pictureBox1.Image = Properties.Resources.close;
i++;
}
else
{
pictureBox1.Image = Properties.Resources.open;
i--;
}
}
You can use this.
private void pictureBox1_Click(object sender, EventArgs e)
{
if (pictureBox1.Image!=null&& getSignatureLen( pictureBox1.Image) == getSignatureLen(Properties.Resources.openeye))
{
pictureBox1.Image = Properties.Resources.closeeye;
}
else
{
pictureBox1.Image = PProperties.Resources.openeye;
}
}
public long getSignatureLen(Image img)
{
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
img.Save(mStream, img.RawFormat);
return mStream.Length;
}
}
You can save the value in the un-used Tag property, of the PictureBox object.
pictureBox1.Tag = 1; //Set it to whatever
if (pictureBox1.Tag == "1") { } //Do your check
I have a WinForms program where the user clicks on a PictureBox control.
I want a small red dot (a few pixels across) every time the user clicks.
I also don't want any of the previous dots to go away.
I know I will need a generic list of ellipses and rectangles, but am not sure how to execute this. How would I go about doing this?
In my program a pictureBox1_Click method handles mouse click events and returns the position of the clicks.
A pictureBox1_Paint method handles the graphics to be drawn on these points.
You have to create a container that can reference the Points collection and add one point to the collection each time you click on a paint-able Control.
Maybe, you want to also create different kinds of drawing points, based on some conditions or requirements.
Thus, you need to store also these extra properties, not just a point coordinate.
If so, you need a specialized object that can expose these properties when needed.
So, here's a custom Class object with some simple properties, that lets you define a Point's Color and Size. For each of its Points collection.
It also implements the IDisposable interface, because we need to create a Pen object for each Point we draw. And a Pen object needs to be disposed (implements IDisposable).
To perform the drawing, you just need to call Control.Invalidate() (pictureBox1.Invalidate() in the example). This causes the repainting of the invalidated parts of the control, raising the OnPaint() event.
Each point (that needs to be repainted) is drawn using e.Graphics.DrawEllipse().
You can test it this way:
With predefined properties, using just a Mouse pointer coordinate:
myPoints.Add(new MyPoints.DrawingPoint(e.Location));
With specific properties when something different is needed:
With a size of 8x8 pixels
newPoint.Dot = new Rectangle(e.Location, new Size(8, 8)));.
With an orange Pen of 2 pixels in size
newPoint.DrawingPen = new Pen(Color.Orange, 2);
Add this new Point to the collection
myPoints.DrawingPoints.Add(newPoint);
The Clear() method is used to Dispose() the current list of Points and create a new, empty, List:
MyPoints.Clear();
Sample implementation:
MyPoints myPoints = new MyPoints();
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
//Use default property values
//myPoints.Add(new MyPoints.DrawingPoint(e.Location));
MyPoints.DrawingPoint newPoint = new MyPoints.DrawingPoint();
newPoint.Dot = new Rectangle(e.Location, new Size(4, 4));
newPoint.DrawingPen = new Pen(Color.Red, 2);
myPoints.DrawingPoints.Add(newPoint);
(sender as Control).Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (MyPoints.DrawingPoint mypoint in myPoints.DrawingPoints) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(mypoint.DrawingPen, mypoint.Dot);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
myPoints.Dispose();
}
The Point objects collection Class container:
internal class MyPoints : IDisposable
{
bool IsDisposed = false;
public MyPoints() => DrawingPoints = new List<DrawingPoint>();
public List<DrawingPoint> DrawingPoints { get; set; }
public void Add(DrawingPoint NewPoint)
{
if (NewPoint.Dot.Size.Width > 1 && NewPoint.Dot.Size.Height > 1) {
DrawingPoints.Add(NewPoint);
}
}
public void Clear()
{
this.Dispose();
this.DrawingPoints.Clear();
this.DrawingPoints = new List<DrawingPoint>();
}
public void Remove(Point point)
{
Remove(this.DrawingPoints.Select((p, i) => { if (p.Dot.Contains(point)) return i; return -1; }).First());
}
public void Remove(int Index)
{
if (Index > -1) {
this.DrawingPoints[Index].Delete();
this.DrawingPoints.RemoveAt(Index);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool IsSafeDisposing)
{
if (IsSafeDisposing && (!this.IsDisposed) && (this.DrawingPoints.Count > 0)) {
foreach (DrawingPoint dp in this.DrawingPoints)
if (dp != null) dp.Delete();
}
}
public class DrawingPoint
{
Pen m_Pen = null;
Rectangle m_Dot = Rectangle.Empty;
public DrawingPoint() : this(Point.Empty) { }
public DrawingPoint(Point newPoint)
{
this.m_Pen = new Pen(Color.Red, 1);
this.m_Dot = new Rectangle(newPoint, new Size(2, 2));
}
public Pen DrawingPen { get => this.m_Pen; set => this.m_Pen = value; }
public Rectangle Dot { get => this.m_Dot; set => this.m_Dot = value; }
public void Delete()
{
if (this.m_Pen != null) this.m_Pen.Dispose();
}
}
}
This is how it can work, changing its properties when required:
Basically you must use GDI+
Check the code bellow:
private void pictureBox1_Click(object sender, EventArgs e)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
Pen p = new Pen(Color.Red, 3);
var cursorPosition = pictureBox1.PointToClient(Cursor.Position);
g.DrawEllipse(p, cursorPosition.X, cursorPosition.Y, 15, 15);
MyCircles.Add(cursorPosition);
pictureBox1.Refresh();
}
List<Point> MyCircles = new List<Point>();
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
foreach (var item in MyCircles)
{
MessageBox.Show($"One circle was created: X:{item.X}, Y:{item.Y}");
}
}
The MyCircles List is only if you want to store the circles to reshow the image with the circles later, and the doubleclick is a sample of using it (i mean persistency purposes, because the circles will not leave the image until you close your app). Check other g methods to draw lines, rectangles or whatever. (Uses System.Drawing namespace)
I came up with a simple solution for my problem
I made a list of rectangles:
List<Rectangle> rects = new List<Rectangle>();
And added rectangles with ever click:
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
rects.Add(new Rectangle(e.Location, new Size(4,4)));
}
And painted the points:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var rect in rects)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(magenta,rect);
}
}
And added a method for clearing drawings with rects.Clear()
My code paints a circle on Form 2 when coordinates of circles are entered in textboxes in form 1 and button is clicked. The problem is, every time the coordinates are entered in Form 1, A new Form 2 is opening rather than old one getting updated.
Code on 1st Form
private void button1_Click(object sender, EventArgs e)
{
int r1, r2;
setValue = textBox1.Text;
setValue1 = textBox2.Text;
Int32.TryParse(setValue, out r1);
Int32.TryParse(setValue1, out r2);
Form2 f2 = new Form2();
//// f2.Show();
// f2.addcoordinate(r1,r2);
// f2.Update();
Graphics g2;
g2 = f2.CreateGraphics();
Class1 add = new Class1();
add.addcoordinate(r1,r2);
}
Code in Class1
public void addcoordinate(int r1, int r2)
{
// MessageBox.Show(r1.ToString());
Form2 f2 = new Form2();
f2.addcoordinate(r1, r2);
f2.Show();
}
Code on Form2
private List<Point> circleCoordinates = new List<Point>();
public Form1()
{
InitializeComponent();
}
public void addcoordinate(int r1, int r2)
{
this.circleCoordinates.Add(new Point(r1, r2));
}
protected override void OnPaint(PaintEventArgs e)
{
// linedrawing goes here
foreach (Point point in this.circleCoordinates)
{
e.Graphics.DrawEllipse(Pens.Black, new Rectangle(point, new
Size(10, 10)));
}
base.OnPaint(e);
}
Please suggest.
In Form1 you defined f2 as follows:
Form2 f2 = new Form2();
Every time this line of code is run, it creates a new instance of the object. That's why you see a new form each time you click on your button.
Define the Form2 object inside the Form1 class and out of all the private methods by moving the above mentioned line of code outside the methods in the class. Then use the specific instance of Form2 that you have declared (f2 in this case), in the code inside your methods. This way, you are working on the same instance of the class and you are not creating new instances of the object Form2 every time you click on button1.
In my application I have a mainform. When the open button is clicked I want to show a second (borderless) form whith the text loading. I've got this working so far.
But what I want is that the loading form is centered relative to the mainform. How do I do this?
SOLUTION:
private void tsbOpen_Click(object sender, EventArgs e)
{
if (_fileDialog.ShowOpenDialog() == DialogResult.OK)
{
_progress = new frmProgress(); // _progress is a member var
backgroundWorker1.RunWorkerAsync("open");
_progress.ShowDialog(this);
}
}
You can set StartPosition to CenterParent and pass the mainform as an Owner.
I created a subform named ProcessingRequest and I put some text and an animated gif on it.
I have a Property in my main form that calculates the location my sub form should be in.
private Point ProcessingLocation { get { return new Point(this.Location.X + this.Width / 2 - new ProcessingRequest().Width / 2, this.Location.Y + this.Height / 2 - new ProcessingRequest().Height / 2); } }
I have a class that makes a new thread to show the sub form.
public class ShowProgress
{
static private System.Drawing.Point point;
static private ProcessingRequest p;
static public void ShowProgressForm(System.Drawing.Point myPoint)
{
point = myPoint;
Thread t = new Thread(new ThreadStart(ShowProgress.ShowForm));
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
static private void ShowForm()
{
p = new ProcessingRequest();
p.StartPosition = FormStartPosition.Manual;
p.Location = point;
p.TopMost = true;
Application.Run(p);
}
static public void CloseForm()
{
p.Invoke(new CloseDelegate(ShowProgress.CloseFormInternal));
}
static private void CloseFormInternal()
{
p.Close();
}
}
public delegate void CloseDelegate();
Then in my main form I simply put
ShowProgress.ShowProgressForm(ProcessingLocation);
//heavy processing code goes here or whatever
ShowProgress.CloseForm();
:)
Martijn try this
at the start of the method put some code like this
public sub Bah()
{
if (me.InvokeRequired)
{
me.Invoke(new action(Bah));
return
}
myform.showdialog...
}
dont know if this code compiles to 100% but you get the idea
Get the position of the main form coordinates and its size and take the size of child form and put some simple mathematics on it.
Having hard time understanding classes and why I can't access certain object.
How can i modify the code so I can change "map"(which is a bunch of labels) properties in all of my classes/events?
The method Draw2d() creates a couple of labels on the main form that I wish to change on different events(button click in this example).
Can someone help me, or just hint me into the right direction.
The Code:
public partial class Form1 : Form
{
public void Draw2d()
{
const int spacing = 20;
Label[][] map = new Label[5][];
for (int x = 0; x < 5; x++)
{
map[x] = new Label[5];
for (int y = 0; y < 5; y++)
{
map[x][y] = new Label();
map[x][y].AutoSize = true;
map[x][y].Location = new System.Drawing.Point(x * spacing, y * spacing);
map[x][y].Name = "map" + x.ToString() + "," + y.ToString();
map[x][y].Size = new System.Drawing.Size(spacing, spacing);
map[x][y].TabIndex = 0;
map[x][y].Text = "0";
}
this.Controls.AddRange(map[x]);
}
}
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Draw2d();
}
private void button1_Click(object sender, EventArgs e)
{
map[0][0].Text = "1"; // <-- Doesn't work
}
}
Thanks!
you have to declare the map as property(global to class)
public partial class Form1 : Form {
public Label[][] map;
....
}
then you can use inside class like
this->map[...][...]
or from outside like
objClass->map[...][...]
My guess is that you added
public Label[][] map;
but forgot to change the second line of Draw2d from
Label[][] map = new Label[5][];
to
map = new Label[5][];
I just tried your code, and it works fine if you change those two lines. If that's not the problem, could you say what error you're getting, please?