I have no code to show because this is a problem with me not understanding the behavior of the designer in VS2015 using C#. I have added a series of labels to a panel, so I can iterate through them in code. The problem is, it seems no matter what order I add the labels to the panel, the indexes of the controls make no sense.
Here is a screen shot. The back colored labels to the right are all contained in a separate panel. I have coded the labels to show their index within the panel container.
These were added one at a time from the bottom up. How can I manually add the labels and still have predictable indexes?
Any help is appreciated.
This is the result after making the labels the same size, renaminging them lbl0, lbl1 etc. and adding them one at a time from top to bottom....
You can see where controls are added to the control collection if you look in the Form.Designer.cs file, which is part of the class definition for your form.
Here you will see a section that begins with // Form, and under that you will see where it calls `this.Controls.Add();
The items in this list appear in the order in which you dropped them onto the form (at least for me they do). I just copy/pasted 10 labels onto the form, and I see this:
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 684);
this.Controls.Add(this.label10);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
At runtime, the controls are found in the collection exactly as you would expect from reading the code above; the last control added is at index [0]:
Now, adding a panel is a slightly different story, but not much. Because the Panel is a container object, the labels get added to the Panel controls collection:
//
// panel1
//
this.panel1.Controls.Add(this.label18);
this.panel1.Controls.Add(this.label17);
this.panel1.Controls.Add(this.label16);
this.panel1.Controls.Add(this.label15);
this.panel1.Controls.Add(this.label14);
this.panel1.Controls.Add(this.label13);
this.panel1.Controls.Add(this.label12);
this.panel1.Controls.Add(this.label11);
this.panel1.Location = new System.Drawing.Point(37, 366);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 172);
this.panel1.TabIndex = 13;
Note, however, that the behavior is the same. The most recent control is added to the Controls collection first, and will be in the Panel.Controls[0] position.
Related
For a Computer Graphics class in school I have been assigned to use a GLControl in my project. I have downloaded and installed OpenTK and OpenTK.GLControl. Both of the references appear within my project references tab in solution explorer. I have been trying to find out how to add the GLControl to my toolbox.
The things I have tried.
I have done the tools -> choose toolbox tools -> (select GLControl) but it isn't there to select to add to toolbox
I have attempted to drag the reference of the tool to the toolbar as someone suggested doing on the web.
I have uninstalled my nuget packages and reinstalled them.
Crying for hours and hoping that it works.
What if anything is there that I can do to make this work as I am unable to get anywhere without this control.
As the author of an IDE that makes heavy use of OpenTK's GLControl (SGDK2) I can tell you that I never put the GLControl in the toolbox, and I don't think I would want to. It's not exactly the kind of control you want to deal with at design time. Instead I simply added a dummy control (a Panel) to my form, then went into the designer code file (Form1.Designer.cs, for example) and replaced every occurrence of System.Windows.Forms.Panel with OpenTK.GLControl. Pretty much everything I do with the control is in code, so the designer integration isn't that important anyway.
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new OpenTK.GLControl();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(32, 37);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(223, 175);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private OpenTK.GLControl panel1;
EDIT:
That said, I did just try right-clicking on the General section of the Toolbox, selecting "Choose Items..." and browsing to the location of OpenTK.GLControl.dll. This added GLControl to the General category in my toolbox. So I'm not sure why you would be having difficulty. But hopefully forcing it onto a form as shown above will either get it into your project anyway or give you a better error or explanation about why it's not working with your project.
I want to build a form that has 100 label and 100 text box
what I did is:
add new form
add panel to that form using drag and drop
change the dock property of that panel to fill
change the AutoScroll property to True
start adding the labels and text boxes using drag and drop
The problem
I added like 40 labels and text boxes but I can't add any more because I can't expand the form nor the label vertically.
Note
I can minimize the size of the panel and a vertical scroll bar appears. (maybe this information helps you to help me).
A data entry window with that many text boxes is going to require scrolling. So set the Panel's AutoScrollMinSize property to, say, (1000, 1000) as a first guess. You'll see the scrollbars appear. They work at design time as well, allowing you to scroll the panel and place the controls. High odds you should be using a DataGridView btw.
Something that needs to be said: the odds that you can get a human to enter 100 data items without any mistake are very close to zero. A very frustrating job for the hapless user, it will take him 10 or more minutes only to arrive at failure. Create a user friendly UI, one that partitions the data entry job in small steps that can be successfully completed. Automatically solves this problem as well.
Set parent form's properties AutoSize and AutoScroll to true. Then disable docking for your panel. This way you can set any size to panel and scroll form contents to add new controls. When panel design is done, set docking to Fill again.
Or you can set position for newly added controls using Properties panel. This will move controls to appropriate position on the panel.
This is a sample method I've used to add an unknown number of controls to a form. The trick is a FlowLayoutPanel.
As has been said before: you don't want 100 manually added controls on your page.
private void AddMappingControls() {
HeaderFlowLayoutPanel.Controls.Clear();
MappingFlowLayoutPanel.Controls.Clear();
Label sourceHeaderLabel = new Label();
sourceHeaderLabel.Text = "Velden in Excel (bron)";
sourceHeaderLabel.Name = "BronLabel";
sourceHeaderLabel.Width = MappingFlowLayoutPanel.Width / 2 - 20;
HeaderFlowLayoutPanel.Controls.Add(sourceHeaderLabel);
Label destinationHeaderLabel = new Label();
destinationHeaderLabel.Text = "Velden in Word sjabloon (bestemming)";
destinationHeaderLabel.Name = "BestemmingLabel";
destinationHeaderLabel.Width = MappingFlowLayoutPanel.Width / 2 - 20;
HeaderFlowLayoutPanel.Controls.Add(destinationHeaderLabel);
foreach (string destination in this.destinationFields) {
ComboBox sourceFieldComboBox = new ComboBox();
sourceFieldComboBox.BindingContext = new System.Windows.Forms.BindingContext();
sourceFieldComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
//sourceFieldComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
sourceFieldComboBox.Name = destination + "ComboBox";
sourceFieldComboBox.ValueMember = destination;
sourceFieldComboBox.DataSource = this.sourceFields;
sourceFieldComboBox.Width = MappingFlowLayoutPanel.Width / 2 - 20;
MappingFlowLayoutPanel.Controls.Add(sourceFieldComboBox);
Label nameLabel = new Label();
nameLabel.Text = destination;
nameLabel.Name = destination + "Label";
nameLabel.Width = MappingFlowLayoutPanel.Width / 2 - 20;
MappingFlowLayoutPanel.Controls.Add(nameLabel);
}
}
I meant exactly the same as MeanGreen but he was first. I have created sample solution: https://www.amazon.com/clouddrive/share?s=i9N7raPPQPEjOdHPRn99uE
I am designing an application in which i am using tab-control, and in one of the tab-page the information i want to display in bigger than the form size, the information is displayed in various text-boxes. i tried by adding following lines in designer code but it is still not working.
this.AutoScroll = true;
this.AutoScrollMargin = new System.Drawing.Size(20, 20);
this.AutoScrollMinSize = new System.Drawing.Size(this.Width, this.Height);
any help would be appreciated.
You have to set the AutoScroll on the TabPage, not the Form, you can do this at design time by selecting your tabpage first, then set the AutoScroll to true in the Properties window, or you can do by code like this:
tabPage1.AutoScroll = true;
//do the same for other tabPages
Notice that "this" refer to whole class (your form).
Increase the value like this:
tab.AutoScrollMinSize = new System.Drawing.Size(1000,1000);
Or you can add panel1 to your tab, then dock it into the tab:
panel1.dock = dockingSyle.Fill;
Now you can make panel1 scrollable.
I have a TableLayoutPanel with a grid of PictureBox controls within it. I'm trying to find a shortcut way to change them all to Label controls instead of manually deleting each one and placing new controls in each cell.
I thought I could go into the designer code and find/replace PictureBox with Label, but now I get an
"Object does not match target type"
error in Visual Studio's error list. I can't view the designer page now either. Is this not allowed? If it is allowed, what's the right way to do it?
If you take a closer look at the generated code:
label1:
this.label1 = new System.Windows.Forms.Label();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 163);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
pictureBox1:
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(97, 75);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
My guess is that the
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
is changed by you into something like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
which doesn't work, and results in designer issues. Object does not match target type.
so, apply the changes you already did, remove the lines like:
((System.ComponentModel.ISupportInitialize)(this.label1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.label1)).EndInit();
and I think you're good to go.
Don't change designer code. That stuff is automatically generated. Not only can your changes cause unexpected behavior, but they can get over-written as well.
I would attempt to make a change or 2 to your form, or whatever your designer is behind, and hope it regenerates all it's code.
You can delete all the picture boxes in the designer, then add all the labels in the _load event (or another convenient event). That way it will be easier to change next time.
As Haxx illustrated, you will have to clean-up the extra initialization PictureBox requires as well. The error you received is a interface casting error. In your case, as Haxx guessed, the Label control doesn't implement the ISupportInitialize interface.
Unlike most, I am not afraid of changing designer code in the interest of expediency, for what you are doing, it is ok to do so. Just know your objects, check-in prior to doing so, and don't put custom code in there!
plz keep in mind I'm very noobish...
What I'm trying to do is add "blips" to my form. I have a calculation that determines where all these "blips" will be, which is plotted on a graph. the solution determines that the coordinates are "blipHours, blipAltitude"
I want to somehow add small dots to my form at these locations. At first I was going to create something to be my "blips" but then I realized that I want it so small that I could just use an empty picture box with the background color what I want (I know this isn't the best way but I'm still very new to this).
I've created the code that will add the blip
PictureBox blip = new PictureBox();
blip.Location = new Point(blipHours, blipAltitude);
blip.Size = new Size(6, 6);
blip.BackColor = System.Drawing.Color.Lime;
blip.Text = "";
blip.Name = callsign;
this.Controls.Add(blip);
It adds the blip, but it always adds it underneath other controls. Is there a way to make it add the new blip on top of everything else so that it's visible?
my second question is how do I remove all of the blips that get created at once with the click of a button?
An alternative to nobugz' answer is to change the Z-order of your controls via the Form.Controls.SetChildIndex method:
this.Controls.Add(blip);
this.Controls.SetChildIndex(blip, 0);
You can use .AddAt to set its position in the list of controls.