Carriage return

  • Thread starter Thread starter Jlaz
  • Start date Start date
J

Jlaz

I am new to C# and I am writing a small Windows app that consists of a Label
and Button. I've written code that displays the following in the Label:

---- ----
2004 $1,200.00
2005 $1,260.00
2006 $1,323.00
2007 $1,389.15
2008 $1,458.61
2009 $1,531.54
2010 $1,608.12
2011 $1,688.53
2012 $1,772.96
2013 $1,861.61
2014 $1,954.69

I need it to display the heading as well without having to create a second
label to produce the "Year Rent":

Year Rent
---- ----
2004 $1,200.00
2005 $1,260.00
2006 $1,323.00
2007 $1,389.15
2008 $1,458.61
2009 $1,531.54
2010 $1,608.12
2011 $1,688.53
2012 $1,772.96
2013 $1,861.61
2014 $1,954.69


My Code is the following:
private void btnExecute_Click(object sender, System.EventArgs e)

{

int iYear = 2004, iFinalYear = 2014;

decimal mRent = 1200, mIncrease = 5.0m;


label1.Text = "Year Rent\r\n";

label1.Text = "---- ----\n";

while (iYear <= iFinalYear)

{

label1.Text += iYear + " " + mRent.ToString( "C") + "\n";

mRent *= (1 + mIncrease / 100);

mRent = Decimal.Round(mRent, 2);

iYear++;

}

}

Question1: How do I get the heading to show?

Question2: How can I display this same info in a Grid?

Thanx
 
Hi,

1. Your header doesn't show because in the line:

label1.Text = "---- ----\n";

you erase the previous value you assigned to label1.Text. The correct code
is:

label1.Text += "---- ----\n"; // '+' was missing

2. Showing data in the grid takes a little more, check this doc:

http://support.microsoft.com/kb/315786/EN-US/

Regards - Octavio
 
Question 1 is the easier to answer :
label1.Text = "---- ----\n";
You forgot to use '+=' instead of '='

(I just suggest to make use of the 'System.Text.StringBuilder' class in
order to deal with text concatenation...)


To Question 2:
The are many ways to achieve this.
Have a look, for example, at the ListView.

Hope this helps...

Michel.
 
Hi Jlaz,

In addition to Octavio's answer, you should use \r\n for line breaks, not just \n. Some controls won't recognize \n as a line break.
 
Hi,
I enjoyed the link http://support.microsoft.com/kb/315786/EN-US/. But I am
unable to understand the fundamentals behind it. How can class members are
automatically get displayed in Data Grid. I have also observed that only
those values get displayed, for which there exist get.

Thanks
Chakravarti Mukesh
 
Hi Chakravarti,

Any object 'property' is shown. This means you can create objects or structs out of each line of your list

This code create a list of CostItem objects and stores it in an ArrayList.
The ArrayList is then bound to a DataGridView (Framework 2.0).
The DataGridView will detect the properties Rent and Year in the objects inside the ArrayList and will create necessary columns and headers.

The m behind the rent indicates that the number should be treated as decimal.

private void Form1_Load(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
list.Add(new CostItem(2004, 1200m));
list.Add(new CostItem(2005, 1260m));
list.Add(new CostItem(2004, 1323m));
list.Add(new CostItem(2004, 1389.15m));

dataGridView1.DataSource = list;
}


class CostItem
{
int iYear;
decimal dRent;

public int Year
{
get
{
return iYear;
}
}

public string Rent
{
get
{
// output the rent as currency
return dRent.ToString("c");
}
}

// Constructor
public CostItem(int y, decimal r)
{
iYear = y;
dRent = r;
}
}
 
Thanx guys,

Your answers worked for me. I'm making the switch from VB to C# any
recomendations?
 
Back
Top