Windows Develop Bookmark and Share   
 index > Windows Forms Designer > scrollable form disturbs controls location when added at run time
 

scrollable form disturbs controls location when added at run time

Hello all,
I havedeveloped an application in which one of my forms adds controls(Label,Button,Progressbar) at runtime;i.e on every click of a button, a Label,a Button & a Progressbar is added in a row shape.
I have set AutoScroll Property of the form to true and also fixedthe form's height & width. When the number of controls(being added at runtime)increase, form sets the scrollbar automatically and i can see allthe controls added no matter how many have been added,thanks to AutoScroll=True.
But those controls which are added after the scrollbar is activated, are locatedwith uneven space between them;i.e. if the scrollbar is activated after 5 contorls are adeed,then the controls being added after these 5 controls are placed with different space with respect to each other.Each row of controls(Lable,Button,Progressbar) is located at different Y location. Why is it happening? It only happens when scrollbar is activated(when form size issmall & more contorls are added).
Thanks in advance


maiqbal
Aamir Iqbal  Wednesday, September 09, 2009 10:07 AM
Hi,
You need to calculate the scroll bar y location and subtract it to your control y location.i hope it will help you
if you want to more post some code sample..
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 09, 2009 10:14 AM

thanks Gnanadurai for your quick reply.
You mean i should calculate the new Y location for my next runtime control and subtract it to previously added runtime control? Or you mean to get the Y location of the Form & subtract from my next runtime control?
Please make it clear,thanks.
My whole code to create the runtime controls is here:

//------------Start Code------------

public bool addNewControls(string senderOrReciver, string fromUser, string fileName, string ToUser)

{

try

{

FontFamily ff = new FontFamily("Verdana");

float size = float.Parse("8.25");

FontStyle objFS = FontStyle.Bold;

Font myFont = new Font(ff, size, objFS);

string dname;

string dnameCancel;

if (senderOrReciver.Trim().ToString() == "Sender :")

{

dname = fromUser + fileName;

dnameCancel = fromUser +

"=" + ToUser + "=" + fileName;

}

else

{

dname = ToUser + fileName;

dnameCancel =

"Rec" + fromUser + "=" + ToUser + "=" + fileName;

}

Label l = new Label();

l.Name =

"fileName" + "_" + dname;

l.Font = myFont;

//l.ForeColor = Color.White;

l.Text =

"File Name : " + fileName;

l.Location =

new Point(202, FileNameY + y);

l.Width = 200;

l.BackColor =

Color.Transparent;

this.Controls.Add(l);

Label l1 = new Label();

l1 =

new Label();

l1.Name =

"Sender" + "_" + dname;

if (senderOrReciver == "Sender : ")

{

l1.Text = senderOrReciver + fromUser;

}

else

{

l1.Text = senderOrReciver + ToUser;

}

//l1.ForeColor = Color.White;

//l1.Text = "Sender : " + fromUser;

l1.Location =

new Point(28, senderNameY + y);

l1.Font = myFont;

//l1.BackColor = Color.Transparent;

l1.Width = 200;

this.Controls.Add(l1);

Label l2 = new Label();

l2 =

new Label();

l2.Name =

"display" + "_" + dname;

l2.Text =

"";

l2.Location =

new Point(27, displayAndButton + y);

l2.Font = myFont;

//l2.BackColor = Color.Transparent;

l2.Width = 200;

this.Controls.Add(l2);

ProgressBar p = new ProgressBar();

p.Name =

"progressBar" + "_" + dname;

p.Location =

new Point(30, progressBarY + y);

p.Height = 15;

p.Width = 463;

p.Maximum = 100;

//p.BackColor = Color.Transparent;

this.Controls.Add(p);

Button b = new Button();

b.Name =

"cancel" + "_" + dnameCancel;

b.Location =

new Point(385, displayAndButton + y);

b.Text =

"Cancel";

//b.BackColor = Color.Transparent;

//b.Click += new EventHandler(picBtnCancel_Click);

b.Click +=

new EventHandler(b_Click);

this.Controls.Add(b);

//fileID = fileID + 1;

//Add File Open Button

Button ob = new Button();

ob.Name =

"Open" + "_" + dname;

ob.Location =

new Point(200, displayAndButton + y);

ob.Text =

"Open File";

//ob.BackColor = Color.Transparent;

ob.Visible =

false;

ob.Click +=

new EventHandler(ob_Click);

this.Controls.Add(ob);

//End File Open Button

y = y + 90;

return true;

}

catch (Exception exc)

{

return false;

}

// }

}



