Adding textboxes over the panel height - c#

So I need to add textboxes to a panel with a click of a button. Every click adds one textbox under the last one and so on. But when it goes over the panel height it suddenly makes bigger space between the texboxes even though the int is still the same.
Here's my code so far.
List<TextBox> textboxes = new List<TextBox>();
private void button1_Click(object sender, EventArgs e)
{
tbY += 30;
TextBox tb = new TextBox();
tb.Left = 3;
tb.Top = tbY;
tb.Font = new Font("Verdana", 12, FontStyle.Bold);
tb.Size = new Size(325, 25);
tb.BorderStyle = BorderStyle.None;
button1.Top = tbY;
panel1.Controls.Add(tb);
textboxes.Add(tb);
ScrollToBottom(panel1);
}

The Top of a Control is calculated relative to the scroll position of its Parent.
You are always scrolling to the bottom of your Panel, so you need to set it like this, taking the curent scroll position into account:
tb.Top = tbY + panel1.AutoScrollPosition.Y;
Note that the AutoScrollPosition.Y is negative when you have scrolled downward, so we need to add it!

You might as well use a flowLayoutPanel for this purpose. Use the following properties on your flowLayoutPanel and it'll work as you intend. (without having to do the manual calculation)
List<TextBox> textboxes = new List<TextBox>();
public Form1()
{
InitializeComponent();
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel1.AutoScroll = true;
}
private void button1_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Left = 3;
tb.Font = new Font("Verdana", 12, FontStyle.Bold);
tb.Size = new Size(325, 25);
tb.Text = tb.Top.ToString();
tb.BorderStyle = BorderStyle.None;
flowLayoutPanel1.Controls.Add(tb);
textboxes.Add(tb);
}

Related

Make dynamically created panels in winform responsive (same size as my flowlayoutPanel) - C#

