Display value on a form dynamically

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); 
    } 
} 

0
Utf
2/24/2010 9:09:01 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

7 Replies
600 Views

Similar Articles

[PageSpeed] 8

On Feb 24, 1:09=A0pm, Rick <R...@discussions.microsoft.com> wrote:
> I have a form that needs to display values from an array on the form; how=
 do
> I achieve the following?

Something like this:

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

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

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

    nextLocation.X +=3D offset.X;
    if ((i % columns) =3D=3D 0)
    {
        nextLocation.X -=3D offset.X * columns;
        nextLocation.Y +=3D offset.Y;
    }
}
0
Random
2/24/2010 9:29:58 PM
On 2/24/2010 4:09 PM, Rick wrote:
> 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.

-- 
Mike
0
Family
2/24/2010 10:22:33 PM
Rick wrote:
> 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
0
Peter
2/24/2010 10:44:24 PM
Family Tree Mike wrote:
> 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
0
Peter
2/24/2010 10:47:20 PM
Perfect!
Thanks for your help.

"Random" wrote:

> On Feb 24, 1:09 pm, Rick <R...@discussions.microsoft.com> wrote:
> > I have a form that needs to display values from an array on the form; how do
> > 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;
>     }
> }
> .
> 
0
Utf
2/24/2010 10:56:01 PM
On 2/24/2010 5:47 PM, Peter Duniho wrote:
> Family Tree Mike wrote:
>> 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

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

-- 
Mike
0
Family
2/24/2010 10:59:39 PM
Family Tree Mike wrote:
> 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
0
Peter
2/24/2010 11:09:36 PM
Reply:

Similar Artilces:

Change Display Names in Exch '03 to Last,First
In Exchange 2000, you could use ADSI and scripts to change display names for contacts/users from firstname,lastname to lastname,firstname. However, I am working with a new client that has a 2003 Exchange server up and populated, but the GAL shows in firstname,lastname against his will. How can I change this so that the GAL is listed the way he wants without individually modifying each contact/user? -- Lydell Anderson A+, Net+, MCP, MCSA I don't see how it should be any different with Exchange 2003. Modifying the Display name will change how the GAL is displayed. You should be ...

