public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Size = new Size(50, 50);
GraphicsPath Gcircle = new GraphicsPath();
Gcircle.AddEllipse(0, 0, 50, 50);
this.button1.Region = new Region(Gcircle);
}
I want to make a beautiful button,but button of above method is very ugly
there is another way to do ???
Try this links if this is what your need
www.codeproject.com/Articles/5082/Round-Button-in-C
www.codeproject.com/Articles/5582/Elongated-Buttons-Rounded-cornered-Groupboxes-and
www.codeproject.com/Articles/15730/RoundButton-Windows-Control-Ever-Decreasing-Circle
Related
this question might be really stupid but here it is anyway. What I want my programm to do: When I press a button I want to add a DatePicker Component to a List and then display all the Components in the Main Form. However when I press the button it only adds the components but doesnt show them in the Form Window. No Errors are thrown. What do I have to do to display the DatePicker Components in the Main Form?
//class containing the List of Components
class Eintrag
{
static public List<DateTimePicker> Anfangszeit = new List<DateTimePicker>();
}
//Main Form Class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Eintrag.Anfangszeit.Add(new DateTimePicker());
for (int i = 0; i < Eintrag.Anfangszeit.Count; i++)
{
Eintrag.Anfangszeit[i].Location = new System.Drawing.Point(30, 50 + 50*i);
Eintrag.Anfangszeit[i].Size = new System.Drawing.Size(200, 20);
Eintrag.Anfangszeit[i].Visible = true;
Eintrag.Anfangszeit[i].Show();
}
}
}
John Wu is right, you have to add the Controls to the Form via Controls.Add()
private void button1_Click(object sender, EventArgs e)
{
Eintrag.Anfangszeit.Add(new DateTimePicker());
for (int i = 0; i < Eintrag.Anfangszeit.Count; i++) {
Eintrag.Anfangszeit[i].Location = new System.Drawing.Point(30, 50 + 50 * i);
Eintrag.Anfangszeit[i].Size = new System.Drawing.Size(200, 20);
Eintrag.Anfangszeit[i].Visible = true;
this.Controls.Add(Eintrag.Anfangszeit[i]);
Eintrag.Anfangszeit[i].Show();
}
}
Can you help me with this piece of code ?
I want to learn to use Invalidate and Update , but I'm not sure how to use it
This code doesn't work. I want set new text but it doesn't "refresh" and doesnt write the new string "Prova". Thanks
namespace Invalidate
{
public partial class Form1 : Form
{
private String txt;
private PointF xy;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString("This is the text that prints!",
this.Font, System.Drawing.Brushes.Azure, 0, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
Panel pHText = new Panel();
pHText.Name = "ctrId";
pHText.Location = new Point(10, 10);
pHText.Size = new Size(200, 200);
pHText.BackColor = Color.White;
pHText.Paint += paintingUrCtr;
Controls.Add(pHText);
}
private void paintingUrCtr(object sender, PaintEventArgs e)
{
Font myFont = new Font("Arial", 14);
e.Graphics.DrawLine(new Pen(Color.Black), 0, 0, 10, 10);
e.Graphics.DrawString(this.txt, myFont, Brushes.Blue, 10, 10);
}
public void setText(String text, PointF pos)
{
this.txt = text;
this.xy = pos;
}
private void button_Click(object sender, EventArgs e)
{
setText("Prova", new PointF(100, 100));
this.Invalidate();
this.Update();
}
}
}
Calling this.Invalidate(); does not invalidate child controls, you need to use the overload that takes in a bool and pass in true
private void button_Click(object sender, EventArgs e)
{
setText("Prova", new PointF(100, 100));
this.Invalidate(true);
this.Update();
}
EDIT: as mentioned in the comments, you could also just invalidate the single child control and update that.
private void button_Click(object sender, EventArgs e)
{
setText("Prova", new PointF(100, 100));
this.Controls["ctrId"].Invalidate(true);
this.Controls["ctrId"].Update();
}
I have a program which dynamically creates movable pictureboxes when I click on buttons. I need to do something like when I click on the picturebox, this click adds to my dynamically created picturebox a new textbox when I can write descripiton of this picturebox(name,...). This textbox should be able to move with picturebox.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<PictureBox> pictureboxes = new List<PictureBox>();
public Form1()
{
InitializeComponent();
}
private void AddPictureBox(string imagePath)
{
var pb = new PictureBox();
pb.Name = "picturebox" + pictureboxes.Count;
pb.Location = new Point(pictureboxes.Count * 100, 100);
pb.Size = new Size(70, 70);
pb.BorderStyle = BorderStyle.None;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(pb);
pb.Image = Image.FromFile(imagePath);
pb.Refresh();
pb.MouseDown += new MouseEventHandler(picMouseDown);
pb.MouseMove += new MouseEventHandler(picMouseMove);
pb.MouseUp += new MouseEventHandler(picMouseUp);
pictureboxes.Add(pb);
Invalidate();
}
private void router_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\\router.jpg");
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
Invalidate();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\pc.jpg");
}
private void server_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\server.png");
}
}
Thanks for any help :).
You can add a TextBox to a PictureBox in code like this:
TextBox newTextBox = new TextBox();
newTextBox.Parent = yourPictureBox;
// place it e.g. to the left bottom:
newTextBox.Location = new Point(10, yourPictureBox.Height - newTextBox.Height);
Note that this will add the TextBox to the Controls collection of the PB; so it will sit on top of the PictureBox; so, yes, it will move with the PictureBox but it will also hide a part or the PB!
If instead you simply want to group them, add them both to something like a Panel, again by setting that as their Parent!
Also note that you can't do this in the Designer; PictureBox is not really meant to act as a Container..
It doesn't matter how the PictureBox was created, as long as you have a reference to it.
I am trying to open a (non decorated) childform at the upper right corner of my main form no matter if the main form is maximized or at it's normal size.
But no matter how I try I don't get it to open where I want it to.
I found a post that described how to open the form relative to another control in the form, but that didn't work either:
How to display a Modal form in a position relative to the a control in the parent window (opener)
Have tried to search for a few hours now on google for a solution, but either there's no answer (doubdfull) or I am not searching for the tight words combination (more likely).
Could anyone please either point me to a similar question, or help me how to achieve what I am hoping for?
Sounds to me you ought to be using a UserControl that you anchor to the top and right. But let's make a form work. You'll need to wire its Load event so you can move it into the right spot after it rescaled itself. Then you need the main form's LocationChanged and Resize events so you can keep the child form in the right spot.
So a sample program with boilerplate Form1 and Form2 names and a button on Form1 to display the child could look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.button1.Click += button1_Click;
this.Resize += this.Form1_Resize;
this.LocationChanged += this.Form1_LocationChanged;
}
Form child;
private void button1_Click(object sender, EventArgs e) {
if (child != null) return;
child = new Form2();
child.FormClosed += child_FormClosed;
child.Load += child_Load;
child.Show(this);
}
void child_FormClosed(object sender, FormClosedEventArgs e) {
child.FormClosed -= child_FormClosed;
child.Load -= child_Load;
child = null;
}
void child_Load(object sender, EventArgs e) {
moveChild();
}
void moveChild() {
child.Location = this.PointToScreen(new Point(this.ClientSize.Width - child.Width, 0));
}
private void Form1_Resize(object sender, EventArgs e) {
if (child != null) moveChild();
}
private void Form1_LocationChanged(object sender, EventArgs e) {
if (child != null) moveChild();
}
}
Try something like that:
private void button1_Click(object sender, EventArgs e)
{
ChildForm win = new ChildForm();
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
Point parentPoint = this.Location;
int parentHeight = this.Height;
int parentWidth = this.Width;
int childHeight = win.Height;
int childWidth = win.Width;
int resultX = 0;
int resultY = 0;
if ((parentPoint.X + parentWidth + childWidth) > screenWidth)
{
resultY = parentPoint.Y;
resultX = parentPoint.X - childWidth;
}
else
{
resultY = parentPoint.Y;
resultX = parentPoint.X + parentWidth;
}
win.StartPosition = FormStartPosition.Manual;
win.Location = new Point(resultX, resultY);
win.Show();
}
I think you should try something like this:
private void buttonOpen_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
//"this" is the parent form
frm2.DesktopLocation = new Point(this.DesktopLocation.X + this.Width - frm2.Width, this.DesktopLocation.Y);
}
Simple, easy, and works (for me).
As I'm pretty new to C#, I struggle with the following piece of code. When I click to button 'knop', the method 'klik' has to be executed. The method has to draw the Bitmap 'b', generated by 'DrawMandel' on the form. But I constantly get the error 'no overload for matches delegate 'system.eventhandler'.
using System;
using System.Windows.Forms;
using System.Drawing;
class Mandelbrot : Form
{
public Bitmap b;
public Mandelbrot()
{
Button knop;
knop = new Button();
knop.Location = new Point(370, 15);
knop.Size = new Size(50, 30);
knop.Text = "OK";
this.Text = "Mandelbrot 1.0";
this.ClientSize = new Size(800, 800);
knop.Click += this.klik;
this.Controls.Add(knop);
}
public void klik(PaintEventArgs pea, EventArgs e) {
Bitmap c = this.DrawMandel();
Graphics gr = pea.Graphics;
gr.DrawImage(b, 150, 200);
}
public Bitmap DrawMandel()
{
//function that creates the bitmap
return b;
}
static void Main() {
Application.Run(new Mandelbrot());
}
}
You need to change public void klik(PaintEventArgs pea, EventArgs e) to public void klik(object sender, System.EventArgs e) because there is no Click event handler with parameters PaintEventArgs pea, EventArgs e.
Yes there is a problem with Click event handler (klik) - First argument must be an object type and second must be EventArgs.
public void klik(object sender, EventArgs e) {
//
}
If you want to paint on a form or control then use CreateGraphics method.
public void klik(object sender, EventArgs e) {
Bitmap c = this.DrawMandel();
Graphics gr = CreateGraphics(); // Graphics gr=(sender as Button).CreateGraphics();
gr.DrawImage(b, 150, 200);
}
You need to wrap button click handler to match the pattern
public void klik(object sender, EventArgs e)
Change the klik method as follows:
public void klik(object pea, EventArgs e)
{
Bitmap c = this.DrawMandel();
Button btn = pea as Button;
Graphics gr = btn.CreateGraphics();
gr.DrawImage(b, 150, 200);
}