c# objects won't go under each other - c#

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.

Related

RichTextBox doesnt show scrollbars and text goes off limit

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.

Dynamic button to fit the area size

I am trying to create a system that has X amount of buttons, like a POS system. Number of buttons will be different based on the number of items user input. My question is if I should create it dynamically like the following:
private void createButton(int numOfBtn)
{
for (int i = 0; i < numOfBtn; i++)
{
Button btn = new Button();
btn.Name = "button";
btn.Text = "button";
btn.ForeColor = Color.White;
btn.BackColor = Color.Green;
btn.Font = new Font("Serif", 24, FontStyle.Bold);
btn.Width = 170;
btn.Height = 80;
btn.TextAlign = ContentAlignment.MiddleCenter;
btn.Margin = new Padding(5);
}
}
Should create buttons one by one on the designer, dynamically like the above code or if there is a better way of doing so?
Also, currently I am having the windows set to Maximum size when it launches, so when I am creating the buttons, how can I tell if the number of button has max out the space(flowlayout).
Edit: Is it better to create all the buttons from the designer first and assign value of it after? but this way there will always have a maximum amount of buttons, let says if I create 20 buttons from the designer, at max I can only assign twenty items.. what would the better way of doing such task?

Create GroupBox and labels inside it programmatically

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.

How can i add several labels to a list in a for loop?

I want to be able to do something like this so that i can just add label then label1 label 2 label3 etc and i cant create an array of new labels or anything since my labels have an image attached to them and have to keep a certain position as well. So making a new label that has some text as a label that wont show up anywhere in particular on my form isn't much use.
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
labels.Add(label + (i.ToString()));
}
You can add Labels to list from Form by name like this :
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
var label =
this.Controls.OfType<Label>().Where(lb => lb.Name == "label" + i).FirstOrDefault();
if (label != null)
labels.Add(label);
}
Here is a Code Sample:
You need to add the labels to the Controls collection of the tab page:
Page2.Controls.Add(CustomLabel[CustomLabel.Count - 1] as Control);
List<Label> customLabels = new List<Label>();
foreach (string ValutaCustomScelta in Properties.Settings.Default.ValuteCustom)
{
Label label = new Label();
label.Location = new System.Drawing.Point(317, 119 + customLabels.Count*26);
label.Parent = Page2;
label.Name = "label" + ValutaCustomScelta;
label.Text = ValutaCustomScelta;
label.Size = new System.Drawing.Size(77, 21);
customLabels.Add(label);
Page2.Controls.Add(label);
}
Have you added the labels by code or by design?
If you added them by code just add them then to the list.
If you added them by design there are two options. They are all on the same container (panel, form, groupbox...) or they are not.
If they are you can:
for each(control ctr in $yourcontainer.controls)
{
if(ctr is Label)
{
labels.add(ctr);
}
}
If they are not in the same container you can add them by hardcode
labels.add(%label1name);
labels.add(%label2name);
labels.add(%label3name);
labels.add(%label4name);
But if they are too much, you can run the code before for each one of the panels

How do I set event methods for programmatically created combo boxes?

I am creating small program that has dynamically created combo boxes. Each time the user opens the program, based on some context, there could be 3-30 items that need 4 drop down lists for selection. I am creating these based off of the following code, which is just a snippet.
for (int i = 0; i < 4; i++)
{
s.Children.Add(new ComboBox()
{
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(m, -25, 0, 0),
Width = 75,
Height = 25,
FontSize = 12,
Name = "obj1_" + i.ToString(),
ItemsSource = objs,
});
m = m + 50;
s.Children.Add(new Label()
{
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(m, -25, 0, 0),
Width = 25,
Height = 25,
FontSize = 12,
Name = "lbl1_" + i.ToString(),
});
}
s is a stack panel that I am adding each of the combo boxes too. The ItemSource is from a small method elsewhere to figure out which list should go into the drop down.
My question is, how do I call the events for these created combo boxes? Trying
private void obj1_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox this_box = (ComboBox)sender;
lbl1_1.Content = "!!!";
}
works well enough but the label doesn't exist in the current context.
Also, am I creating the boxes and labels the best way for this type of scenario?
Thanks in advance.
Inside you for loop
1)Create an new Panel
2)Add the label and combo to that panel
3)Add the newly created panel to s
Inside your obj1_1_SelectionChanged event:
1)Find the Parent control of the ComboBox
2)Search for the label inside its Children and update its text
Solution 2
When you create your controls create a Guid (or an int) and set the Tag property of your controls to that object.
Now when you are on a combo you can search your Window for the label with the same Tag
for (int i = 0; i < 4; i++)
{
Guid g = Guid.NewGuid();
s.Children.Add(new ComboBox()
{
Tag = g
});
s.Children.Add(new Label()
{
Tag = g
});
}

Categories

Resources