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?
Related
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'm trying to create PictureBoxes dynamically at runtime with c# winforms.
My project: I want to write a program, which has a node-GUI (a GUI with various types of nodes, some kind of boxes, which are connected together and process an image, an audio stream or whatever).
Therefor i want to create and delete Pictureboxes dynamically at runtime, but my testing won't work, the form is empty.
Here is my code:
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 AudioNodeGUI
{
public partial class AudioNodeWindow : Form
{
public AudioNodeWindow()
{
InitializeComponent();
}
private void AudioNodeWindow_Load(object sender, EventArgs e)
{
}
private void AudioNodeWindow_Paint(object sender, PaintEventArgs e)
{
PictureBox start_picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(19, 32),
Location = new Point(100, 100),
Visible = true,
Image = Bitmap.FromFile(#"C:\Users\Benjamin.MBENJAMIN\Pictures\Start.png"),
};
start_picture.Show();
}
}
}
Please help !
You need to add the control you created to the Forms control.
Before you Show() the picturebox, try adding this line:
Controls.Add(start_picture);
Secondly, you don't want to doing this onPaint()!
I would say you need to move it to Load() method instead, that way it will be done when the form loads, rather than everytime it's repainted!
Change:
start_picture.Show();
to:
this.Controls.Add(start_picture);
start_picture.Show();
Controls.Add tells the form that the PictureBox is meant to be part of this specific form.
Also, you will not want to do this in in your Paint event handler. Leaving it there will result in many more picture boxes than you would like I expect...
I have change your code as :
PictureBox start_picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(19, 32),
Location = new Point(100, 100),
Visible = true,
Image = Bitmap.FromFile(#"D:\test\learn.png"),
};
//start_picture.Show();
Controls.Add(start_picture);
I am adding a TextBox to my form at runtime, and this is a brand new project, so this is the only code I have so far, so I am 100% positive that this is not my own doing:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
Why won't the TextBox display? There's nothing at all. I set breakpoints all over the place, but none of them how anything that could help me. All seems normal, but isn't.
The code is very simple, the only reason I can think of is you have some other control added before (wide enough to cover the added TextBox), try this:
private void button1_Click(object sender, EventArgs e)
{
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
box.BringToFront();
}
Also check the event handler ControlAdded, I guess the form has some code for this event handler and discard the control added if it's type of TextBox, something like this:
private void form_ControlAdded(object sender, ControlEventArgs e) {
if(e.Control is TextBox) Controls.Remove(e.Control);
}
The code that adds the textbox to the form is in button1_Click event handler. If you move it to the constructor, it will work just fine.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
TextBox box = new TextBox();
box.Location = new Point(2, 2);
this.Controls.Add(box);
}
}
}
I had a similar problem.
Looking at the code I found out that the textboxes that DO show were of type System.Windows.Forms.TextBox while those that DID NOT show were of type VisualJS.Web.TextBox. Perhaps your problem is similar.
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
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.