4 Replies - 708 Views - Last Post: 29 December 2010 - 07:28 AM Rate Topic: -----

#1 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

DateTime.Now

Posted 29 December 2010 - 04:56 AM

hello, i am using winForms, and i am wandering what the best way to go about putting a DateTime.Now string into the form.

i have tried the following into a rich text box:

ClockBox.AppendText(String.Format("{0}", DateTime.Now));


thank you!
Is This A Good Question/Topic? 0
  • +

Replies To: DateTime.Now

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: DateTime.Now

Posted 29 December 2010 - 05:29 AM

Hi Simagen,

That way is as good as any I think really. I suppose you don't really need the call the String.Format() in this example as you are only displaying the one thing on it's own with no formatting. You could just simply do this:

ClockBox.AppendText(DateTime.Now.ToString());


That is equivalent to your line of code. Obviously though, if you want to include any formatting, or include the DateTime as part of a sentence for example, String.Format() is a good way to go :). You can put the string in a label, a normal text box etc.

This post has been edited by CodingSup3rnatur@l-360: 29 December 2010 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: DateTime.Now

Posted 29 December 2010 - 05:38 AM

Hey jason :) its cam

this is a personal project i am doing to practice!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public double salePrice { get; private set; }
        public double discountPrice { get; private set; }
        public double vat { get; private set; }
        public double onlyVat { get; private set; }
        public double totalPrice { get; private set; }
        public double changeGiven { get; private set; }
        public double payment { get; private set; }
        public double subtotal { get; private set; }

        private void ClockBox_TextChanged(object sender, EventArgs e)
        {
            ClockBox.AppendText(DateTime.Now.ToString());
        }  

        private void calcPriceAndDiscount_Click(object sender, EventArgs e)
        {
            double salePrice;
            double subtotal;
            double discountPrice;
            
            VATBox.AppendText("20");

            if (String.IsNullOrWhiteSpace(DiscountBox.Text))
            {
                DiscountBox.AppendText("0");
            }

            if (double.TryParse(PriceBox.Text, out salePrice))
            {
                if(double.TryParse(DiscountBox.Text, out discountPrice))
                {
                subtotal = (salePrice - discountPrice);
                SubtotalBox.AppendText(String.Format("{0:c}", subtotal));

                this.calcPriceAndDiscount.Enabled = false;
                }
            }

            else
            {
                SubtotalBox.AppendText("Could not be calculated!");
            }           
        }

        private void calcWithVat_Click(object sender, EventArgs e)
        {            
            double salePrice;
            double subtotal;
            double discountPrice;
            
            double onlyVat;
            double totalPrice;
            double vat = 20.00;            
            
            if (double.TryParse(PriceBox.Text, out salePrice))
            {
                if (double.TryParse(DiscountBox.Text, out discountPrice))
                {
                    subtotal = (salePrice - discountPrice);

                    if (double.TryParse(VATBox.Text, out vat))
                    {
                          onlyVat = (subtotal / 100.00 * vat);
                          totalPrice = (onlyVat + subtotal);   

                          FinalPriceBox.AppendText(String.Format("{0:c}", totalPrice));

                          calcWithVat.Enabled = false;
                    }                                                           
                }
            }

            else
            {
                FinalPriceBox.AppendText("Could not be calculated!");
            }            
        }

        private void calcPaymentMinPrice_Click(object sender, EventArgs e)
        {
            double changeGiven;
            double payment;

            double salePrice;
            double subtotal;
            double discountPrice;

            double onlyVat;
            double totalPrice;
            double vat = 20.00;

            if (double.TryParse(PriceBox.Text, out salePrice))
            {
                if (double.TryParse(DiscountBox.Text, out discountPrice))
                {
                    subtotal = (salePrice - discountPrice);

                    if (double.TryParse(VATBox.Text, out vat))
                    {
                        onlyVat = (subtotal / 100.00 * vat);
                        totalPrice = (onlyVat + subtotal);

                        if (double.TryParse(PaymentBox.Text, out payment))
                        {
                            changeGiven = (payment - totalPrice);

                            ChangeGivenBox.AppendText(String.Format("{0:c}", changeGiven));

                            calcPaymentMinPrice.Enabled = false;
                        }

                        else
                        {
                            PaymentBox.AppendText("Please enter payment amount!");                            
                        }
                    }
                }
            }             
        }

        private void PrintButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.PaymentBox.Text) || String.IsNullOrWhiteSpace(this.SubtotalBox.Text)
                || String.IsNullOrWhiteSpace(this.VATBox.Text) || String.IsNullOrWhiteSpace(this.FinalPriceBox.Text)
                || String.IsNullOrWhiteSpace(this.PaymentBox.Text) || String.IsNullOrWhiteSpace(this.ChangeGivenBox.Text))
            {

                return;
            }

            else
            {
                PrintButton.Enabled = false;

                MessageBox.Show("Printing Receipt!");
                Application.Exit();
            }
        }

        private void AbortButton_Click(object sender, EventArgs e)
        {

        }

             
    }
}


i am still working on the DateTime.Now at the top of the coding which will go into my rich text box, which i have disabled from letting the user edit it.

hope you are well!

This post has been edited by simagen: 29 December 2010 - 05:53 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: DateTime.Now

Posted 29 December 2010 - 07:18 AM

You could go on the MSDN and see all the options available to you.
http://msdn.microsof...m.datetime.aspx

You would see there are several existing methods like

.ToShortTimeString()
.ToLongTimeString()
.ToShortDateString()
.ToShortTimeString()

And that you can even create your own custom formated string
.ToString("HH:mm:ss DD-mmm-yyyy"); // For example

The MSDN should always be your first stop to see what you can do with an object, type or method.
An easy google approach for this would have been to google with the terms "MSDN DateTime"
Was This Post Helpful? 0
  • +
  • -

#5 simagen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 10-November 10

Re: DateTime.Now

Posted 29 December 2010 - 07:28 AM

thank you very much tlhIn'toq for the guidance! i will definately use this!

thank you all

cam
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1