I have winform on which are dynamically created 52 radio buttons.
This is method for creating them:
private void CreateRadioButton()
{
int rbCount = 52;
int numberOfColumns = 23;
radioButtons = new RadioButton[rbCount];
int y = 520;
for (int i = 0; i < rbCount; i++)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = Convert.ToString(i + 1);
if (i % numberOfColumns == 0)
y += 20;
var x = 11 + i % numberOfColumns * 50;
radioButtons[i].Location = new Point(x, y);
radioButtons[i].Size = new Size(40, 15);
//radioButtons[i].Anchor = AnchorStyles.Left;
//radioButtons[i].Anchor = AnchorStyles.Bottom;
radioButtons[i].Font = new Font(radioButtons[i].Font.FontFamily, 8, FontStyle.Bold);
radioButtons[i].UseVisualStyleBackColor = true;
radioButtons[i].Click += new EventHandler(rbtns_click);
xtraTab.Controls.Add(radioButtons[i]);
}
}
There is problem when form is maximized. Radio buttons disappear.
If I set
radioButtons[i].Anchor = AnchorStyles.Left;
radioButtons[i].Anchor = AnchorStyles.Bottom;
The radio buttons are overlayed.
What can I do to keep their position on the same place if form is resized?
these two lines
radioButtons[i].Anchor = AnchorStyles.Left;
radioButtons[i].Anchor = AnchorStyles.Bottom;
mean that Anchor value AnchorStyles.Left will be replaced by AnchorStyles.Bottom
AnchorStyles has Flags attribute set, enum values can be combined:
radioButtons[i].Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
if set via Designer, in "Windows Form Designer generated code" it looks like this:
this.radioButton1.Anchor = ((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
Related
I have dynamic Labels and TextBox's on one panel.
I can delete the Panel. No Problem but then I also don't know how to delete the Textboxes etc
and i hoped that i can refresh or clear the panel so that all labels and textboxes will deleted..
Label makeLabelC = new Label();
makeLabelC.Width = 100;
makeLabelC.Font = new Font(makeLabelC.Font.Name, 8, FontStyle.Bold | FontStyle.Underline);
makeLabelC.Location = new Point(400, 100);
makeLabelC.Name = e.Node.Text;
makeLabelC.Text = e.Node.Text;
this.Controls.Add(makeLabelC);
this.Controls.Add(panel1);
TextBox textboxC = new TextBox();
textboxC.Width = 100;
textboxC.Location = new Point(500, 100 );
textboxC.Name = e.Node.Text + "lbl";
textboxC.Text = "enter here";
this.Controls.Add(textboxC);
this.Controls.Add(panel1);
for (int z = 0; z < n; z++)
{
Label makeLabel = new Label();
makeLabel.Width = 100;
makeLabel.Location = new Point(400, 150 + 2 * z * makeLabel.Height);
makeLabel.Name = e.Node.Text;
makeLabel.Text = e.Node.Nodes[z].Text;
this.Controls.Add(makeLabel);
this.Controls.Add(panel1);
TextBox textbox = new TextBox();
textbox.Width = 100;
textbox.Location = new Point(500, 150 + 2 * z * textbox.Height);
textbox.Name = e.Node.Text + "lbl";
textbox.Text = "enter here";
this.Controls.Add(textbox);
this.Controls.Add(panel1);
}
}
is there a way with panel how to do this or an other solution?
I thought that the Panel can help me there...
thanks Janik
You are adding the controls to the form instead of the panel - which you also add multiple times
this.Controls.Add(panel1); // do this once
panel1.Controls.Add(textbox); // add the controls to the panel
Once you have done this, when you remove the panel, you will also remove its child controls.
I have this small piece of code inside a method that is activated when a user press a button in my main form:
int tmp = 0;
int j = 20;
var bookdp = new Book(); // DIFFERENT FORM
while(books.Count > tmp)
{
bookdp.Controls.Add(new Button()
{
Text = "" + this.books[tmp],
Size = new System.Drawing.Size(200, 75),
Location = new System.Drawing.Point (20, j),
TextAlign = ContentAlignment.MiddleLeft,
Name = "" + button1 + tmp
});
tmp++;
j += 80;
}
bookdp.Show();
this.Hide();
Ok, this basically creates a 2/or more buttons in my implementation. The question is, how can I acess this buttons inside the "var bookdp = new Book();" form, because I want that the user be able to click on the new books that appeared.
I'm new to C#.
Thanks for the reply.
You need to make those controls public and pass a reference of the owning from into bookdp.
Or if you want to access bookdp's controls from your current form.
int tmp = 0;
int j = 20;
var bookdp = new Book(); // DIFFERENT FORM
List<Button> buttonsAdded = new List<Button>(books.Count);
while (books.Count > tmp)
{
Button newButton = new Button()
{
Text = "" + this.books[tmp],
Size = new System.Drawing.Size(200, 75),
Location = new System.Drawing.Point(20, j),
TextAlign = ContentAlignment.MiddleLeft,
Name = "" + button1 + tmp
};
bookdp.Controls.Add(newButton);
buttonsAdded.Add(buttonsAdded);
tmp++;
j += 80;
}
bookdp.Show();
string text = buttonsAdded[0].Text;
this.Hide();
i have little app where I have one panel containing 6 more panels (inside this panels pictures), and when i click on the button, botton this panel dynamically created couple panels, and when panels create whole this panels are blinking... it's looks not good... please, tell me, what to do?
a cycle where I created the panel, clicking on a button to created panel adds the same number of panels
for (int i = 0; i < list_afy_add.Count; i++)
{
Panel main_panel = new Panel();
main_panel.Name = i.ToString();
main_panel.Width = 308;
main_panel.BackColor = Color.Transparent;
main_panel.Location = new Point(x, y);
Panel panel = new Panel();
panel.Name = i.ToString();
panel.MouseEnter += new EventHandler(panel_MouseEnter);
panel.Width = 300;
panel.Location = new Point(3, 5);
Label textBox_date = new Label();
panel.Controls.Add(textBox_date);
textBox_date.Name = "textBox_date" + i.ToString();
textBox_date.Location = new Point(220, 8);
textBox_date.Size = new System.Drawing.Size(70, 15);
textBox_date.BorderStyle = BorderStyle.None;
textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
textBox_date.TextAlign = ContentAlignment.MiddleRight;
textBox_date.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
textBox_date.ForeColor = SystemColors.InactiveCaption;
Label textBox_name = new Label();
panel.Controls.Add(textBox_name);
textBox_name.Size = new System.Drawing.Size(100, 15);
textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
textBox_name.Location = new Point(5, 8);
textBox_name.BorderStyle = BorderStyle.None;
textBox_name.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
textBox_name.ForeColor = SystemColors.InactiveCaption;
Label textBox_msg = new Label();
panel.Controls.Add(textBox_msg);
textBox_msg.Location = new Point(5, 30);
textBox_msg.Name = i.ToString();
textBox_msg.Tag = list_afy_add[i]._AddEventNotification.eventNotificationId;
textBox_msg.BorderStyle = BorderStyle.None;
textBox_msg.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
textBox_msg.ForeColor = SystemColors.Highlight;
textBox_msg.Cursor = Cursors.Arrow;
textBox_msg.MaximumSize = new System.Drawing.Size(280, 100);
textBox_msg.MinimumSize = new System.Drawing.Size(280, 14);
textBox_msg.AutoSize = true;
textBox_msg.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
Label labelOpenChat = new Label();
labelOpenChat.Size = new System.Drawing.Size(90, 15);
labelOpenChat.Text = "Открыть чат";
labelOpenChat.Font = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(200)));
labelOpenChat.Location = new Point(10, 8);
labelOpenChat.BorderStyle = BorderStyle.None;
labelOpenChat.BackColor = Color.FromArgb(((int)(((byte)(203)))), ((int)(((byte)(208)))), ((int)(((byte)(217)))));
labelOpenChat.ForeColor = SystemColors.InactiveCaption;
panel.Controls.Add(labelOpenChat);
main_panel.Controls.Add(panel);
panel1.Controls.Add(main_panel);
panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
main_panel.Dock = DockStyle.Top;
date_and_msg_check(list_afy_add[i]._AddEventNotification.eventNotificationName, list_afy_add[i]._AddEventNotification.eventNotificationDate, list_afy_add[i]._AddEventNotification.eventNotificationMsg);
textBox_name.Text = valForName;
textBox_date.Text = valForDate;
textBox_msg.Text = valForMsg;
int height = textBox_msg.Size.Height + textBox_name.Height + 19;
panel.Height = height;
main_panel.Height = height + 10;
int panHei = panel.Height;
/*--------------------------------*/
Panel panel_top_left = new Panel();
Panel panel_top_right = new Panel();
Panel panel_bottom_left = new Panel();
Panel panel_bottom_right = new Panel();
Bitmap btm_msg_panel_top_left = new Bitmap(Properties.Resources.blue_t_l1);
panel_top_left.BackgroundImage = btm_msg_panel_top_left;
Bitmap btm_msg_panel_top_right = new Bitmap(Properties.Resources.blue_t_r);
panel_top_right.BackgroundImage = btm_msg_panel_top_right;
Bitmap btm_msg_panel_bottom_left = new Bitmap(Properties.Resources.blue_b_l);
panel_bottom_left.BackgroundImage = btm_msg_panel_bottom_left;
Bitmap btm_msg_panel_bottom_right = new Bitmap(Properties.Resources.blue_b_r);
panel_bottom_right.BackgroundImage = btm_msg_panel_bottom_right;
panel_top_left.Width = panel.Width;
panel_top_left.Height = height + 2;
panel_top_right.Width = btm_msg_panel_top_right.Width;
panel_bottom_left.Height = btm_msg_panel_bottom_left.Height;
panel_bottom_left.Width = btm_msg_panel_bottom_left.Width;
panel_bottom_right.Height = btm_msg_panel_bottom_right.Height;
panel_bottom_right.Width = btm_msg_panel_bottom_right.Width;
panel_top_right.Location = new Point(panel_top_left.Width - 4, 0);
panel_bottom_left.Location = new Point(0, panel_top_left.Height - panel_bottom_left.Height);
panel_bottom_right.Location = new Point(panel_top_left.Width - 4, panel_top_left.Height - panel_bottom_right.Height);
panel_top_right.Anchor = AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
panel_bottom_right.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
panel_bottom_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
panel_top_left.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
/*--------------------------------*/
Button delButt = new Button();
delButt.Name = list_afy_add[i]._AddEventNotification.eventNotificationId;
delButt.Click += new EventHandler(delButt_Click);
delButt.Size = new System.Drawing.Size(12, 12);
delButt.Location = new Point(292, 6);
delButt.Anchor = AnchorStyles.Right | AnchorStyles.Top;
delButt.BackColor = Color.Transparent;
delButt.FlatStyle = FlatStyle.Flat;
delButt.FlatAppearance.BorderSize = 0;
delButt.FlatAppearance.MouseDownBackColor = Color.Transparent;
delButt.FlatAppearance.MouseOverBackColor = Color.Transparent;
delButt.BackgroundImage = bmp_close_normal;
panel_top_left.Controls.Add(delButt);
delButt.Hide();
panel.Controls.Add(panel_bottom_right);
panel.Controls.Add(panel_top_right);
panel.Controls.Add(panel_bottom_left);
panel.Controls.Add(panel_top_left);
}
You can try using Reflection to set DoubleBuffered to true for all your panels like this:
public static class PanelExtensions {
public static void EnableDoubleBuffered(this Panel panel){
typeof(Panel).GetProperty("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(panel, true, null);
}
}
//Use it
yourPanel.EnableDoubleBuffered();
NOTE: I'm not sure if this works, just post here to help you try some approach which can solve your problem, otherwise I have no idea to make it better and will remove this answer.
Try to use the Control's SuspendLayout() method before adding the child controls, and don't forget to call the ResumeLayout() after you have finished. This should help to reduce or eliminate the blinking.
The essence of my program is the user can click for an amount of textboxes and radiobuttons they want to add by clicking on a button and using a numericUpDown. After clicking the button the program adds a button under the text boxes, but if I change the number of rows the first button stays and a new one gets created. So I would like to know how can I only have the one button? Would it also be possible for me to apply this with removing the textboxes and radiobuttons.
This is my code for the button handler
int length = (int)this.numericUpDownNumComp.Value;
bool created = false;
CustomButton c = new CustomButton();
for(int i = 0; i < length; i++)
{
//instantiate and configure the text boxes
textboxComputer.Add(new TextBox());
System.Drawing.Point p = new System.Drawing.Point(176, 114 + i * 25);
//to evoke an object in an ArrayList we use the 'as' keyword
(textboxComputer[i] as TextBox).Location = p;
(textboxComputer[i] as TextBox).Size = new System.Drawing.Size(183, 20);
//use 'as' again here to add the control to the controls Collection
this.Controls.Add(textboxComputer[i] as TextBox);
//instantiate and configure the labels
this.labels.Add(new Label());
System.Drawing.Point pLabel = new System.Drawing.Point(100, 114 + i * 25);
(labels[i] as Label).Location = pLabel;
(labels[i] as Label).Size = new System.Drawing.Size(80, 13);
(labels[i] as Label).Text = #"Computer " + (i + 1).ToString() + ":";
this.Controls.Add((labels[i] as Label));
//add some mouse events
(textboxComputer[i] as TextBox).MouseEnter += new System.EventHandler(this.textBox_mouseEnter);
(textboxComputer[i] as TextBox).MouseLeave += new System.EventHandler(this.textBox_mouseLeave);
//add the radio buttons - these are already sized (See RadioButtons.cs) so just need to place at a point
radioButtons.Add(new RadioButtons());
(radioButtons[i] as RadioButtons).Location = new System.Drawing.Point(370, 110 + i * 25);
this.Controls.Add(radioButtons[i] as RadioButtons);
int last = length - 1;
}
if (created == true)
{
this.Controls.Remove(c as Button);
//(c as Button).Location = new System.Drawing.Point(370, 110 + i * 25 + 25);
created = false;
}
created = true;
this.Controls.Add(c as Button);
(c as Button).Location = new System.Drawing.Point(370, 110 + length * 25 + 25);
I have an application that requires a set of questions that may range from 1 to 1000.
The questions are set up by the user and I need the groupbox to contain the
2 radio buttons indicated below.
The code does create multiple groupboxes containing 2 radio buttons.
This code is in a loop that is determined by how many questions are needed.
The issue is that when a radio button is clicked in any of the groupboxes,
that it removes the click from which ever groupbox was previously clicked.
How do I resolve this?
GroupBox grpAnswerType = new GroupBox(); // new groupbox
if (intZ < 9)
{
grpAnswerType.Name = "grpAnswerType00" + strQNumber;
}
if (intZ >= 10 & intZ <= 99) // intZ is the counter in the loop
{
grpAnswerType.Name = "grpAnswerType0" + strQNumber; // name is used later
}
if (intZ >= 100 & intZ <= 999)
{
grpAnswerType.Name = "grpAnswerType" + strQNumber;
}
grpAnswerType.Location = new Point(290, intR + 20);
grpAnswerType.Size = new Size(150, 45);
grpAnswerType.ForeColor = System.Drawing.Color.Red;
grpAnswerType.BackColor = SystemColors.Control;
grpAnswerType.Font = font;
grpAnswerType.Text = "Choose answer type ";
this.Controls.Add(grpAnswerType);
grpAnswerType.Show();
clsGlobals.gGroupBoxRadioButton3[intZ] = grpAnswerType; // add to array for later storage to database
pnlQ11.Controls.Add(grpAnswerType); // add to the dynamic panel on the form
RadioButton rbtnA1 = new RadioButton(); // Radio Button1
if (intZ < 9)
{
rbtnA1.Name = "rbtnA100" + strQNumber;
}
if (intZ >= 10 & intZ <= 99)
{
rbtnA1.Name = "rbtnA10" + strQNumber;
}
if (intZ >= 100 & intZ <= 999)
{
rbtnA1.Name = "rbtnA1" + strQNumber;
}
rbtnA1.Location = new Point(295, intR + 38);
rbtnA1.Size = new Size(60, 25);
rbtnA1.Text = "One";
rbtnA1.Font = font;
rbtnA1.ForeColor = System.Drawing.Color.Blue;
rbtnA1.BackColor = SystemColors.Control;
grpAnswerType.Controls.Add(rbtnA1);
pnlQ11.Controls.Add(rbtnA1); // if this is not commented, it appears on the panel, if not it does not
rbtnA1.Show();
clsGlobals.gRadioButtonOne[intZ] = rbtnA1;
rbtnA1.BringToFront();
RadioButton rbtnA2 = new RadioButton(); // Radio Button 2
if (intZ < 9)
{
rbtnA2.Name = "rbtnA200" + strQNumber;
}
if (intZ >= 10 & intZ <= 99)
{
rbtnA2.Name = "rbtnA20" + strQNumber;
}
if (intZ >= 100 & intZ <= 999)
{
rbtnA2.Name = "rbtnA2" + strQNumber;
}
rbtnA2.Location = new Point(355, intR + 38);
rbtnA2.Size = new Size(70, 25);
rbtnA2.Text = "All"; ;
rbtnA2.Font = font;
rbtnA2.ForeColor = System.Drawing.Color.Blue;
rbtnA2.BackColor = SystemColors.Control;
grpAnswerType.Controls.Add(rbtnA2);
pnlQ11.Controls.Add(rbtnA2); // if this is not commented, it appears on the panel, if not it does not
rbtnA2.Show();
clsGlobals.gRadioButtonAll[intZ] = rbtnA2;
rbtnA2.BringToFront();
I think it is because pnlQ11 is another UserControl (I am guessing you're using Windows Forms) and, acording to MSDN, a control can only be assigned to one Control.ControlCollection (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.add%28v=vs.100%29.aspx).
So, your RadioButton is removed from the GroupBox and added to pnlQ11, which is the same control where all other RadioButtons belong. Then, the solution is to avoid adding the RadioButton to that other control and keep it only in the GroupBox