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.
Related
I want to create a groupbox programmatically and inside it put labels. Now I created this but with design and I have like this:
I'm trying but I don't know how can I assign labels in correct position and how can I assign it to a specific group box
GroupBox groupBox1 = new GroupBox();
Panel grid1 = new Panel();
Label lbl1 = new Label { Text = "Completed" };
Label lbl2 = new Label { Text = "label" };
Label lbl3 = new Label { Text = "In progress" };
Label lbl4 = new Label { Text = "label" };
//etcetera
groupBox1.Width = 185;
groupBox1.Height = 160;
grid1.Height = 185;
grid1.Width = 160;
How can I achieve that? Regards
Update
As the comments below I try
GroupBox groupBox1 = new GroupBox();
this.Controls.Add(groupBox1);
Panel grid1 = new Panel();
groupBox1.Controls.Add(grid1);
groupBox1.Location = new Point(20, 250);
grid1.Location = new Point(20, 250);
Label lbl1 = new Label { Text = "test" };
Label lbl2 = new Label { Text = "Test2" };
groupBox1.Name = "TESTTT";
groupBox1.Width = 222;
groupBox1.Height = 149;
grid1.Height = 218;
grid1.Width = 145;
grid1.Controls.Add(lbl1);
grid1.Controls.Add(lbl2);
Result:
But my group box it just clear without name and without labels, why it happen?
Controls in WinForms are arranged so that they reside inside one another. So your Form has a Controls collection which is essentially a Collection of type Control. So if you add a GroupBox to the form, then you must add it to the Controls collection of the form. Then if you add a control to your GroupBox then you need to add it to the GroupBox collection of controls.
With that in mind, you can do something like this:
private void AddGroupBoxAndLables()
{
GroupBox groupBox1 = new GroupBox();
groupBox1.SetBounds(50, 50, 300, 200);
this.Controls.Add(groupBox1);
Label lblCompleted = new Label { Name = "lblCompleted", Text = "Completed" };
lblCompleted.Location = new Point(20, 20);
groupBox1.Controls.Add(lblCompleted);
Label valCompleted = new Label { Name = "valCompleted" };
valCompleted.Location = new Point(80, 20);
groupBox1.Controls.Add(valCompleted);
Label lblInProgress = new Label { Name = "lblInProgress", Text = "In Progress" };
lblInProgress.Location = new Point(20, 60);
groupBox1.Controls.Add(lblInProgress);
Label valInProgress = new Label { Name = "valInProgress" };
valInProgress.Location = new Point(80, 60);
groupBox1.Controls.Add(valInProgress);
}
simplest way for solution is that your code is already created when you did design. Code is created automatically in respective form's designer.cs or designer.vb file. To see this file, in solution explorer click on button 'Show all files'. Still for your understanding let me explain code
You have created groupbox using
GroupBox groupBox1 = new GroupBox();
To see this group box on form you need to add this groupbox to form
this.Controls.Add(groupBox1);
Similar in case of panel. If you want to add panel inside of groupbox then
groupbox1.Controls.Add(grid1);
Then add all labels inside of panel.
You will find similar kind of code in form's designer.cs.
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.
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
public int dialog()
{
Form prompt = new Form(); // creates form
//dimensions
prompt.Width = 300;
prompt.Height = 125;
prompt.Text = "Adding Rows"; // title
Label amountLabel = new Label() { Left = 75, Top = 0, Text = "Enter a number" }; // label for prompt
amountLabel.Font = new Font("Microsoft Sans Serif", 9.75F);
TextBox value = new TextBox() { Left = 50, Top = 25, Width = prompt.Width / 2 }; // text box for prompt
Button confirmation = new Button() { Text = "Ok", Left = prompt.Width / 2 - 50, Width = 50, Top = 50 }; // ok button
confirmation.Click += (sender, e) => { prompt.Close(); }; // if clicked it will close
prompt.AcceptButton = confirmation; // enter
// adding the controls
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.Controls.Add(value);
prompt.ShowDialog();
int num;
Int32.TryParse(value.Text, out num);
return num;
}
This is what my prompt looks like when it's called
I just clicked a button to call that method. Now as you notice, the text box is not selected. How do I make it so that if this method is called, it will make the text box selected by default without having to click it or tab to it?
(I know this is minor but every detail would look nicer)
The order used to tab between controls is determined by the property TabIndex. This property is determined automatically by the order in which you add the controls (If you don't change it manually) The control with TabIndex = 0 will be focused at the opening of the form (Of course if the control could be focused)
Try with
prompt.Controls.Add(value);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.ShowDialog();
You mean Focused ? Like this:
textBox1.Focus();
Write this code after your show dialog,It should work.
prompt.ShowDialog();
prompt.Controls.OfType<TextBox>().First().Focus();
Or if it doesn't work try to set ActiveControl property before opening your prompt:
promt.ActiveControl = value;
prompt.ShowDialog();
I use WebBrowser in some DLL to make screenshots.
The main problem that it is not maximized sometimes and I guess it takes settigns of the Internet Explorer.
So my question is how to maximize WebBrowser control via C#?
Thank you!
Rectangle r = new Rectangle();
r.X = cropToRectangle.X;
r.Y = cropToRectangle.Y;
r.Width = cropToRectangle.Width;
r.Height = cropToRectangle.Height;
Point p = new Point();
p.X = scrollTo.X;
p.Y = scrollTo.Y;
var sb = Math.Max(SystemInformation.VerticalScrollBarWidth, SystemInformation.HorizontalScrollBarHeight);
var size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
using (var form =
new FocuslessForm
{
Width = size.Width + sb,
Height = size.Height + sb,
Padding = new Padding(0),
Margin = new Padding(0),
FormBorderStyle = FormBorderStyle.None,
Opacity = 0,
TabStop = false,
ShowInTaskbar = false
})
{
var webBrowser1 = new WebBrowser
{
Padding = new Padding(0),
Margin = new Padding(0),
Dock = DockStyle.Fill,
Url = url,
TabStop = false
};
form.Controls.Add(webBrowser1);
var finished = false;
webBrowser1.DocumentCompleted += delegate
{
webBrowser1.Document.Window.ScrollTo(p);
finished = true;
};
form.Show();
while (!finished)
{
Application.DoEvents();
}
image = CaptureBrowserScreenshot(webBrowser1, r);
form.Close();
}
Well, the WebBrowser is a control that is embedded into your own program's window; it doesn't launch IE as a separate process (though it does hook into IE for the renderer and other critical code). So, the control's location and size is dependent on where you embed it.
I see you are fill-docking the control to the form. This is a good first step. Now, you must make sure the WebBrowser control is being added to the Controls hierarchy of the Form (so it'll show up), and then you must maximize that Form. The way to do this is to set the WindowState property of the Form to WindowState.Maximized.
I implemented Form resize event, when form is maximize, i set web browser size to form size.
It worked well in my app, I share for whom concern.
private void Form1_Resize(object sender, EventArgs e)
{
webBrowser1.Size = this.Size;
}