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.
Related
I have form 1 with 4 buttons when I click a button it opens a new form. Each button opens the same form but I want the corresponding button to enter specific values into two different text boxes on form 2.
Form 1 Button A; Form2 textbox1= 400 textbox2 =0.4
Form 1 Button B; Form2 textbox1= 350 textbox2 =0.9
Form 1 Button C; Form2 textbox1= 700 textbox2 =0.6
Form 1 Button D; Form2 textbox1= USER DEFINED textbox2 = USER DEFINED
How would I go about this
//This is the current text
// Form1:
private void ButtonA_Click(object sender, EventArgs e)
{
Form2 numb = new form2();
numb.FormClosed += new FormClosedEventHandler(numb_FormClosed);
this.Hide();
CalcForm.Show();
}
You can just set the value of the required textBox from the first form like below, but before it make sure that you have set that textBox to be internal so that you can access it from first form(in the Form.Designer.cs):
internal System.Windows.Forms.TextBox textBox1;
and
private void ButtonA_Click(object sender, EventArgs e)
{
Form2 numb = new form2();
numb.FormClosed += new FormClosedEventHandler(numb_FormClosed);
numb.textbox1.Text = "400";
numb.textbox2.Text = "0.4";
this.Hide();
CalcForm.Show();
}
Another approach is to define parameterized constructor for Form2 and set the value of the TextBox in that constructor like below:
public Form2(string a,string b)
{
textBox1.Text = a;
textBox2.Text = b;
}
and
private void ButtonA_Click(object sender, EventArgs e)
{
Form2 numb = new form2("aaaa","bbbb");
numb.FormClosed += new FormClosedEventHandler(numb_FormClosed);
this.Hide();
CalcForm.Show();
}
I have been finding some issues with form1 and form2, I want the result of pressing a button in form1 to be the change of the text in a button in form2, so mainly my problem is how to access this button in form2 from form1,I want to assign the result of int q into the text of button 2 shown in the picture
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
// int a=1;
Random R = new Random();
int start = R.Next(10, 999);
if(start>99)
{
int x = start-1;
int y = x%100;
int z = start/y;
int w = z+1;
int q = start/w;
}
else
{
int y = start-1;
int z = y/2;
int w = start/z;
int q = 1;
}
}
enter image description here
Create a public property in Form2 class
public class Form2 : Form
{
public string Button1Text
{
set { this.Button1.Text = Value; }
}
....
}
Now in form1 click code set it
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
.... your calculations
f2.Button1Text = theResultOfYourCalculation.ToString()
Of course this could also be done making the property Modifiers of your buttons Public through the Forms designer. Giving access to the internal controls of your form (and all of their properties) is a bad idea and in the long term leads to a very bad designed application
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
I am trying to call panel1 paint method to repaint the panel with a orange line (it is initiated with a blue line).
I have tried invalidate(), update() and refresh(), but nothing seems to call the paint event of panel1...
The paint event handler have been added to the panel1:
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
Can someone please assist?
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 testForm = new Form1();
Application.Run(testForm);
testForm.drawNewLine();
}
}
public partial class Form1 : Form
{
bool blueLine = true;
bool orangeLine = false;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (blueLine == true)
{
Pen bluePen = new Pen(Color.Blue, 3);
g.DrawLine(bluePen, 30, 50, 30, 250);
}
else if (orangeLine == true)
{
Pen orangePen = new Pen(Color.Orange, 3);
g.DrawLine(orangePen, 30, 50, 30, 250);
}
g.Dispose();
}
public void drawNewLine()
{
blueLine = false;
orangeLine = true;
//panel1.Invalidate();
//panel1.Update();
panel1.Refresh();
}
}
Application.Run(testForm); blocks until form is closed, so when drawNewLine() is called - form does not exist anymore (create a button which calls it on click and check yourself, the code is working). Invalidate() should work just fine.
Also, you should not dispose Graphics object which is passed to your code in paint event. You were not responsible for creating it, so let the code which created it to destroy it.
Also, dispose Pen objects since you are creating them.
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