Dynamically added shortcuts do not appear

I have several shortcuts and a bunch of TextBox that I want to dynamically add to the Panel. Text fields are added OK and are clearly visible, but these labels are not. Here is the code I use to add shortcuts.

C # is written for the .NET 3.5 WinForms application.

Label lblMotive = new Label(); lblMotive.Text = language.motive; lblMotive.Location = new Point(0, 0); Label lblDiagnosis = new Label(); lblDiagnosis.Text = language.diagnosis; lblDiagnosis.Location = new Point(20, 0); panelServiceMotive.Controls.Add(lblMotive); panelServiceMotive.Controls.Add(lblDiagnosis); 

panelServiceMotive is a Panel control that should display shortcuts, as well as the previously mentioned text fields. language is an object of a self-written language class that works fine and does not matter here.

I hope there is enough information to get help.

+4
source share
4 answers

It seems that the main problem is the location of the controls that you add to the panel. The Location property contains the coordinates of the upper left edge of the control relative to the upper left corner of the parent control (the control in which you add child controls). Looking at your code, it seems like you are adding controls one on top of the other. Note that you always set lblDiagnosis.Location = new Point(0, 0); . If you add controls from the code, the first control that you add will cover all the other controls that you add in one place (as opposed to using the constructor).

You can try something like this to check if these labels are good:

 Label lblMotive = new Label(); lblMotive.Text = language.motive; lblMotive.Location = new Point(0, 40); Label lblDiagnosis = new Label(); lblDiagnosis.Text = language.diagnosis; lblDiagnosis.Location = new Point(0, lblMotive.Location.Y + lblMotive.Size.Height + 10); panelServiceMotive.Controls.Add(lblMotive); panelServiceMotive.Controls.Add(lblDiagnosis); 
+1
source

I just threw your code into an empty application form and it works great:

 private void button1_Click(object sender, EventArgs e) { Panel panelServiceMotive = new Panel(); Label lblMotive = new Label(); lblMotive.Text = "motive"; lblMotive.Location = new Point(0, 0); Label lblDiagnosis = new Label(); lblDiagnosis.Text = "language"; lblDiagnosis.Location = new Point(100, 0); panelServiceMotive.Controls.Add(lblMotive); panelServiceMotive.Controls.Add(lblDiagnosis); this.Controls.Add(panelServiceMotive); } 

Something else must be wrong with your code, which we cannot see from your published code.

+1
source

What do you set for the text in 'language.Motive' 'language.diagnosis', does this come from a resource file or a string const or something else?

I suggest you set them to a solid coded value or check to make sure there is no zero at first.

Also try changing the position of the text field, as they may overlap.

0
source

Do I need to add shortcuts at runtime? A simpler approach is to add shortcuts to the form designer and update the text at run time. If the number of shortcuts required is unknown at design time, another control, such as a ListBox or DataGridView, may be more suitable. Also, look at FlowLayoutPanel as an alternative container for shortcuts; unlike a regular panel, it automatically controls the layout of controls.

0
source

All Articles