line break windows application c#

eagle_ea
eagle_ea used Ask the Experts™
on
hello experts
is there a way to add new line or line breaks in windows application using c#code.
i'm trying to populate checkbos using data from DB.
i need each checkbox to be created one in each line.
currently, it is creating all in one line spot.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Please post the code that populates the checkboxes.

Author

Commented:
while (reader.Read() == true)
                {


                    CheckBox c = new CheckBox();
                    c.Text = reader.GetString(0);
                    Controls.Add(c);

                }
Top Expert 2011

Commented:
Please try the following:

var panel = new FlowLayoutPanel();

panel.AutoSize = true;
panel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
panel.FlowDirection = FlowDirection.TopDown;

while (reader.Read() == true)
{
    CheckBox c = new CheckBox();
    c.Text = reader.GetString(0);
    panel.Controls.Add(c);
}

Controls.Add(panel);

Open in new window


I hope this helps.

Author

Commented:
i changed
var panel = new FlowLayoutPanel();
to
 Panel p = new FlowLayoutPanel();

getting error in the below line,
panel.FlowDirection = FlowDirection.TopDown;
'System.Windows.Forms.Panel' does not contain a definition for 'FlowDirection'      

HainKurtSr. System Analyst

Commented:
add this

Controls.Add(new Literal("<br>"));

to your loop
HainKurtSr. System Analyst

Commented:
oops it should be

Controls.Add(New LiteralControl("<br>"));
HainKurtSr. System Analyst

Commented:
no need to complicate the htnl with panels :) just a simple <br> should work fine...
Top Expert 2011

Commented:
var should have worked fine.  Try the following to fix the FlowDirection definition error.

FlowLayoutPanel p = new FlowLayoutPanel();

p.AutoSize = true;
p.AutoSizeMode = AutoSizeMode.GrowAndShrink;
p.FlowDirection = FlowDirection.TopDown;

while (reader.Read() == true)
{
    CheckBox c = new CheckBox();
    c.Text = reader.GetString(0);
    p.Controls.Add(c);
}

Controls.Add(p);

Open in new window

Author

Commented:
Controls.Add(New LiteralControl("<br>"));
doesn't work well. is there something else to it?

Author

Commented:
wdosanjos, i tried that too...
System.Windows.Forms.Panel' does not contain a definition for 'FlowDirection'      
Top Expert 2011

Commented:
@eagle_ea: Please post your code.  Your variable should be defined as FlowLayoutPanel not Panel.

@HainKurt: LiteralControl is only available on Web apps not WinForms.
Top Expert 2011

Commented:
What's your .NET Framework version?

Author

Commented:
currently, the checkboxes are created horizontally
Panel p = new FlowLayoutPanel();

                p.AutoSize = true;
                p.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                //p.AutoScrollPosition = AutoScrollPosition.Y.ToString();
                //p.FlowDirection = FlowDirection.TopDown;

                while (reader.Read() == true)
                {
                    CheckBox c = new CheckBox();
                    c.Text = reader.GetString(0);
                    p.Controls.Add(c);

                    
                }
                Controls.Add(p);

Open in new window

Author

Commented:
v2.0.50727 - .net framework version
Top Expert 2011
Commented:
Just change line#1 as follows and uncomment line #6.  p must be of type FlowLayoutPanel not Panel.

FlowLayoutPanel p = new FlowLayoutPanel();
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Without the use of some kind of layout panel as wdosanjos suggests, then you'd need to manually place each dynamic control via the Location() property:

                    CheckBox c = new CheckBox();
                    c.Text = reader.GetString(0);
                    c.Location = new Point(someCalculatedX, someCalculatedY);
                    Controls.Add(c);

Author

Commented:
that did the trick wdosanjos.

thanks for all you help

Author

Commented:
hmmmmmmmmmmm..........can I bother you all with one more help?

now that we have created all these checkboxes. how can i read the ones that has been selected?
HainKurtSr. System Analyst

Commented:
you should loop all controls, check the type, if it is checkbox, then cast it to checkbox
then use the selected property & Text values...

Author

Commented:
how to loop all controls in the form?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial