I have a form which creates a picturebox and inserts it in to a static list. I have another form which access the list and adds it to its controls with the following code:
foreach (var pb in Program.pbList)
{
if (!this.Controls.Contains(pb))
{
this.Invoke(new MethodInvoker(delegate()
{
this.Controls.Add(pb);
this.Invalidate();
this.Update();
this.Refresh();
}));
}
}
But it doesn't show up on the new form.
Update:
PictureBox pBox = new PictureBox();
pBox.Size = new Size(this.Size.Width / 14, this.Size.Width / 12);
pBox.BackColor = System.Drawing.Color.Aqua;
pBox.Visible = true;
pBox.Location = pb.Location;
this.Controls.Add(pBox);
this.Refresh();
But the form keeps refreshing and doesn't show my picturebox.
Extra:
public static List<PictureBox> pbList = new List<PictureBox>(); //Is in Program.cs
Code which adds to list:
PictureBox pBox = new PictureBox();
pBox.Size = new Size(this.Size.Width / 14, this.Size.Width / 12);
pBox.BackColor = System.Drawing.Color.Aqua;
pBox.Visible = true;
pBox.Location = new Point(390, 100);
Program.pbList.Add(pBox);
this.Controls.Add(pBox);
It is not possible to reuse controls since they will get disposed whenever the owning form gets disposed.
That will mean they will never render any more. Ever.
Construct a list of images and recreate the controls every time.
I think you cannot do that. Because C# cannot reuse controls because the system would dump the control whenever the form gets dispose
Related
I need to crate a certain number of RichTextBoxes depending on User Input.
I can create one in visual studio using the toolbox, but how would I go about creating multiple through code?
UPDATE:
This is now my code:
RichTextBox richTextBox = new RichTextBox();
richTextBox.Location = new Point(12, 169);
richTextBox.Width = 62;
richTextBox.Height = 76;
this.Controls.Add(richTextBox);
Nothing happens when I run this
OK. Here is a sample that shows that it works:
void Main()
{
Form f = new Form();
Button b = new Button();
b.Click += (sender, args) =>
{
RichTextBox richTextBox = new RichTextBox
{
Name = "rtbBlahBlah",
Location = new System.Drawing.Point(12, 169),
Width = 62,
Height = 76
};
f.Controls.Add(richTextBox);
};
f.Controls.Add(b);
f.Show();
}
Call this.Refresh() to refresh the Control and re-draw all the children within it.
From the Docs:
Forces the control to invalidate its client area and immediately redraw itself and any child controls.
I have three buttons in Form_Load, to every button I give size, location and text.
When I click one of the buttons a new button appears, which should take me to the first screen with the three original buttons. I clear the screen and add the buttons, but I get an error "the name 'button' does not exist in the current context". What can I do to have access to these buttons. Thanks.
Form_Load:
Button play = new Button();
Button howtoplay = new Button();
Button puzzles = new Button();
play.Size = new Size(175, 70);
puzzles.Size = new Size(175, 70);
howtoplay.Size = new Size(175, 70);
play.Location = new Point((ClientRectangle.Right/2)-(play.Width/2), 135);
puzzles.Location = new Point((ClientRectangle.Right / 2) - (play.Width / 2), 210);
howtoplay.Location = new Point((ClientRectangle.Right / 2) - (play.Width / 2), 285);
play.Text = "Play";
howtoplay.Text = "How To Play";
puzzles.Text = "Puzzles";
Controls.Add(play);
Controls.Add(howtoplay);
Controls.Add(puzzles);
howtoplay.Click += new EventHandler(howtoplay_click);
howtoplay_click:
play.Hide();
puzzles.Hide();
howtoplay.Hide();
Button backB = new Button();
backB.Size = new Size(100, 50);
backB.Location = new Point((ClientRectangle.Right - backB.Width - 10), (ClientRectangle.Bottom - backB.Height - 10));
backB.Text = "Back";
backB.Click += new EventHandler(Back_Click);
Controls.Add(backB);
Back_Click:
Controls.Clear();
Controls.Add(play); //error
Controls.Add(puzzles); //error
Controls.Add(howtoplay); //error
You have declared buttons as local variables of Form_Load event handler method:
private void Form_Load(object sender, EventArgs e)
{
Button play = new Button();
Button howtoplay = new Button();
Button puzzles = new Button();
// ...
}
These variables are not available outside the method. You should use form fields instead:
// available in all instance methods of form
Button play;
Button howtoplay;
Button puzzles;
private void Form_Load(object sender, EventArgs e)
{
play = new Button();
howtoplay = new Button();
puzzles = new Button();
// ...
}
Note: Usually you should manually create controls only when those controls should be added to your form dynamically at runtime. But you are creating controls in Form_Load event handler, so I suggest you use designer to create controls. It will create a class field for each control and add appropriate initialization code. All you need to do is drag-and-drop control (buttons in this case) from toolbox to form and setup each control properties.
my timer application is simple. I am facing problem when i am trying to add multiple tasks using existing panel i have uploaded a image here is the code which i have tryed(panel1 is allready added to form using panel1 we need to create new panels which are nothing but there copy of panel1 which has same funcationlty as panel1)
private void btn_addtimer_Click(object sender, EventArgs e)
{
itf = new Input_task_form();
DialogResult dr = itf.ShowDialog();
if (dr == DialogResult.OK) {
//lbl_text.Text =itf.Task_name;
//Height = Height + 234;
//this.Size = new Size(535, Height);
//this.panel1.Show();
timer1 = new System.Windows.Forms.Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
panel1 = new Panel();
this.lbl_text.Text = itf.Task_name;
Height = Height + 234;
this.Size = new Size(535, Height);
panel1.Show();
this.panel1.Name = "panel" + pcount.ToString();
}
else if (dr == DialogResult.Cancel) {
MessageBox.Show("You clicked on Cancel");
}
}
This doesn't do anything:
panel1.Show();
Instead of panel1.Show(), you want to add the panel to the form:
this.Controls.Add(panel1);
Additionally, you want to set the Top and Left properties for the panel to something sensible. It looks like you may be confusing the Height property for Top. One option to consider is a FlowLayoutPanel or similar as parent control for these items.
I am working on a simple Windows Forms application which consists of a Panel where I draw graphics with Graphic. Let's say, my panel is now of size 300x300 but the content inside is 500x500. Obviously, I need to add scrollbars to the Panel.
My code so far:
public CircuitControl()
{
// Initialize empty list of circuit objects
CircuitObjects = new List<CircuitObject>();
drawingAreaPanel.AutoScroll = true;
drawingAreaPanel.VerticalScroll.Enabled = true;
drawingAreaPanel.VerticalScroll.Visible = true;
drawingAreaPanel.HorizontalScroll.Enabled = true;
drawingAreaPanel.MaximumSize = new Size(300, 300);
drawingAreaPanel.Size = new Size(600, 600);
}
But none of these codes create actually a scroll bar. My question is: Where and how do I set the size of the Panel where I actually drew? I think this is the part which is missing. Thanks.
The scrollbars won't show up until there's actually something in the Panel that you can't see all of.
Try placing a larger control, such as a PictureBox, inside the Panel, and setting the PictureBox's initial size as larger than the Panel.
Just add:
drawingAreaPanel.AutoScroll = true;
And it will be done automatically.
€dit: Don't forget to set the anchors in order to get the scrollbars.
A clean and simple approach is to set AutoScrollMinSize. This shows the scrollbars (or just one if you leave the other value at 0).
Now drawing through the graphics object will not be scrolled automatically.
This can be easily achieved with a transformation matrix, which is set before painting and translates the drawing by the scroll offset.
A pretty example: (this flickers of course without further optimizations)
private void button1_Click(object sender, EventArgs e)
{
using(Form frm = new Form())
{
Panel pnl = new Panel();
pnl.Paint += delegate (Object snd, PaintEventArgs e2) {
Matrix mtx = new Matrix();
mtx.Translate(pnl.AutoScrollPosition.X, pnl.AutoScrollPosition.Y);
e2.Graphics.Transform = mtx;
e2.Graphics.Clear(Color.Black);
for(int i=0; i <= 125; i++)
for(int j=0; j <= 125; j++)
using(Brush b = new SolidBrush(Color.FromArgb(255, 255-i*2, j*2, (i*j) % 255)))
e2.Graphics.FillRectangle(b, new Rectangle(5+j*20, 5+i*20, 20, 20));
};
pnl.AutoScrollMinSize = new Size(126*20+10, 126*20+10);
pnl.Dock = DockStyle.Fill;
frm.Controls.Add(pnl);
frm.Padding = new Padding(25);
frm.ShowDialog(this);
}
}
I want to programmatically create a panel and add some pictureBoxes where i select the image through a for loop. I tried lots of ways but the form shows empty.
My code is :
private void draw_pipeline()
{
Panel pnl = new Panel();
pnl.Size = new System.Drawing.Size(1130, 145);
pnl.Location = new Point(380, 260);
pnl.BorderStyle = BorderStyle.FixedSingle;
for (int i =0; i<3; i++)
{
PictureBox pic = new PictureBox();
pic.SizeMode = PictureBoxSizeMode.Zoom;
switch (i)
{
case 0:
{
pic.Location = new Point(3, 15);
pic.Size = new Size(73, 121);
pic.Image = new Bitmap("if.png"); break;
}
case 1:
{
pic.Location = new Point(76, 15);
pic.Size = new Size(73, 121);
pic.Image = new Bitmap("line.png"); break;
}
}
pnl.Controls.Add(pic);
}
}
the result i want to create is illustrated in the picture below, that contains two picture boxes with two images, if.png which is the if-box image and the line.png which is the line image. I repeat the result of my code is that form shows empty!! Any help?
You'll need to add the Panel to the Form at some point, in the same way you're adding PictureBoxes to the Panel:
this.Controls.Add(pnl);
(The this is assuming that your draw_pipeline method belongs to the Form to which you're trying to add the Panel.)