Carriage return

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jlaz

    #1

    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_Clic k(object sender, System.EventArg s 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(m Rent, 2);

    iYear++;

    }

    }

    Question1: How do I get the heading to show?

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

    Thanx


  • Octavio Hernandez

    #2
    Re: Carriage return

    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:



    Regards - Octavio



    "Jlaz" <Xbrenjoe3@msn. com> escribió en el mensaje
    news:emIhao6LGH A.536@TK2MSFTNG P09.phx.gbl...[color=blue]
    >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_Clic k(object sender, System.EventArg s 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(m Rent, 2);
    >
    > iYear++;
    >
    > }
    >
    > }
    >
    > Question1: How do I get the heading to show?
    >
    > Question2: How can I display this same info in a Grid?
    >
    > Thanx
    >
    >[/color]


    Comment

    • Morten Wennevik

      #3
      Re: Carriage return

      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 <Xbrenjoe3@msn. com> wrote:
      [color=blue]
      > 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_Clic k(object sender, System.EventArg s 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(m Rent, 2);
      >
      > iYear++;
      >
      > }
      >
      > }
      >
      > Question1: How do I get the heading to show?
      >
      > Question2: How can I display this same info in a Grid?
      >
      > Thanx
      >
      >
      >[/color]



      --
      Happy coding!
      Morten Wennevik [C# MVP]

      Comment

      • Chakravarti Mukesh

        #4
        Re: Carriage return

        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" <octavio.hdez.N OSPAM@gmail.com > wrote in message
        news:%23aB8Wq7L GHA.984@tk2msft ngp13.phx.gbl.. .[color=blue]
        > 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/[/color]
        [color=blue]
        >
        > Regards - Octavio
        >
        >
        >
        > "Jlaz" <Xbrenjoe3@msn. com> escribió en el mensaje
        > news:emIhao6LGH A.536@TK2MSFTNG P09.phx.gbl...[color=green]
        >>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_Clic k(object sender, System.EventArg s 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(m Rent, 2);
        >>
        >> iYear++;
        >>
        >> }
        >>
        >> }
        >>
        >> Question1: How do I get the heading to show?
        >>
        >> Question2: How can I display this same info in a Grid?
        >>
        >> Thanx
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Nick Hounsome

          #5
          Re: Carriage return

          Why not use a ListView?

          "Jlaz" <Xbrenjoe3@msn. com> wrote in message
          news:emIhao6LGH A.536@TK2MSFTNG P09.phx.gbl...[color=blue]
          >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_Clic k(object sender, System.EventArg s 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(m Rent, 2);
          >
          > iYear++;
          >
          > }
          >
          > }
          >
          > Question1: How do I get the heading to show?
          >
          > Question2: How can I display this same info in a Grid?
          >
          > Thanx
          >
          >[/color]


          Comment

          • Morten Wennevik

            #6
            Re: Carriage return

            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(obje ct 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.D ataSource = 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]

            Comment

            • Octavio Hernandez

              #7
              Re: Carriage return

              Excellent post, Morten!

              Regards - Octavio


              "Morten Wennevik" <MortenWennevik @hotmail.com> escribió en el mensaje
              news:op.s4u470s uklbvpo@stone.. .[color=blue]
              > 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(obje ct 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.D ataSource = 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][/color]


              Comment

              • Jlaz

                #8
                Re: Carriage return

                Thanx guys,

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


                "Jlaz" <Xbrenjoe3@msn. com> wrote in message
                news:emIhao6LGH A.536@TK2MSFTNG P09.phx.gbl...[color=blue]
                >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_Clic k(object sender, System.EventArg s 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(m Rent, 2);
                >
                > iYear++;
                >
                > }
                >
                > }
                >
                > Question1: How do I get the heading to show?
                >
                > Question2: How can I display this same info in a Grid?
                >
                > Thanx
                >
                >[/color]


                Comment

                • Michel Bertin

                  #9
                  Re: Carriage return

                  Question 1 is the easier to answer :
                  [color=blue]
                  > label1.Text = "---- ----\n";[/color]
                  You forgot to use '+=' instead of '='

                  (I just suggest to make use of the 'System.Text.St ringBuilder' 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" <Xbrenjoe3@msn. com> a écrit dans le message de news:
                  emIhao6LGHA.536 @TK2MSFTNGP09.p hx.gbl...[color=blue]
                  >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_Clic k(object sender, System.EventArg s 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(m Rent, 2);
                  >
                  > iYear++;
                  >
                  > }
                  >
                  > }
                  >
                  > Question1: How do I get the heading to show?
                  >
                  > Question2: How can I display this same info in a Grid?
                  >
                  > Thanx
                  >
                  >[/color]


                  Comment

                  Working...