小编Cel*_*itz的帖子

c# InvalidArgument=“-1”的值对于“index”无效

我有一个 cmbPlace(组合框),它的项目自动填充了 System.IO 驱动器(C:\、D:\ 等)。同时它也有验证事件。代码如下:

using System.IO;
public FNamefile()
{
    InitializeComponent();
    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
        cmbPlace.Items.Add(d.Name);
    }
}

private void FNamefile_Load(object sender, EventArgs e)
{
   errorProvider1.ContainerControl = this;
}

private bool ValidatePlace()
{
   bool bStatus = true;
   int m = cmbPlace.SelectedIndex;
   if ((cmbPlace.Items[m]).ToString() == cmbPlace.Text)
   {
       errorProvider1.SetError(cmbPlace, "");
   }
   else if (cmbPlace.Text == "" || (cmbPlace.Items[m]).ToString() != cmbPlace.Text)
   {
       errorProvider1.SetError(cmbPlace, "Please enter a valid location");
       bStatus = false;
   }
   return bStatus;
}
private void …
Run Code Online (Sandbox Code Playgroud)

c# validation winforms

2
推荐指数
1
解决办法
5950
查看次数

去年的总天数

我有label1 label2和button1,其中我按下button1,label1文本将根据上个月的总天数(比如'31'天)更改,label2文本将根据去年的总天数(假设为'365'天)进行更改

我只知道如何编码上个月使用DateTime.DaysInMonth的总天数,但没有DateTime.DaysInYear的方法不是它

这是我的代码

private void button1_Click(object sender, EventArgs e)
{
    //Last Month
    string month = DateTime.Now.ToString("MM");
    int months = Int32.Parse(month);
    int previousmonths = months - 1;

    //Month in this Year
    string year = DateTime.Now.ToString("yyyy");
    int years = Int32.Parse(year);
    int daysmonth = DateTime.DaysInMonth(years, previousmonths);
    MessageBox.Show(daysmonth.ToString());

    //Last Year
    string year = DateTime.Now.ToString("yyyy");
    int years = Int32.Parse(year);
    int lastyears = years - 1;
    int daysyear = DateTime.DaysInYear(lastyears);
    MessageBox.Show(daysyear.ToString());
}
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
2
解决办法
823
查看次数

标签 统计

c# ×2

validation ×1

winforms ×1