//---End Code---

Waiting for you kind reply,
Thanks


maiqbal
Aamir Iqbal  Wednesday, September 09, 2009 10:54 AM
Hi,
Post your code using insert code block located near by html icon in the toolbar.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 09, 2009 12:06 PM
Hi,
I cant not able to workout your code .but from your code i understand everytime you creating a set of controls.instead of doing like that create a user control.whenever you want you add it at run time.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 09, 2009 12:09 PM

Thanks Gnanadurai for your continuous replies.
Yes, everytimei am creating a set of controls at runtime. You mean the code is not workable for you or you could not understand it well.
Here is the code snippet:

public bool addNewControls(string senderOrReciver, string fromUser, string fileName, string ToUser)
        {
            try
            {
                FontFamily ff = new FontFamily("Verdana");
                float size = float.Parse("8.25");
                FontStyle objFS = FontStyle.Bold;
                Font myFont = new Font(ff, size, objFS);
                string dname;
                string dnameCancel;              
                
                
                
                if (senderOrReciver.Trim().ToString() == "Sender :")
                {
                    dname = fromUser + fileName;
                    dnameCancel = fromUser + "=" + ToUser + "=" + fileName;
                }
                else
                {
                    dname = ToUser + fileName;
                    dnameCancel = "Rec" + fromUser + "=" + ToUser +  "=" + fileName;
                }

                Label l = new Label();
                l.Name = "fileName" + "_" + dname;
                l.Font = myFont;
                //l.ForeColor = Color.White;
                l.Text = "File Name : " + fileName;
                l.Location = new Point(202, FileNameY + y);

                //l.Width = 200;
                l.BackColor = Color.Transparent;

                this.Controls.Add(l);
                
                Label l1 = new Label();
                l1 = new Label();
                l1.Name = "Sender" + "_" + dname;
                if (senderOrReciver == "Sender :")
                {
                    l1.Text = senderOrReciver + fromUser;

                }
                else
                {

                    l1.Text = senderOrReciver + ToUser;

                }
                //l1.ForeColor = Color.White;
                //l1.Text = "Sender : " + fromUser;
                l1.Location = new Point(28, senderNameY + y);
                l1.Font = myFont;
                //l1.BackColor = Color.Transparent;
                l1.Width = 200;
                this.Controls.Add(l1);
                

                Label l2 = new Label();
                l2 = new Label();
                l2.Name = "display" + "_" + dname;

                l2.Text = "";
                l2.Location = new Point(27, displayAndButton + y);
                l2.Font = myFont;
                //l2.BackColor = Color.Transparent;
                l2.Width = 200;
                this.Controls.Add(l2);


                ProgressBar p = new ProgressBar();

                p.Name = "progressBar" + "_" + dname;
                p.Location = new Point(30, progressBarY + y);
                p.Height = 15;
                p.Width = 463;
                p.Maximum = 100;


                //p.BackColor = Color.Transparent;
                this.Controls.Add(p);
                

                Button b = new Button();
                b.Name = "cancel" + "_" + dnameCancel;
                b.Location = new Point(385, displayAndButton + y);
                b.Text = "Cancel";
                
                //b.BackColor = Color.Transparent;
                //b.Click += new EventHandler(picBtnCancel_Click);
                b.Click += new EventHandler(b_Click);
                this.Controls.Add(b);
                
                //fileID = fileID + 1;


                //Add File Open Button
                Button ob = new Button();
                ob.Name = "Open" + "_" + dname;
                ob.Location = new Point(200, displayAndButton + y);
                ob.Text = "Open File";
                //ob.BackColor = Color.Transparent;
                ob.Visible = false;
                ob.Click += new EventHandler(ob_Click);
                this.Controls.Add(ob);
                
                //End File Open Button
                y = y + 90;
                
                
                            return true;
            }
            catch (Exception exc)
            {
                return false;
            }
            //    }
        }