Mail-merge dynamic database source
I have created a mail-merge that gets its input from Excel via OLEDB. Each month it needs to get the input from a spreadsheet in a folder specific to that month. Moreover, although I create and test the merge on my pc, I distribute it to others who have a different naming convention for their folders. Once I save my merge document, the source folder is saved with it. [Although I have examined the merge document carefully, and can't find any clear text reference to the database. Does anyone know how that info is stored?] I know that if the database doesn't exist when th...

Specific cell values
Hi everyone, i need to acheive the following but i'm not sure how to do it. i have a cell (K49), the cell needs to collect specific data from a range ( D7:D32 ) on the same worksheet. it needs to look in the range and if the cell value is CNC, then add the figure form the same row in column E. Example: D E F glass £200 26 August 05 metal £650 26 August 05 wood £300 26 August 05 CNC £1000 ...

Filter rows containing cells with values within a range (for example <11 or >10 but <21)
Column B consists of Brand names (each brand has numerous products) Column C consists of Product names (over 500 products) Coumns D to CG (row 1) consist of months going back a few years. The data from D2:CG539 consists of the rank of each brand for each month. I would like to filter my rows so that I can view only the products that have ever ranked in the top 10. I already have a couple of formulas that allow me to filter my data. Example 1: If I want to know the highest rank ever achieved by a product I use this formula: =MIN(D2:CG2) and then drag it down. Now I can use autofilter and see w...

Pivot Tables
I have a new computer and previously I was able to use Pivot tables as a shortcut to highlight a list of say 500 items and display the unique items in a pivot table. However when I try to do that now it doesn't display the items, it only show the dropdown with the unique items checked or unchecked and therefore I cannot highlight the unique items to create my unique items list. Does anyone know if there is an option that is preventing me from doing this or any other way to create a unique items list from a larger list. Thank you very much. If you create a pivot table with a fi...

Unbound option groups and form's RecordSource in Access 2007
I have a form that contains unbound option groups and an unbound combo box in the form header section. The form itself is bound to a query, but in the Load event I set the form's RecordSource to something like: SELECT * FROM [query] WHERE FALSE; This has the effect of hiding all of the data fields on the form, leaving only the option groups and combo box showing. The option groups are used to limit the items that show in the combo box, and the combo box is used to select an item to view on the form. When a selection is made in the combo box, the form's RecordSource is modified ...

dynamic sort macro across 3 linked sheets
Good evening -B3 thru M3 in sheets Row 3 in "Sheet1, Sheet1 (2), Sheet1 (3 contains symbols which are dynamic i.e. alphabetic and change in an one sheet or all sheets daily so that the alphabetic order is los almost daily but which order is critical to maintain order and sens when viewed. -what macro be devised to simply sort these changes across the thre linked sheets and keep good order- an array or 3-d referrence? - I giv up and so this request for help thanks -wrpalme -- wrpalme ----------------------------------------------------------------------- wrpalmer's Profile: http://...

Copy date from a form to a different table
Good Morning to all. I have a database that i use. i have a table "stock" and a table "materials subform" The form i use to write the data to the subform extracts its data from the "stock" table. I do a simple cculation and then i need to write 1 field back to the "stock" table. The names used in the "stock" table are "partnumber" "quantityinstock" "costprice" and "sellprice". In the subform the fields are "partno" "tempqty" "buyprice" and "myprice" . What i wan...

how can I disable TouchPad scrolling is a access form with VB code
...

Default Value for new Table Row
Is there a way to specify a "default" value for a field when a new row is created in a table? I have a simple table with the date as the first column. When I add a new entry to the table I would like the date to default to the current day. I tried browsing the Excel objects but could not find any objects or events related to tables. I could develop a form, but that would be a lot of work just to set a simple default. It seems seems like there should be an "on_new_table_row" or "on_table_row_add" event where you could set a default with just a line or two o...

IF statement that yields a value based on a formula
Help! If cell B12 >= 0, then I need the answer to be calculated based on the formual "B12 * .40". If cell B12 < 0, then the answer is zero. How do I do this? -- barbara h IF(B12>=0,B12*0.4,IF(B12,0,0)) Click yes if helped -- Greatly appreciated Eva "barbara h" wrote: > Help! If cell B12 >= 0, then I need the answer to be calculated based on the > formual "B12 * .40". If cell B12 < 0, then the answer is zero. > > How do I do this? > -- > barbara h Hi, Try this =MAX(0,B12*0.4) -- Mike ...

replacing values in cells from a pre-designed table
I am working with an Excel Spreadsheet. I have, in column C, a different value on each row: 1.2 or 0.7 or 2.5, etc. In column D I want to have Excel automatically place, in each row, a new value based on a reference table I would create. For example If column C value is Then column D value on same row will b 0.7 9 0.8 9 0.9 9 1.0 9 ...

How do I get an automatic reminder with a certain cell value? #2
Is there a way to receive a pop-up comment or text box when a certain thing is typed into a cell? For example, each time I type "new instrument", I want a reminder to pop-up like a comment would that says "Call Property Accounting". Is there a function like this in Excel? Assuming data entry cells are col. A, right-click the worksheet tab, choose "View Code", copy in the code below, and press ALT+Q to close VBE. Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim sMsg As String With Target If .Column > 1 Then Exit Sub If .Count > 1 Then Exit...

Display records one time only
Primary Key is ID and data is extracted by ClubID. Fields are ID, ClubID, StpMail, FNm, LNm, MI, AD1, CO, Cty, ST, ZP. Prompt asks for up to 5 ClubID's, but the resulting Dataset has duplicates (one ID may have been a member of all ClubID's) ClubID is a year value (i.e., 2007). How do I restrain the data so I don't end-up with Duplicate values? Thanks in advance for any suggestions. -- FirstVette52 No duplicate ID's regardless of the ClubID. -- FirstVette52 "FirstVette52" wrote: > Primary Key is ID and data is extracted by ClubID. Fi...

Setting a cell to not go below an X value, or above a Y value
Hey, This may be very easy to do, but I just don't have a clue, I've look a most excel variables and they just seem to choose MIN and MAX fro columns or tables. What I want to do is I've got the sum adding up say cells A2 and A3 i cell A4. I want cell A4 to never go below 80, or above 200. (Formula in cell A would be =SUM(A2:A3)) Anyone think they can help -- Snake ----------------------------------------------------------------------- Snakey's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=2785 View this thread: http://www.excelforum.com/showthre...

