2017-07-21 15 views
-3

ファイルが存在する場合、チェックボックスが自動的にチェックされるようなプログラムを作成したいと思います。ファイルが存在する場合CheckBoxを自動的にチェック

private void button3_Click(object sender, EventArgs e) 
{ 
    //check whether the file is exist or not 
    if (string curFile = @"C:\s\test.txt") 
    { 

    } 
    else 
    { 

    } 
} 
+0

コードの先頭に「Using System.IO」を含めます。 after:この行は例: checkBox1.Checked = File.Exists(@ "c:\ PathToFile \ file.txt")?true:false; – Rafael

答えて

0

を返します。 File.Exists()戻りtrueまたはfalse以来

if System.IO.File.Exists(@"C:\s\test.txt") { 
     yourcheckbox.checked = true 
    } 
    else { 
     yourcheckbox.checked = false 
    } 
+0

checked = Checked – Sybren

0

File.Exists(fileLocation)あなたはファイルが存在し、その後atributteがtrueに.checkedを設定するかどうかを確認するためにSystem.IOを使用しなければならないブール値

0

、あなたのような直接返された値を使用することができます:

このような何かやってに相当します
yourcheckbox.Checked = File.Exists(@"c:\Path\To\File.txt"); 

:このような何か

bool didItExist = File.Exists(@"c:\path\to\file.txt"); 
checkbox.Checked = didItExist; 

および/または

checkbox.Checked = File.Exists(@"c:\path\to\file.txt") ? true : false; 

File.Exists()が派生しているため、先頭にusing System.IO;を追加することを忘れないでください。

私が最初に提案した選択肢は、それを行う最も簡潔な方法だと思います。

あなたのコメントをフォローしてください...完全に機能的なスニペットはここにあります。

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.IO; 

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

      /* 
      * I'm adding the fields dynamically through code so that 
      * you know all the fields that are on my form. 
      * Because they are created dynamically, I'll use 'Find' later on 
      * to retrieve them. 
      */ 
      addButton(); 
      addTextField(); 
      addChkbx(); 
     } 

     private void addChkbx() 
     { 

      /* 
      * This is the checkbox that will be dyamically checked or unchecked 
      */ 
      CheckBox chkbx = new CheckBox(); 
      chkbx.Location = new Point(this.Location.X + 35, this.Location.Y + 55); 
      chkbx.Name = "thechkbx"; 
      this.Controls.Add(chkbx); 
     } 

     private void addTextField() 
     { 
      /* 
      * This is the field I'll be looking at to pull the file path 
      */ 
      TextBox txtb = new TextBox(); 
      txtb.Name = "txtbx_thebox"; 
      txtb.Location = new Point(this.Location.X + 5, this.Location.X + 35); 
      this.Controls.Add(txtb); 
     } 

     private void addButton() 
     { 
      /* 
      * This will be the button you'll click on to get the check/uncheck behavior 
      */ 
      Button btn = new Button(); 
      btn.Text = "ClickMe"; 
      btn.Click += Btn_Click; 
      btn.Location = new Point(this.Location.X + 5, this.Location.Y + 5); 
      this.Controls.Add(btn); 
     } 

     private void Btn_Click(object sender, EventArgs e) 
     { 
      /* 
      * This is the answer to your question 
      * Because I dynamically create the fields, I'm using a Find to get them and use them. 
      */ 
      ((CheckBox)this.Controls.Find("thechkbx", true)[0]).Checked = File.Exists(((TextBox)this.Controls.Find("txtbx_thebox", true)[0]).Text); 


     } 
    } 
} 
+0

私のコードを実行した後、チェックボックスはまだオフになっています。 –

+0

実際のコードを掲載して絞り込むことができます。 –

関連する問題