maiqbal
Aamir Iqbal  Thursday, September 10, 2009 3:45 AM
Hi,
i just tried out your code its working fine
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
addNewControls("ABC", "ABC", "ABC", "ABC");
}
}
int y = 0;
int FileNameY = 10;
int senderNameY = 10;
int displayAndButton = 30;
int progressBarY = 10;
public bool addNewControls(string senderOrReciver, string fromUser, string fileName, string ToUser)
{
try
{
FontFamily ff = new FontFamily("Verdana");
float size = float.Parse("8.25");
FontStyle objFS = FontStyle.Bold;
Font myFont = new Font(ff, size, objFS);
string dname;
string dnameCancel;



if (senderOrReciver.Trim().ToString() == "Sender :")
{
dname = fromUser + fileName;
dnameCancel = fromUser + "=" + ToUser + "=" + fileName;
}
else
{
dname = ToUser + fileName;
dnameCancel = "Rec" + fromUser + "=" + ToUser + "=" + fileName;
}

Label l = new Label();
l.Name = "fileName" + "_" + dname;
l.Font = myFont;
//l.ForeColor = Color.White;
l.Text = "File Name : " + fileName;
l.Location = new Point(202, FileNameY + y);

//l.Width = 200;
l.BackColor = Color.Transparent;

this.Controls.Add(l);

Label l1 = new Label();
l1 = new Label();
l1.Name = "Sender" + "_" + dname;
if (senderOrReciver == "Sender :")
{
l1.Text = senderOrReciver + fromUser;

}
else
{

l1.Text = senderOrReciver + ToUser;

}
//l1.ForeColor = Color.White;
//l1.Text = "Sender : " + fromUser;
l1.Location = new Point(28, senderNameY + y);
l1.Font = myFont;
//l1.BackColor = Color.Transparent;
l1.Width = 200;
this.Controls.Add(l1);


Label l2 = new Label();
l2 = new Label();
l2.Name = "display" + "_" + dname;

l2.Text = "";
l2.Location = new Point(27, displayAndButton + y);
l2.Font = myFont;
//l2.BackColor = Color.Transparent;
l2.Width = 200;
this.Controls.Add(l2);


ProgressBar p = new ProgressBar();

p.Name = "progressBar" + "_" + dname;
p.Location = new Point(30, progressBarY + y);
p.Height = 15;
p.Width = 463;
p.Maximum = 100;


//p.BackColor = Color.Transparent;
this.Controls.Add(p);


Button b = new Button();
b.Name = "cancel" + "_" + dnameCancel;
b.Location = new Point(385, displayAndButton + y);
b.Text = "Cancel";

//b.BackColor = Color.Transparent;
//b.Click += new EventHandler(picBtnCancel_Click);
// b.Click += new EventHandler(b_Click);
this.Controls.Add(b);

//fileID = fileID + 1;


//Add File Open Button
Button ob = new Button();
ob.Name = "Open" + "_" + dname;
ob.Location = new Point(200, displayAndButton + y);
ob.Text = "Open File";
//ob.BackColor = Color.Transparent;
ob.Visible = false;
//ob.Click += new EventHandler(ob_Click);
this.Controls.Add(ob);

//End File Open Button
y = y + 90;


return true;
}
catch (Exception exc)
{
return false;
}
//    }
}


i am suggest you to create a user control.
i thing that one fit for your requirement.
have a look at this link
http://msdn.microsoft.com/en-us/library/a6h7e207(VS.71).aspx
i hope it will help you.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Proposed As Answer byGnanadurai Monday, September 14, 2009 5:04 AM
  •  
Gnanadurai  Thursday, September 10, 2009 5:44 AM
hello Gnanadurai.
I used your idea of putting all the controls i am creating at runtime in a user control and added that user control on the form but it did not solve my problem.
Actually, the layout of the form with scroll bargets disturbed when controls are added one by one. But when i add controls using FOR loop (1 to 50), it works fine.
But my scenario is to add controls one by one by showing a confirmation window to the user and then user clicks on a button to add the runtime controls.
Do you have any other idea of how to get rid of this problem?
Waiting for your kind reply,
thanks
maiqbal
Aamir Iqbal  Wednesday, September 16, 2009 10:06 AM

You can use google to search for other answers

Custom Search

More Threads

• DuplicateItemsNotSupported
• Integrating Outlook for sending email
• An idea for Visual Studio....
• keeping the designer from generating code for a public property
• Add a Windows Form file into a project
• Weird artifact in Designer
• Notifiers (bottom left corner popups)
• How to add event to web browser control
• Cannot cast MyControl to MyControl in designer
• scroll bar issue in MDI application