So I'm creating some panels with the following code
private void button1_Click(object sender, EventArgs e)
{
int xlocation = 5;
for (int i = 0; i < 10; i++)
{
Panel panel = new Panel();
{
panel.Name = string.Format("{0}", i);
panel.Text = string.Format(i.ToString());
panel.BackColor = Color.White;
panel.Location = new System.Drawing.Point(xlocation, 30);
panel.Width = flowLayoutPanel1.Width;
panel.Height = 50;
panel.MouseEnter += new System.EventHandler(this.panel_MouseEnter);
flowLayoutPanel1.Controls.Add(panel);
Label label = new Label();
label.Location = new System.Drawing.Point(15, 10);
label.AutoSize = true;
label.Font = new System.Drawing.Font("Calibri", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
label.Text = string.Format("{0}", "GNU" + i);
panel.Controls.Add(label);
Label label10 = new Label();
label10.Location = new System.Drawing.Point(15, 35);
label10.AutoSize = true;
label10.Font = new System.Drawing.Font("Calibri", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label10.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
label10.Text = string.Format("{0}", "hest");
panel.Controls.Add(label10);
}
xlocation = xlocation + 85;
}
}
The problem is when I resize my form, my flowLayoutPanel obviously gets bigger, but the panels inside doesn't.
I have tried using the
private void Form1_Resize(object sender, EventArgs e)
{
}
But it doesn't seem to work.
Any answer is much appreciated!
Thanks!
You can either use the Anchor property and set it to Right and Left, or can dock each panel to parent control. Something like this should work:
Panel panel = new Panel();
panel.Dock = DockStyle.Top; // Docks the panel to top, and fills to the width of Parent control
panel.Anchor = (AnchorStyles.Right | AnchorStyles.Left); // Set anchor to right and left
I found a solution. I just created a list with my panels like so
List<Panel> pnls = new List<Panel>();
And just did like so to make them follow the width of my flowlayoutpanel, so you can actually use a flowlayoutpanel ;-)
private void Kontrol_Resize(object sender, EventArgs e)
{
for (int i = 0; i < pnls.Count; i++)
{
pnls[i].Width = activityData.Width - 24;
pnls[i].Height = activityData.Height / 4;
}
}

How can I add all checked boxes from checklistbox to a new form?

I want to get all items from CheckedBoxList and add it to a new Form so for every checked item I want a new Label with checkedItem name and a TextBox. So far I'm doing that but when I open the form I got no results at all. I don't know how to get the checked item name and I'm doing this:
labels[i].Text = i.ToString();
private void Button4_Click(object sender, EventArgs e)
{
testForm = new Test();
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
int n = 0;
for (int i=0;i<checkedListBox1.CheckedIndices.Count;i++)
{
txtBox = new TextBox[checkedListBox1.CheckedIndices.Count];
labels = new Label[checkedListBox1.CheckedIndices.Count];
labels[i] = new Label();
labels[i].Text = i.ToString();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(labels[i]);
txtBox[i] = new TextBox();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(txtBox[i]);
}
Controls.Add(tableLayoutPanel);
testForm.ShowDialog();
}
Any suggestions?
Thank you, for the invested time.
The solution was to change the code to:
testForm.Controls.Add(tableLayoutPanel);

Dynamic button and create textbox

I have a textbox and button on c# form and users can enter number.I create a label which users want and each label have a button.Here if I click those buttons i wanna create textbox but if users continue to click,i want to create more textbox.
Button[] Btn= new Button[10];
for (int i = 0; i < labelNumber; i++)
{
Btn[i] = new Button();
Btn[i].Text = "Add";
Btn[i].Location = new Point(40, 100 + i * 29);
Btn[i].Size = new Size(50,20);
this.Controls.Add(Btn[i]);
Btn[i].Click += new EventHandler(addNewTextbox);
}
on the code above; for example; if labelNumber == 3 so i have 3 label and 3 button with them, if i click add button i wanna create textbox near thislabel.
private void addNewTextbox(object sender, EventArgs e)
{
TextBox[] dynamicTextbox = new TextBox[10];
Button dinamikButon = (sender as Button);
int yLocation = (dinamikButon.Location.Y - 100) / 29;
//int xLocation = dinamikButon.Location.X - 100;
dynamicTextbox[yLocation] = new TextBox();
dynamicTextbox[yLocation].Location = new Point(100, 100 + yLocation * 29);
dynamicTextbox[yLocation].Size = new Size(40, 50);
this.Controls.Add(dynamicTextbox[yLocation]);
}
here i change textbox y coordinates but i couldn't it for X. if i change this
dynamicTextbox[yLocation].Location = new Point(100*x, 100 + yLocation * 29);
x++;
it sort equals all of them.
Label1 Button1
Label2 Button2
Label3 Button3
if i click Button1 4 times,it has to create 4 textbox alongside label1. and if i click Button2 2 times,it has to create 2 textbox alongside label2
Please Help ME.
The simplest way is to keep a list containing the created textboxes in the button's Tag property like this
private void addNewTextbox(object sender, EventArgs e)
{
var button = (Button)sender;
var textBoxes = button.Tag as List<TextBox>;
if (textBoxes == null)
button.Tag = textBoxes = new List<TextBox>();
var textBox = new TextBox();
textBoxes.Add(textBox);
textBox.Location = new Point(100 * textBoxes.Count, button.Top);
textbox.Size = new Size(40, 50);
this.Controls.Add(textBox);
}
This way you not only can add a new text box, but also can easily determine the created text boxes by each button at any time if needed.

Push down control automatically not working

Sometime ago I asked this question. I get an answer and it worked fine at time. But now, I'm trying to do the same but isn't working. I have a Form and a FlowLayoutPanel setted in the same way as the answer but it isn't working. Both Form has FLowLayoutPanel has set AutoSize to true and FlowDirection set to TopDown but the form is growing vertically without pushing down the progressBar control and label itself. Here's what's like my form after click on button a couple of times(the button's code is the same as in the accepted question in the link I have linked):
What am I missing?
Try this and see if it works!
public Form1()
{
InitializeComponent();
Size = new Size(400, 150);
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
FlowLayoutPanel panel = new FlowLayoutPanel();
panel.Size = new Size(200, 150);
panel.MaximumSize = new System.Drawing.Size(panel.Width, int.MaxValue);
panel.FlowDirection = FlowDirection.TopDown;
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
Controls.Add(panel);
Label label = new Label();
label.Text = "Starting text!\n";
label.Padding = new System.Windows.Forms.Padding(0, 0, 0, 50);
label.AutoSize = true;
panel.Controls.Add(label);
ProgressBar progressBar = new ProgressBar();
progressBar.Location = new Point(0, 125);
progressBar.Size = new Size(190, 25);
panel.Controls.Add(progressBar);
Button button = new Button();
button.Location = new Point(275, 50);
button.Text = "Click me!";
button.Click += (object sender, EventArgs e) => { label.Text += "some more text, "; };
Controls.Add(button);
}
Ok, I've tested the solution suggested in the earlier post you made and it's working fine for me...
Test these things:
Make sure both the Label and the ProgressBar are located inside the FlowLayoutPanel
If you mean that it's growing horizontally <---->, then set the MaximumSize-Width of the FlowLayoutPanel to how wide it can be before switching to a new row (and from there growing vertically instead!)
Otherwise please provide more information so that I can help you from there.

How to check if a space on the form has an object

I'm working on a mind-map project. I'm trying to get the "New Bubble" button to create a new textbox into a FREE space on the form. So I want to check if there is another bubble in the place where it's getting created. If it already has a textbox then I want it to find a new place and repeat the process.
How can I do this?
public partial class frmMap : Form
{
private void btnProperties_Click(object sender, EventArgs e)
{
new frmProperties().Show();
}
private void btnNewBubble_Click(object sender, EventArgs e)
{
var tb = new TextBox();
tb.Multiline = true;
tb.BorderStyle = BorderStyle.FixedSingle;
tb.Top = 100;
tb.Left = 200;
tb.Size = new Size(100, 100);
this.Controls.Add(tb);
}
}
You can check "collision" with other controls like so:
foreach (Control checkControl in Controls)
{
if (tb.Bounds.IntersectsWith(checkControl.Bounds))
...
}
Of course, thats a lot of checking to do! If you are just going to "grid" the controls, it would be faster/easier to just layout a bool array that holds the state of each "cell" (filled/empty) then pick the first empty one you find.
Create dynamic textbox:
var tb = new TextBox();
tb.Multiline = true;
tb.BorderStyle = BorderStyle.FixedSingle;
tb.Top = 100;
tb.Left = 200;
tb.Size = new Size(100, 100);
Then use Rectangle.IntersectWith to check if new textbox intersects with other already added texboxes (you can remove control type filter, if you have other type of controls to check):
while(Controls.OfType<TextBox>().Any(x => tb.Bounds.IntersectsWith(x.Bounds))
{
// adjust tb size or position here
}
And last step - add textbox to form:
Controls.Add(tb);

Categories

Resources