Display value on a form dynamically

R

Rick

I have a form that needs to display values from an array on the form; how do
I achieve the following?

If item count in array is < 20 then print

Val1 Val2
Val3 Val4
Val5 Val6
and so on

If items in array is > 20 then print

Val1 Val2 Val3
Val4 Val5 Val6
Val7 Val8 Val9
and so on

The following code works fine and prints values in two columns butI need to
modified it to print in three columns if nItemsCount > 20


{
for (i = 1; i <= nItemsCount; i++) {

if (i % 2) {
nX = nX * 25;
nY = nY - 15;
}

myVal.Name = "myVal" + i;
myVal.Text = sLabel;
myVal.Location = new Point(nX, nY + 15);
Controls.Add(myTextBoxLabel);
}
}
 
R

Random

I have a form that needs to display values from an array on the form; howdo
I achieve the following?

Something like this:

Point nextLocation = new Point();
Point offset = new Point(100, 25);
int itemCount = 11;
int columns = 2;

if (itemCount > 20)
{
columns = 3;
}

for (int i = 1; i <= itemCount; i++)
{
TextBox myVal = new TextBox();
myVal.Text = "#" + i.ToString();
myVal.Location = nextLocation;
Controls.Add(myVal);

nextLocation.X += offset.X;
if ((i % columns) == 0)
{
nextLocation.X -= offset.X * columns;
nextLocation.Y += offset.Y;
}
}
 
F

Family Tree Mike

I have a form that needs to display values from an array on the form; how do
I achieve the following?

If item count in array is< 20 then print

Val1 Val2
Val3 Val4
Val5 Val6
and so on

If items in array is> 20 then print

Val1 Val2 Val3
Val4 Val5 Val6
Val7 Val8 Val9
and so on

The following code works fine and prints values in two columns butI need to
modified it to print in three columns if nItemsCount> 20


{
for (i = 1; i<= nItemsCount; i++) {

if (i % 2) {
nX = nX * 25;
nY = nY - 15;
}

myVal.Name = "myVal" + i;
myVal.Text = sLabel;
myVal.Location = new Point(nX, nY + 15);
Controls.Add(myTextBoxLabel);
}
}

Er, um, why did you just post the same question with different code, in
the VB.Net and C# forums? I answered you on VB.net. By the way, I
don't believe that this code (C#) works either. You are increasing the
x at every other step. You describe increasing the y at every step.
 
P

Peter Duniho

Rick said:
I have a form that needs to display values from an array on the form; how do
I achieve the following?

If item count in array is < 20 then print

Val1 Val2
Val3 Val4
Val5 Val6
and so on

If items in array is > 20 then print

Val1 Val2 Val3
Val4 Val5 Val6
Val7 Val8 Val9
and so on

The following code works fine and prints values in two columns butI need to
modified it to print in three columns if nItemsCount > 20


{
for (i = 1; i <= nItemsCount; i++) {

if (i % 2) {
nX = nX * 25;
nY = nY - 15;
}

myVal.Name = "myVal" + i;
myVal.Text = sLabel;
myVal.Location = new Point(nX, nY + 15);
Controls.Add(myTextBoxLabel);
}
}

Your question seems a little inconsistent. The example output you give
organizes the data across by columns, then down by rows as each row is
filled. And the code itself doesn't look like it could do anything
close to what you want. But even if we make some assumptions and fix
the computational errors in your example code, the code looks more like
something intended to output the data down by row, then across by column
as each column is filled.

Personally, when dealing with a grid layout, I would prefer to organize
the code with more explicit attention to the row and column:

// input data, initialized as appropriate
object[] rgobjData = …;

// wherever you want the output to start
Point ptOrigin = new Point();

// per "Random"'s example
Point ptOffset = new Point(100, 25);

int ccol = rgobjData.Length > 20 ? 3 : 2;

for (int iobj = 0; iobj < rgobjData.Length; iobj++)
{
Point ptCur = ptOrigin;

ptCur.Offset(
(iobj % ccol) * ptOffset.X,
(iobj / ccol) * ptOffset.Y);

myVal.Name = "myVal" + iobj;
myVal.Text = rgobjData[iobj].ToString();
myVal.Location = ptCur;
Controls.Add(myTextBoxLabel);
}

I find that simply calculating x/y, row/col, whatever directly from the
index is more readable and maintainable than constantly adjusting the
individual coordinates back and forth according to the particular point
within the iteration.

All that said…

Depending on what you really want, you may find that it makes more sense
to use the ListBox control (which can do multi-column output), the
ListView control (which also does multi-column output in "list" mode,
and may provide additional control over the formatting), or the
FlowLayoutPanel control (which allows you to simply add new control
instances, such as TextBox instances, while automatically handling the
organization of the controls in a grid fashion).

Pete
 
P

Peter Duniho

Family said:
Er, um, why did you just post the same question with different code, in
the VB.Net and C# forums?

In his defense, had he posted VB.NET code here, I would have been asking
him why he's asking a VB.NET question in the C# newsgroup.

Sometimes you just can't win. :)

(Of course, the true answer is that if he doesn't care about the
specific language for his question, he shouldn't be posting in _either_
language-specific newsgroup. There's a fine general-purpose .NET
Framework newsgroup, and of course the question is only barely pertinent
to .NET anyway; it probably makes more sense in a forum dedicated to
broadly general programming questions. But of course, that's probably
splitting hairs too much).

Pete
 
R

Rick

Perfect!
Thanks for your help.

Random said:
Something like this:

Point nextLocation = new Point();
Point offset = new Point(100, 25);
int itemCount = 11;
int columns = 2;

if (itemCount > 20)
{
columns = 3;
}

for (int i = 1; i <= itemCount; i++)
{
TextBox myVal = new TextBox();
myVal.Text = "#" + i.ToString();
myVal.Location = nextLocation;
Controls.Add(myVal);

nextLocation.X += offset.X;
if ((i % columns) == 0)
{
nextLocation.X -= offset.X * columns;
nextLocation.Y += offset.Y;
}
}
.
 
F

Family Tree Mike

In his defense, had he posted VB.NET code here, I would have been asking
him why he's asking a VB.NET question in the C# newsgroup.

Sometimes you just can't win. :)

(Of course, the true answer is that if he doesn't care about the
specific language for his question, he shouldn't be posting in _either_
language-specific newsgroup. There's a fine general-purpose .NET
Framework newsgroup, and of course the question is only barely pertinent
to .NET anyway; it probably makes more sense in a forum dedicated to
broadly general programming questions. But of course, that's probably
splitting hairs too much).

Pete

I know, and agree... I just was frustrated because I saw the question
there (vb) first.
 
P

Peter Duniho

Family said:
I know, and agree... I just was frustrated because I saw the question
there (vb) first.

I hear you. And I agree, it would have been better to cross-post an
off-topic post than to multi-post the same question with the only change
being the specific language.

Lesser of two evils. Best is to avoid both, but if one can't do that,
pick the one that's the least bad. :)

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