I am trying to make a tool that allows me to choose a certain location on a picturebox to put text from a textbox on. It will need to be able to place multiple different texts on the picturebox and then be able to be deleted. This is my current code:
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 TextboxTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
Graphics G = Graphics.FromImage(pictureBox1.Image);
G.DrawString(textBox1.Text, new Font("Tahoma", 40), Brushes.Black, new Point(MousePosition.X, MousePosition.Y));
}
}
}
At the moment i can type the text in the textbox, but can't draw the string on the picturebox and choose its location. I have a button which is meant to confirm the text written is right and then allow the user to choose its location. Please can someone help me sort this code out?
Thanks-
The MousePosition property is relative to the screen, not the PictureBox.
You should handle the MouseClick event and draw the string at e.X and e.Y.
Alternatively, you can call pictureBox1.PointToClient to transform screen coordinates to control-relative coordinates.
Also, you should dispose the Graphics object in a using statement.
Finally, I'm pretty sure you'll need to call pictureBox1.Invalidate() after modifying the image to force it to repaint.
Related
What is needed to define 'canvas' on line 27? I've looked everywhere but it only gives respoenses of why an object might not be defined, and not about why the specific object canvas is not defined. Can someone tell me what i'm Missing?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void canvas_Paint(object sender, PaintEventArgs e)
{
Graphics gObject = canvas.CreateGraphics();
Brush red = new SolidBrush(Color.Red);
Pen redPen = new Pen(red, 8);
gObject.DrawLine(redPen, 10, 10, 35, 500);
}
}
}
That canvas must be a control in the form. You got to add it in the designer and give it the canvas name. What kind? Hmm… I'd say use a PictureBox.
You would also need to link the Paint event to canvas_Paint from the properties panel (events tab). And yes, e.Graphics is preferred over using CreateGraphics.
Where did you get that code from?
I'm trying to use the FlowLayoutPanel with linkLabels in Visual Studio for a quick project. I've selected "TopDown" for direction and wrapping to false. When I launch the program; however, the direction always shows left to right. Is there a box or something that I haven't checked? Or is there any reason a linklabel would ignore the flow direction?
Here's my code and some screenshots of what I see.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace myProject
{
public partial class Form1 : Form
{
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
linkLabel1.LinkClicked += linkLabel1_LinkClicked;
linkLabel2.LinkClicked += linkLabel2_LinkClicked;
linkLabel3.LinkClicked += linkLabel3_LinkClicked;
Controls.Add(panel);
panel.Controls.Add(linkLabel1);
panel.Controls.Add(linkLabel2);
panel.Controls.Add(linkLabel3);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel1, 0);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel2, 0);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel.Controls.SetChildIndex(linkLabel3, 0);
}
}
}
This is the control view before I've started the program.
This is what I see when I run the program - marked with the red arrow.
Because you are initializing your FlowLayoutPanel in the code-behind, you have to set the FlowDirection property of this new instance of FlowLayoutPanel in the same code-behind:
FlowLayoutPanel panel = new FlowLayoutPanel();
public Form1()
{
InitializeComponent();
panel.FlowDirection = FlowDirection.TopDown;
The FlowLayoutPanel that you declare in your code-behind is separate from the one you have in your layout, so the FlowDirection property is not set the same. I tested the code above and I believe it does what you were looking for.
I have a windows form project, and I want the whole form to change location automatically, but the truth is that I have no idea what to call, and where to call it. I have searched online, and all code I discovered was incomplete. I am fairly new at this, so it did not help me.
Here is the code that I am working with, if it helps:
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;
using System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private SoundPlayer _soundplayer;
public Form1()
{
InitializeComponent();
SoundPlayer player = new SoundPlayer(Properties.Resources.sound);
player.Play();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var myForm = new Form2();
myForm.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_soundplayer.PlayLooping();
}
}
}
Change the Location for the form:
this.Location = new Point(400, 500);
You just need to decide which event will trigger this code; for example, the Click event of a button.
MSDN: Location
To position forms using the Properties window
In the Properties window, choose the form from the drop-down. Set the form's StartPosition property to Manual.
Type the values for the Location property, separated by a comma, to position the form, where the first number (X) is the distance from the left border of the display area and second number (Y) is the distance from the upper border of the display area.
Note Expand the Location property to enter the X and Y subproperty values individually.
Reference MSDN
So I am just experimenting with drawing images and other things, however it seems that my code only works in the load form event?
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 Interface_Editing_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//This works perfectly
/*Image i = Image.FromFile(#"C:\Users\Simon\Pictures\clickedbutton.jpg");
Bitmap b = (Bitmap)panel1.BackgroundImage;
using (Graphics g = Graphics.FromImage(b))
{
g.DrawImage(i, 0, 0, i.Size.Width, i.Size.Height);
}*/
}
private void panel1_Click(object sender, EventArgs e)
{
//Doesnt draw anything, but will show the message box
Image i = Image.FromFile(#"C:\Users\Simon\Pictures\clickedbutton.jpg");
Bitmap b = (Bitmap)panel1.BackgroundImage;
using (Graphics g = Graphics.FromImage(b))
{
//MessageBox.Show(" ");
g.DrawImage(i, 0, 0, i.Size.Width, i.Size.Height);
}
}
}
}
I know it is most likely something simple that I am overlooking but I would appreciate if someone could give me some insight in to whats happening. Thanks
Windows doesn't know that you changed the image. The Image class doesn't have any events that anybody could listen to so it knows that the image was changed. It works in the Load event because the window isn't visible yet, it will get painted right after that. So you can see the changed image. It doesn't work in the Click event handler since the panel is already displayed and has no reason to repaint itself.
Simply let it know that repainting is required. Add this line of code to the bottom of the method:
panel1.Invalidate();
It seems that this is probl;em related to handling click event inside a panel due to different controls inside it.
Make sure that your application handles panel1_click event. (May be through debugger you will come to know it).
Application may not handle this event due to different reasons such as different controls presence on panels,etc.
You can refer Following question having the same issue as you:
Panel events won't work in c#
Handling a click event anywhere inside a panel in C#
This working example might guid you:
private Bitmap _bmp = new Bitmap(250, 250);
public Form1()
{
InitializeComponent();
panel1.Click += new MouseEventHandler(panel1_Click);
panel1.Paint += new PaintEventHandler(panel1_Paint);
using (Graphics g = Graphics.FromImage(_bmp))
g.Clear(SystemColors.Window);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_bmp, new Point(0, 0));
}
private void panel1_Click(object sender, MouseEventArgs e)
{
using (Graphics g = Graphics.FromImage(_bmp))
{
g.DrawString("Mouse Clicked Here!", panel1.Font, Brushes.Black, e.Location);
}
panel1.Invalidate();
}
I'm currently trying to make a simple paint program in C# using a Windows Forms Application. When converting my list of Points to an Array using the ToArray function, I'm getting a generic "ArgumentException was unhandled: Parameter is invalid" error. I know I've done this before and it's worked fine, is there something special about the DrawLines function that I'm not aware of? Below is the code, with the line in question being the last line in the panel1_Paint event. Thanks in advance for any help you can provide.
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 GetSig
{
public partial class Form1 : Form
{
bool paint = false;
List<Point> myPointList = new List<Point>();
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (paint)
{
myPointList.Add(e.Location);
panel1.Invalidate();
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLines(Pens.Black, myPointList.ToArray());
}
}
}
According to the MSDN:
The first two points in the array specify the first line.
You need minimum two points in your Array.
Well, you can't draw lines without at least two points :)
if (myPointList.Count >= 2)
{
e.Graphics.DrawLines(Pens.Black, myPointList.ToArray());
}
Most likely that exception is not coming from the call to ToArray but from e.Graphics.DrawLines.