Code help - dynamic control properties

gabe.m

New member
Joined
Jan 23, 2017
Messages
3
Programming Experience
3-5
I am trying to change the properties of an existing button.

Here's the logic behind it:
- interrogate database and create button names list
- create all buttons inside control
Later:
- interrogate database and create button names list (just to double check if there are any updates)
- create a new button and assign name
- check if the button exists already
- if YES, reposition this button to new location

Basically, all this comes to is: if btn exists, select it somehow and change its properties.


Here's the code wrap around it, with the commented portion definitely not working :)

C#:
				controlName = "btnModelSelection" + i.ToString();				Button btn = new Button();
				btn.Name = controlName;
				if (this.pnlModelSelection.Controls.Contains(btn))
				{
					//this.pnlModelSelection. = new System.Drawing.Point(btnX, btnY);
				}
				else
				{
					btn.Size = new System.Drawing.Size(btnWidth, btnHeight);
					btn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
					btn.Location = new System.Drawing.Point(btnX, btnY);
					btn.Text = modelSelectionButtonNames[i];
					btn.Font = new Font("Microsoft Sans Serif", 12);
					this.pnlModelSelection.Controls.Add(btn);
				}
 
When you create a new object then that is not the same as another object.
Controls(string_name) will return a control that has that Name or null. You can then check for null and either create a new object or use the one that was returned.
There is also Controls.ContainsKey method that could be used as alternative, but would need to get it with Controls(string_name) anyway.
 
I just thought I can simply change btn.location value, see what happens, hopefully I am not going to confuse the heck out of C#... anyways, this might not be the smartest idea, as btn is not the same as panel.btn... I'll see if it works.

Using Controls(string_name) - all I'm seeing is that it cannot be used as a method.

I might just delete it somehow and create a new one.


C#:
                controlName = "btnModelSelection" + i.ToString();                Button btn = new Button();
                btn.Name = controlName;


                if (this.pnlModelSelection.Controls.Contains(btn))
                {
                    btn.Location = new System.Drawing.Point(btnX, btnY);
                }
                else
                {
                    btn.Size = new System.Drawing.Size(btnWidth, btnHeight);
                    btn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
                    btn.Location = new System.Drawing.Point(btnX, btnY);
                    btn.Text = modelSelectionButtonNames[i];
                    btn.Font = new Font("Microsoft Sans Serif", 12);
                    this.pnlModelSelection.Controls.Add(btn);
                }
 
Using Controls(string_name) - all I'm seeing is that it cannot be used as a method.
No, it's a collection, in code you use square brackets.
 
Ended up clearing all the controls in the form for now... I will figure it out in the future.

C#:
this.pnlModelSelection.Controls.Clear();
 
Back
Top Bottom