为什么我的c#应用程序不起作用?

时间:2014-06-03 02:34:23

标签: c#

这个节目应该从你输入的日期或每日漫画中获得加菲尔德漫画。第一个按钮应该允许你选择一个自定义漫画,但只有每日漫画(按钮2)似乎工作在这里代码:

    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 Garfield_Comic_Viewer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int month = int.Parse(textBox1.Text);
            int day = int.Parse(textBox2.Text);
            int year = int.Parse(textBox3.Text);

        switch (month)
    {
        case 1:
            month = 01;
        break;
        case 2:
        month = 02;
        break;
        case 3:
        month = 03;
        break;
        case 4:
        month = 04;
        break;
        case 5:
        month = 05;
        break;
        case 6:
        month = 06;
        break;
        case 7:
        month = 07;
        break;
        case 8:
        month = 08;
        break;
        case 9:
        month = 09;
        break;
    }
            switch (day)
        {
            case 1:
                day = 01;
                break;
            case 2:
                day = 02;
                break;
            case 3:
                day = 03;
                break;
            case 4:
                day = 04;
                break;
            case 5:
                day = 05;
                break;
            case 6:
                day = 06;
                break;
            case 7:
                day = 07;
                break;
            case 8:
                day = 08;
                break;
            case 9:
                day = 09;
                break;
        }



            pictureBox1.ImageLocation = ("http://garfield.com/uploads/strips/" +year+ "-"            +month+ "-" + day + ".jpg");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string mon = DateTime.Now.ToString("MM");
            string d = DateTime.Now.ToString("dd");
            string y = DateTime.Now.ToString("yyyy");
            pictureBox1.ImageLocation = "http://garfield.com/uploads/strips/" + y + "-" + mon + "-" + d + ".jpg";
 DateTime whole = DateTime.Now;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

问题是你期望整数表现得像字符串一样。

您缺少的一点是:

 int a = 01;

与:

相同
 int a = 1;

没有区别。你不能填充一个整数。您需要的是使用格式说明符将格式整数转换为字符串。像这样:

pictureBox1.ImageLocation = 
    string.Format("http://garfield.com/uploads/strips/{0:D4}-{1:D2}-{2:D2}.jpg", 
        year, month, day);

这将给出一个字符串,其中包含格式为yyyy-mm-dd的数字。

(填充字符串格式的整数的文档是here。)