Display currency as calculated
Gyus I am just about finished but I can't get Excel to display my currency as the calculated result. I need to display this accurately so I can take the figures to my accountant. My issue is that when I calculate the GST on an amount, the calc is fine but is displayed in correctly eg $22.63 / 11 = 2.05727272727273, so I need it to display it as $2.05 not as it does - $2.06 (I am sure the taxman would like this printout). I looked at the TRUNC command but can't get it to work. Is there a way to do this in code to a column of numbers where every row has been summed(horiz...

Smartlist restriction dynamics 9 issue
If I add the Qty Shipped field to Purchase Order Lines, there are many zeros. If I add a restriction Qty Shipped=0, there are only 3 records found. If enter a restriction Qty Shipped < Qty Ordered, no zeros appear in there either. What is wrong with Smartlist? LuvSQL, Not sure what's wrong with SmartList, but I was able to replicate the issue. I am on v10 SP1. -- MG.- Mariano Gomez, MIS, MCP, PMP Maximum Global Business, LLC http://www.maximumglobalbusiness.com "Luvsql" wrote: > If I add the Qty Shipped field to Purchase Order Lines, there are many zeros. &g...

Icon on WindowList menu using MDIChild Form icon.
hi, I would like to know is there any way on how to put icon on WindowList type menu using MDIChild's own form icon in VB6? Regards, Alex ...

Find cell which returned maximum value in formula
-- De De, try this, =CELL("address",INDEX(A:A,MATCH(MAX(A:A),A:A,0))) will show the first one if there are more than one -- Paul B Always backup your data before trying something new Please post any response to the newsgroups so others can benefit from it Feedback on answers is always appreciated! Using Excel 2002 & 2003 "De" <De@discussions.microsoft.com> wrote in message news:A17DFFDA-BE05-463C-B68D-65FE235E6098@microsoft.com... > > -- > De Thanks, Paul, but I found this hard to follow. Excel kept returning that I had disabled macros but yo...

Getting value from a picklist
I'm trying to use the value in a select picklist (based on the Contacts table) in some SQL to get further data from the CRM database. I'm looking at something along the lines of (AIR CODE!): var oDataValue = crmForm.all.new_enduserid.DataValue; var oSQL = "SELECT * FROM whateverTable WHERE TableID = " + oDataValue; However, it doesn't work - I'm unable to get the value of the selected item in the picklist. This line of code: alert(oDataValue); returns: [object Object] while attempting to get SelectedText returns "undefined". What am I doing wrong? ...

access compare values and select higher of two
In Access database I want to compare the values in two fields in a form and then select the higher value, insert it into another field and then use in a formula. E.g. Value 1 = 500 Value 2 = 600, 600 to be inserted into another field and then be multiplied. On 11 apr, 21:26, Captain Turtle <Captain Tur...@discussions.microsoft.com> wrote: > In Access database I want to compare the values in two fields in a form a= nd > then select the higher value, insert it into another field and then use i= n a > formula. > > E.g. Value 1 =3D 500 =A0 Value 2 =3D 60...

How do i have a check display with any data entry in a cell
I am creating a simple spreadsheet for teachers and want to have only a check mark displayed regardless of what they type into a cell. Insert the check mark symbol you want from the Insert-Symbol menus int a reference cell for your formula. Then your formula in B2 would be. =IF(A2>0,$H$2,"") A2 is where the data is entered by the teachers, $H$2 is where yo inserted the check mark symbol for your reference. You can then dra this down for each row of data. Cheers, Stev -- Steve ----------------------------------------------------------------------- SteveG's Profile: ...

InfoPath Forms Services not see RemoteApp file associations
I have a MOSS2007/InfoPath Forms Services with a Form Library. The form library is configured Opening browser-enabled documents as "Open in the client application". Works as expected when InfoPath 2007 is installed on the local client. When a RemoteApp (Server 2008 R2) version of InfoPath 2007 is configured on a client, I get an error indicatiing the client app must be installed or a web form must be enabled. The RemoteApp (Server 2008 R2) version of InfoPath 2007 works fine on the client when opening a local .xsn file, or when access file via the network (UNC ...

Add CompanyName to Opportunities Form
The opportunity form does not allow me to add the CompanyName of the lead. Neither does the Contact Form. This seems a little restrictive. Any advise on a workaround would be greatly appreciated. Thanks - Mitch use deployment manager to create a mapping from lead to opportunity to bring over the company name -- John O'Donnell Microsoft CRM MVP http://www.mscrmfaq.us "Mitch" <Mitch@discussions.microsoft.com> wrote in message news:7423C9B9-2476-4296-8314-EEF7928D4ED2@microsoft.com... > The opportunity form does not allow me to add the CompanyName of the lead. >...

Run Macro If Cell value = "Other"
Alright, I have a very simple macro at the moment that unhides a column and adjusts the print area to suit. What I need is a maco that will run this macro if the cell F4 = "Other" from a drop down menu. Seems painfully simple I just can't find where it is. Cheers If Range("F4").Value = "Other" Then Call yourMacro End If -- HTH Bob Phillips (replace xxxx in the email address with gmail if mailing direct) <bobbly_bob@hotmail.com> wrote in message news:1163993734.539069.261810@j44g2000cwa.googlegroups.com... > Alright, I have a ...