Form Layout at Runtime ?
-
Is it possible to control the layout of controls on a form at runtime. Actually I have a xml file which will contain some questions & the type of answer it expects. I need to design an interface with these questions on different labels & appropriate controls for accepting the answer to these questions (it might check box, radio button or textbox...) Thanks in Advance, Cheers
-
Is it possible to control the layout of controls on a form at runtime. Actually I have a xml file which will contain some questions & the type of answer it expects. I need to design an interface with these questions on different labels & appropriate controls for accepting the answer to these questions (it might check box, radio button or textbox...) Thanks in Advance, Cheers
jamesjk wrote:
Is it possible to control the layout of controls on a form at runtime.
Yes, just write code that adjust the particular properties of the controls that you want to change. e.g. If you want to change the text of a label. just do myLabel.Text = "NewText"; If you want to add new controls, you can create them just as you would with any other object, and then you just need to add them to the forms .Controls collection.
Label myNewLabel = new Label(); myNewLabel.Text = "NewText"; this.Controls.Add(myNewLabel); // Add the new control to the Controls collection so it's displayed on the form. myNewLabel.Left = 50; myNewLabel.Top = 50; myNewLabel.Visible = true; myNewLabel.BackColor = Color.Red;
Simon
-
jamesjk wrote:
Is it possible to control the layout of controls on a form at runtime.
Yes, just write code that adjust the particular properties of the controls that you want to change. e.g. If you want to change the text of a label. just do myLabel.Text = "NewText"; If you want to add new controls, you can create them just as you would with any other object, and then you just need to add them to the forms .Controls collection.
Label myNewLabel = new Label(); myNewLabel.Text = "NewText"; this.Controls.Add(myNewLabel); // Add the new control to the Controls collection so it's displayed on the form. myNewLabel.Left = 50; myNewLabel.Top = 50; myNewLabel.Visible = true; myNewLabel.BackColor = Color.Red;
Simon
Thanks Simon. The main problem I'm facing is that the xml will be variable. I'm providing a xml editor to the user so that he can add as many questions in it. With uncertain number of questions do you have any suggestions on how i'll be able to manage the layout ? Thanks Again for the reply.
-
Thanks Simon. The main problem I'm facing is that the xml will be variable. I'm providing a xml editor to the user so that he can add as many questions in it. With uncertain number of questions do you have any suggestions on how i'll be able to manage the layout ? Thanks Again for the reply.
You could have 1 page per question, just do your layout for a page to fit 1 question on it, then have controls to move to the next question, which would have exactly the same layout just with the question text and answers changed. (it wouldn't actually need to be a differnt page or form or anything, just change the text on the form to match the next question) Or, you could use something like the FlowLayoutPanel, which would allow you to add controls to it and it rearranges them to flow in order. You'd probably create one Panel per question, using a predefined layout for the panel, with question and answer buttons, then added each of the question panels to 1 main FlowLayoutPanel which would take care of ordering the question panels for you.
Simon