Hi ,
we have requirement that we need load controls like labels, text boxes,drop down,date time,user control etc at run-time based on our table record.actually are designing questionnaire fill form.questions/answere we get from tables . so its dynamic .
we find out GridBagLayoutPanel which is pretty fast at adding control. design time its fine. but we need load control at run time actually.
what problem i facing ?
I've facing problem when i need add new control in middle of some control.
let say i have drowpdown control with value yes/no . user click on yes.we need add another new control just below dropdown.
let say dropdown control GridBagConstraint OriginX=0 and OriginY=3 .
below dropdown there will be already some control with origin x=0 , originy=4 and so on . if i need add control in originx=0 and originY=4. then how other controls GridBagConstraint origin values will be change accordingly??
e.g we have 4 columns record in table . column1 , column2 , column3 , column4lblQ1(x0,y0) , textbox (x1,y0) , lblQ2 (x2,y0) , datetimepicker(x3,y0)lblQ3 (x0,y1) ,DropDown(x1,y1), lblQ4 (x2,y1) , Checkboxes (x3,y1)blQ5 (x0,y2) , textbox (x1,y2) , lblQ6 (x2,y2) , datetimepicker(x3,y2)lblQ7 (x0,y3) ,DropDown(x1,y3), lblQ8(x2,y3) , Checkboxes(x3,y3)
so let say when user will click on first dropdown of second row (DropDown(x1,y1)) on selected index change i want to add another user control below it.so that it would look like below now. need span column also here.
column1 , column2 , column3 , column4lblQ1(x0,y0) , textbox (x1,y0) , lblQ2 (x2,y0) , datetimepicker(x3,y0)lblQ3 (x0,y1) ,DropDown(x1,y1), lblQ4 (x2,y1) , Checkboxes (x3,y1)
|-----------------------------------------------------------------------------------------------------| this box is usercontrol or groupbox,with controls | (x0,y2) spanX4 |____________________________________________________________|
blQ5 (x0,y3) , textbox (x1,y3) , lblQ6 (x2,y3) , datetimepicker(x3,y3)lblQ7 (x0,y4) ,DropDown(x1,y4), lblQ8(x2,y4) , Checkboxes(x3,y4)
as u can see next control originX,Y values change,moved to next postion.
how can i do that job at runtime now ? can u please help me ?
i'm attaching a sample in which i am just trying to add button in GridBagLayoutPanel on button click . then on third button on click event i trying to add conrol below it. but i i dont know how do.
can some body take a look at my sample and help me please ??
if GridBagLayoutPanel is not good to use for my requirement then can u please suggest me any other fast Infragistics controls which fit my requirement?
frmGridBagLayoutPanel.zip
Hello Tariq,
Thank you for contacting Infragistics!
When you add controls/rows in design time it reorders the remaining controls/rows automatically. When you add controls/rows at run time you are responsible for doing the reordering. After adding insert the new row you will have to loop through your remaining rows and adjust the Y positions.
Hello Mike,
Thanks you for quick response.
Actually that idea come in my mind too. But how to loop remaining rows?
Foreach(control c in gridbaglayoutpanel1. Control)
{
}
New inserted row will be in last of control array.
How can we change the required y position? We need keep in mind that second column top three rows y position will be same. Other rows position need change. Can u plz take a look the sample which I attached and fixed that issue. .and help for me?
Thank You.
Hello Mike ,
Thank you for reply me. Sorry for late response. i was on holidays . and ur line of code helped me.
Infragistics.Win.Layout.GridBagConstraint newAddedRowPosition =new Infragistics.Win.Layout.GridBagConstraint();;
foreach (Control control in this.ultraGridBagLayoutPanel1.Controls) { var gridBagConstraint = this.ultraGridBagLayoutPanel1.GetGridBagConstraint(control);
if (gridBagConstraint.OriginY >= newAddedRowPosition.OriginY) gridBagConstraint.OriginY += 1;
}this work perfectly .
Thank you Mike ,
It's not exactly clear to me from your example, exactly what you are trying to do here. Each time you click the button in this sample, you add a control and append it to the bottom, and then when you get to 5 rows, you start a new column. After 10 buttons, things will start to get weird, since you are not creating a third column at that point, but I guess this is just an example.
Anyway, if you take a very simple case where you add 4 items and you want to insert a new button at row 2, what you have to do is find all the the existing buttons that are at row two or higher and increment their OriginY by 1 to shift them down one row.
foreach (Control control in this.ultraGridBagLayoutPanel1.Controls) { var gridBagConstraint = this.ultraGridBagLayoutPanel1.GetGridBagConstraint(control); if (gridBagConstraint.OriginY >= column) gridBagConstraint.OriginY += 1; }
Of course, in your sample, if you had 5 items, then you would want the 5th item to loop around to the top of the next column. So you would need to check for that and set both the OriginX and Origin Y on that item. And if you are dealing with two columns and you want the items to sort've snake down - in other words, you want to insert an item in the first column and then have the items wrap, you would have to check for items that are in column 1 and shift those down even if the OriginY was less than 2.