thoughts on how to create a continuous form?

R

Rich P

I want to create a continous form in my C# (VS2008) application -- the
same as a continous form in MS Access. The purpose is purely for
exercise. Here is the scenario:

Say I have a table with 50 rows. I want to display the data from each
row in textboxes on a form. Ideally, I could have a simple form with
the desired textboxes and a prev/next button setup to display the data
in each row. But instead I want a continous form, and I am thinking
like having a panel for each row where each panel would contain the
desired textboxes for each row. And these panels would be contained
within a larger panel (the parent panel) which would have a vertical
(and horizontal) scroll bar so I can scroll up and down the form to view
each row (each panel).

A question follows, but Here is what I am currently thinking to achieve
my continuous form: suppose the underlying table contains 30 rows of
data. So in the form (call it formA) I dynamically create 30 panels
with the desired textboxes (I dynamically add textbox controls to each
panel and add each panel control to the Parent Panel) and populate
everything. Then, if I refresh the data and say now there are 50 rows
-- I dynamically re-Add 50 panels and so on. Say Panel1 is the parent
and I add controls to it (the panels containing the textboxes for each
row). My question is if this is the approach I want to take? If this
is not the ideal approach -- could someone suggest how I should do this?

Thanks

Rich
 
P

Peter Duniho

Rich said:
I want to create a continous form in my C# (VS2008) application -- the
same as a continous form in MS Access. The purpose is purely for
exercise. Here is the scenario:

Say I have a table with 50 rows. I want to display the data from each
row in textboxes on a form. [...]

[...] Then, if I refresh the data and say now there are 50 rows
-- I dynamically re-Add 50 panels and so on. Say Panel1 is the parent
and I add controls to it (the panels containing the textboxes for each
row). My question is if this is the approach I want to take? If this
is not the ideal approach -- could someone suggest how I should do this?

Why not just use the GridView class? It's specifically designed for
exactly this kind of use. You can even use binding to have it directly
connected to your database.

Barring that, the next-most-appropriate approach IMHO would be to use
the FlowLayoutPanel. It's basically a panel with cells, which you can
configure for a specific row- or column-major layout along with specific
width and/or height. You can then fill it with TextBox instances as
needed, and it will automatically grow to contain what you want.

You can, of course, do everything yourself. In that case, you probably
want a single UserControl you've designed for convenience that contains
the layout for a single row. Then you can add instances of that
UserControl dynamically as children of a Panel instance, setting the
location of each according to the row index for that UserControl instance.

But really, I would just use GridView.

Pete
 
R

Rich P

Thank you for this great advice. As for the Gridview - just checking to
make sure you don't mean DataGridView? Also, the goal is to display
each row in a Form like view with textboxes and labels and some
additional hidden controls that I would make visible if some criteria
was met (like some button controls). How can I display each row in this
format in a Gridview? Can I place a panel or a form or textboxes in the
grid view cell/row?

I also like the flowLayoutPanel idea. I had forgotten about that. This
one sounds feasable also, except you recommend the gridview. I am just
not sure how I would go about making the gridview display like a form
with textboxes and other controls type layout.

Rich
 
P

Peter Duniho

Rich said:
Thank you for this great advice. As for the Gridview - just checking to
make sure you don't mean DataGridView?

Sorry, yes. I think GridView is an ASP.NET thing. The similar names
trip me up. :)
Also, the goal is to display
each row in a Form like view with textboxes and labels and some
additional hidden controls that I would make visible if some criteria
was met (like some button controls). How can I display each row in this
format in a Gridview? Can I place a panel or a form or textboxes in the
grid view cell/row?

I also like the flowLayoutPanel idea. I had forgotten about that. This
one sounds feasable also, except you recommend the gridview. I am just
not sure how I would go about making the gridview display like a form
with textboxes and other controls type layout.

If you want a custom representation, FlowLayoutPanel is probably a
better approach. You could probably override the necessary drawing
parts of DataGridView to do something similar, but if you can get the
look you want with FlowLayoutPanel, it'll probably be much easier to
implement.

Note that you may find yourself having just one column in the
FlowLayoutPanel, with each row of the column having a single UserControl
instance in the cell. That would make it easier to generalize the
display of each row. Similar to the all-manual solution, except that
you take advantage of FlowLayoutPanel to handle the actual scrolling,
positioning, etc.

Pete
 
R

Rich P

Sounds like I should build a user control with my desired
textboxes/labels/buttons... and then plant that user control in the
flowlayout panel. I will give that a try. Your suggestions will help
me not waste as much time trying to re-invent the wheel by trying to
code everything myself.

As for the user control, I would add a form and the desired textboxes.
Then I need to populate this control. So I am thinking I should add
get:set type properties to the user control for acquiring the data to
display in the textboxes? Then when I plant the usercontrol in the
flowlayout panel I instantiate a copy of the usercontrol class in a loop
as I loop through the rows in the underlying table?

Here is my pseudocode of what I envision: I make a reference to my
usercontrol dll in the application. Then in the form which would
contain the flowlayoutpanel in the Form's load event I would do
something like this?

...form_load(...)
{
myUserCtrl userCtl;
For (int i=0; i<rows.Count, i++)
{
userCtl = new userCtl();
userCtl.txt1 = rows["fld1"].ToString();
userCtl.txt2 = rows["fld2"].ToString();
userCtl.txt3 = rows["fld3"].ToString();
}
...
}

How will the flowlayout panel know to add the next usercontrol? Would
this happen automatically as I instantiate a new userCtl?

Or I could keep it simpler and just add my textboxes directly to the
flowlayout panel, but again, how does the flowlayout panel know to make
the next panel?


Rich
 
P

Peter Duniho

Rich said:
Sounds like I should build a user control with my desired
textboxes/labels/buttons... and then plant that user control in the
flowlayout panel. I will give that a try. Your suggestions will help
me not waste as much time trying to re-invent the wheel by trying to
code everything myself.

As for the user control, I would add a form and the desired textboxes.
Then I need to populate this control. So I am thinking I should add
get:set type properties to the user control for acquiring the data to
display in the textboxes?

Well, that's how I'd do it, yes. Others may disagree, but IMHO it's
good to take that approach, to encapsulate the implementation details,
exposing only the specific data relevant to the client of the
UserControl sub-class.
[...]
How will the flowlayout panel know to add the next usercontrol? Would
this happen automatically as I instantiate a new userCtl?

No, you have to add it explicitly. But it's a simple method call to the
panel's Controls collection.
Or I could keep it simpler and just add my textboxes directly to the
flowlayout panel, but again, how does the flowlayout panel know to make
the next panel?

FlowLayoutPanel automatically adds rows as necessary to accommodate the
children controls added it. And yes, you could just add the TextBox
instances directly, where the FlowLayoutPanel has multiple columns
instead of just one to hold the UserControl sub-class. Either way
should work fine.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top