471,325 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Carriage return

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
Feb 12 '06 #1
8 9209
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

"Jlaz" <Xb*******@msn.com> escribió en el mensaje
news:em*************@TK2MSFTNGP09.phx.gbl...
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

Feb 12 '06 #2
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.
On Sun, 12 Feb 2006 08:58:21 +0100, Jlaz <Xb*******@msn.com> wrote:
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


--
Happy coding!
Morten Wennevik [C# MVP]
Feb 12 '06 #3
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
"Octavio Hernandez" <oc*****************@gmail.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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

"Jlaz" <Xb*******@msn.com> escribió en el mensaje
news:em*************@TK2MSFTNGP09.phx.gbl...
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


Feb 12 '06 #4
Why not use a ListView?

"Jlaz" <Xb*******@msn.com> wrote in message
news:em*************@TK2MSFTNGP09.phx.gbl...
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

Feb 12 '06 #5
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;
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Feb 12 '06 #6
Excellent post, Morten!

Regards - Octavio
"Morten Wennevik" <Mo************@hotmail.com> escribió en el mensaje
news:op.s4u470suklbvpo@stone...
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;
}
}
--
Happy coding!
Morten Wennevik [C# MVP]

Feb 12 '06 #7
Thanx guys,

Your answers worked for me. I'm making the switch from VB to C# any
recomendations?
"Jlaz" <Xb*******@msn.com> wrote in message
news:em*************@TK2MSFTNGP09.phx.gbl...
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

Feb 12 '06 #8
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.
"Jlaz" <Xb*******@msn.com> a écrit dans le message de news:
em*************@TK2MSFTNGP09.phx.gbl...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

Feb 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Canes_Rock | last post: by
2 posts views Thread by Andrew Chanter | last post: by
2 posts views Thread by David Cho | last post: by
3 posts views Thread by Dinsdale | last post: by
11 posts views Thread by evenlater | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.