I have a TabControl with one TabPage with a RichTextBox inside that I added with the windows form design tool and an option in a menu to add more TabPages and RichTextBoxes. When I fill the Richtextbox that I added manually with text the vertical scrollbar appears and the text is inside the limits od the textbox, but when I fill with text a richtextbox that I added with the menu option the text goes beyond the limits of the textbox and no scrollbars appear.
How can I make the richtextboxes that I add behave properly?
this is the code I use to add the tabpages and the richtetxboxes:
I've already tried setting multiline True and forcing the scrollbars.
EDIT: I know what it is. For some reason when I set the size of myRichTextBox it actually makes it bigger than the richtextbox I added in the design tool even tho they are the same size, so the text actually "fits" but it is off limits.
private void AgregarPestaƱaToolStripMenuItem_Click(object sender, EventArgs e)
{
string title = "PestaƱa " + (tabControl1.TabCount + 1).ToString();
string name = "richTextBox" + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
RichTextBox myRichTextBox= new RichTextBox();
myRichTextBox.Location = new System.Drawing.Point(0, 0);
myRichTextBox.Name = name;
myRichTextBox.Size = new System.Drawing.Size(500, 500);
myRichTextBox.TabIndex = 0;
myRichTextBox.Text = "";
myRichTextBox.Multiline = true;
myRichTextBox.ScrollBars = RichTextBoxScrollBars.ForcedBoth ;
textAreas.Add(myRichTextBox);
myTabPage.Controls.Add(myRichTextBox);
tabControl1.TabPages.Add(myTabPage);
}
This is the code inside InitializeComponent():
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(480, 480);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
UPDATE
I fixed the problem, apparently Visual Studio was scaled 125% and because of the way it detects the size of components, the values were incorrect.
Related
in c# winform, How to keep a control always in the center bottom of a form when form's size changed?
if I use DockStyle.Bottom, I can't set its width.
I tried it out. The following is the code, but I don't know why? It's mainly because I don't understand why the Left value will change when the Anchor. Bottom is set?
public void Run()
{
Form form = new Form();
form.Width = 600;
form.Height = 600;
Button btnOK = new Button();
btnOK.Text = "OK";
btnOK.Left = form.ClientSize.Width / 2 - btnOK.Width;
btnOK.Top = 0;
btnOK.Anchor = AnchorStyles.Bottom;
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Left = form.ClientSize.Width / 2;
btnCancel.Top = 0;
btnCancel.Anchor = AnchorStyles.Bottom;
Panel panel = new Panel();
panel.Height = btnOK.Height;
panel.Width = form.ClientSize.Width;
panel.Dock = DockStyle.Bottom;
panel.Controls.Add(btnOK);
panel.Controls.Add(btnCancel);
form.Controls.Add(panel);
form.Show();
}
Use a TableLayoutPanel. You can Dock it to the Bottom or Anchor it to the Bottom, Left and Right. You would have one row then have a column with an absolute width for each control and one extra column on the left and another on the right with 50% width. Those two extra columns will take up half the empty space each, thus keeping the others in the middle.
I am working on a homework c# winforms project and would like to add date and time in top right corner of my main form in a way that in first row I have a date written in one label, and on second row I have time written in second label.
Also I need that those stick in the top right corner if form is resized.
I don't know if it matters, but those label controls are inside panel which is top docked in form, and this panel already contains two controls that are docked left.
example of what I want
I've been playing with anchor and dock properties but I can't get it to work in a way I want.
private void GlavnaForma_Load(object sender, EventArgs e)
{
timerDateTime.Start();
lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}
private void timerDateTime_Tick(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}
Set the anchor to Top, Right like so:
There are several ways to do this.
I would probably make the main form have a table layout panel with one column and three rows. Make the top two rows be absolutely sized and the third row have size type "percent" with a value of 100.0% to take up all remaining room. Then put a label each in the top two rows and justify the labels to the right via setting their "Dock" property to "Right".
All of this can be done in the form designer GUI. The generated code looks like the following:
this.tableLayout = new System.Windows.Forms.TableLayoutPanel();
this.labelDate = new System.Windows.Forms.Label();
this.labelTime = new System.Windows.Forms.Label();
this.tableLayout.SuspendLayout();
this.SuspendLayout();
//
// tableLayout
//
this.tableLayout.ColumnCount = 1;
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Controls.Add(this.labelDate, 0, 0);
this.tableLayout.Controls.Add(this.labelTime, 0, 1);
this.tableLayout.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayout.Location = new System.Drawing.Point(0, 0);
this.tableLayout.Name = "tableLayout";
this.tableLayout.RowCount = 3;
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Size = new System.Drawing.Size(800, 450);
this.tableLayout.TabIndex = 0;
//
// labelDate
//
this.labelDate.AutoSize = true;
this.labelDate.Dock = System.Windows.Forms.DockStyle.Right;
this.labelDate.Location = new System.Drawing.Point(742, 0);
this.labelDate.Name = "labelDate";
this.labelDate.Size = new System.Drawing.Size(55, 24);
this.labelDate.TabIndex = 0;
this.labelDate.Text = "26-8-2019";
//
// labelTime
//
this.labelTime.AutoSize = true;
this.labelTime.Dock = System.Windows.Forms.DockStyle.Right;
this.labelTime.Location = new System.Drawing.Point(748, 24);
this.labelTime.Name = "labelTime";
this.labelTime.Size = new System.Drawing.Size(49, 24);
this.labelTime.TabIndex = 1;
this.labelTime.Text = "19:59:58";
Add whatever further content you want to the third row. Maybe add a panel to that row docked to "Fill"
i'm making a simple login-register program, i save usernames and passwords in lists.
i'm trying to show all the usernames under each other and that it wil repeat everytime a new user will be registered.
for some reason it only shows the last user, and that's it.
for (Int32 i = 0; i < frmLogin.reg_usernames.Count; i++ )
{
TextBox lbl = new TextBox { Location = new Point(15, 30), BorderStyle = BorderStyle.Fixed3D, BackColor = Color.AliceBlue, Font = new Font(Font.FontFamily.Name, 9), ScrollBars = ScrollBars.Vertical };
this.Controls.Add(lbl);
lbl.Text = frmLogin.reg_usernames[i];
}
You need to move the boxes down as you go:
TextBox lbl = new TextBox { Location = new Point(15, 30 * i), BorderStyle ....
Note that your form would need to be large enough to see them all, as well. You may need to set your height appropriately, or place the text boxes inside of a container which could scroll, instead of directly on the form itself.
I have a tablelayout panel in which I am adding rows programmatically. When Adding rows to this container the first time the form is opened (using statement with new ValidationForm()) the table displays fine. Upon the second time, I get added rows at the top for some reason? See the picture here:
This does not affect the number of rows that I use, just displays extra lines. And the number of lines increases each time i close and open the form. Here is the code i use to add rows:
private void insertRow(Label label, dynamic control)
{
// Create Panel
var panel = new Panel();
// Set Object Properties
label.TextAlign = ContentAlignment.MiddleCenter;
label.Dock = DockStyle.Fill;
control.Dock = DockStyle.Fill;
panel.Dock = DockStyle.Fill;
//generate delete button
var delete = new Button();
delete.Text = "X";
delete.ForeColor = System.Drawing.Color.Red;
delete.MaximumSize.Height.Equals(40);
delete.Name = rowCount.ToString();
delete.Dock = DockStyle.Right;
delete.Click += new EventHandler(deleteRow);
// Add Controls to the panel
panel.Controls.Add(control);
panel.Controls.Add(delete);
// add controls
//tableLayoutPanel_Validations.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 48F));
tableLayoutPanel_Validations.RowCount = rowCount + 1;
tableLayoutPanel_Validations.Controls.Add(label, 0, rowCount);
tableLayoutPanel_Validations.Controls.Add(panel, 1, rowCount);
tableLayoutPanel_Validations.RowStyles.Add(new RowStyle(SizeType.AutoSize));
rowCount++;
}
rowCount is a private int that is set to 0 at runtime.
My first instinct is that the entire form would "reset" after the using statement. The form declaration is public partial class ValidationForm: Form {}
Using statement opening the form:
using (AVBuilder.ValidationForm validationBuilder = new AVBuilder.ValidationForm())
{
if (validationBuilder.ShowDialog() == DialogResult.OK)
{
// ADD ITEM TO Validations LIST
ListViewItem result = new ListViewItem(validationBuilder.resultObject + " : " + validationBuilder.resultName);
result.SubItems.Add(validationBuilder.resultJson);
result.ToolTipText = result.Text;
listView_Validations.Items.Add(result);
//MessageBox.Show(validationBuilder.resultJson);
}
}
Make sure the rowCount variable is not declared static, as it will not reset to 0 on a new form instance.
I have a split container on panel 1 I have added a groupbox, in that groupbox is a flowcontrol which has dynamic number of textboxes, i have set both the groupbox and flowcontrol to dockstyle to fill.
In code i have also set the textboxes to dock style to fill, but they wont resize when i move the splitter, while the parent flowcontrol does resize.
Label labelInput = new Label();
TextBox listBoxNewInput = new TextBox();
listBoxNewInput.Name = ce.ToString();
labelInput.AutoSize = true;
labelInput.Font = new Font(labelInput.Font, FontStyle.Bold);
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Text = ts.ToString();
//listBoxNewInput.Width = 150;
listBoxNewInput.MinimumSize = new Size(200,150);
listBoxNewInput.MaximumSize = new Size(1000, 150);
listBoxNewInput.Dock = DockStyle.Fill;
listBoxNewInput.TextChanged += new EventHandler(listBoxNewInput_TextChanged);
//Add the newly created text box to the list of input text boxes
inputTextBoxesList.Add(listBoxNewInput);
//Add the labels and text box to the form
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.Controls.Add(listBoxNewInput);
if i try to put controls directly in to spliter panel 1 only the first two controls appear, which do resize when i move the splitter
splitContainer1.Panel1.Controls.Add(labelInput); splitContainer1.Panel1.Controls.Add(listBoxNewInput);
->if the controls when i put them in flow control resize, when i move the splitter that would be good
OR
->All controls appear when i put them directly into the splitter panel 1
Based on your comments and what I think you are trying to accomplish, I think you need to replace the FlowLayoutPanel with a TableLayoutPanel because it sounds like you are just stacking one TextBox below another.
Create a TableLayoutPanel with 1 column and 1 row.
Here is a working example:
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 150));
for (int i = 0; i < 4; i++) {
AddTextBox("TextBox #" + i.ToString());
}
private void AddTextBox(string info) {
TextBox tx = new TextBox();
tx.Multiline = true;
tx.Text = info;
tx.ScrollBars = ScrollBars.Vertical;
tx.WordWrap = true;
tx.Height = 150;
tx.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
tableLayoutPanel1.Controls.Add(tx);
}
Instead of docking, I set the height of the TextBox and then I set the Anchors so that when the SplitPanel resizes, the TextBoxes resize appropriately.