2011-12-05 2 views
1

簡単な概要:ユーザー名とパスワードでファイルを作成したりアクセスしたりするには、ユーザーからの認証が必要なプログラムがあります。私はA型とB型を持っています。フォームAは私のプログラムのメインウィンドウであり、ボタンをフォームBを指しています。フォームBはログインフォームです。現時点では、フォームAを起動し、ファイルへのアクセスに必要なログインのためにボタンをクリックしてフォームBに移動する必要があります。私のプログラムは、人がログインしているかどうかを認識しており、新しいファイルを作成したり、すでに作成されたファイルにアクセスするためのボタンを有効にします(デフォルトでは無効になっています。フォームAの前にフォームBを起動する理由フォームAにユーザー認証情報が表示されないのはなぜですか?

フォームAを開く前にフォームの認証を確認するにはどうすればよいですか?

が、私は十分に明確ではないよなら、私に教えてください...

さて、私はこれを行うことにより、フォームAの前にフォームBを初期化することを試みた:

public MainWindow() 
{ 
    AuthenticationWindow login = new AuthenticationWindow(); 
    login.ShowDialog(); 

    InitializeComponent(); 
} 

問題はときということです認証が済んだら、自分のプログラムが自分のボタンを有効にしないようにします。

私はで私のフォームを初期化する前に認証をチェックしようとしています

public MainWindow() 
{ 
    AuthenticationWindow login = new AuthenticationWindow(); 
    login.ShowDialog(); 

    if (storedAuth != null) 
    { 
     // Making Deleting and Adding possible 
     // when file was opened. 
     tsmiOpen.Enabled = true; 
     tsmiNew.Enabled = true; 
    } 

    InitializeComponent(); 
} 

しかし、それでもまだ、私は、ファイルを開いたり、作成することはできません。プログラムのような認証されたユーザーをチェックしていないようだ。

これは、認証後に私のボタンを可能に私のコードです:

private void tsmiAuthenticate_Click(object sender, EventArgs e) 
{ 
    AuthenticationWindow authWindow = new AuthenticationWindow(); 

    authWindow.ShowDialog(); 
    storedAuth = authWindow.Result; 

    if (storedAuth != null) 
    { 

     tsmiOpen.Enabled = true; 
     tsmiNew.Enabled = true; 
    } 
} 

私はコードを縮め:

namespace Password_Manager 
{ 
    public partial class MainWindow : Form 
    { 
     private AuthenticateUser storedAuth; 
     private HashPhrase hash = new HashPhrase(); 

     private bool newSelected, openSelected; 

     public MainWindow() 
     { 
      AuthenticationWindow login = new AuthenticationWindow(); 
      login.ShowDialog(); 

      if (storedAuth != null) 
      { 
       // Making Deleting and Adding possible 
       // when file was opened. 
       tsmiOpen.Enabled = true; 
       tsmiNew.Enabled = true; 
      } 

      InitializeComponent(); 
     } 

     private void tsmiAuthenticate_Click(object sender, EventArgs e) 
     { 
      AuthenticationWindow authWindow = new AuthenticationWindow(); 

      // Displaying Authenticate Window. 
      // Not allowing switching between forms. 
      authWindow.ShowDialog(); 
      storedAuth = authWindow.Result; 


      if (storedAuth != null) 
      { 
       // Making Deleting and Adding possible 
       // when file was opened. 
       tsmiOpen.Enabled = true; 
       tsmiNew.Enabled = true; 
      } 
     } 

     private void tsmiAddEntry_Click(object sender, EventArgs e) 
     { 
      // Checking if the file is new or opened. 
      // This matter because we need to 
      // have appropriate path to the file. 
      if (openSelected) 
      { 
       AddEntryWindow addWindow = new AddEntryWindow 
        (this, storedAuth.UserName, storedAuth.Password, 
        ofdOpenFile.FileName); 

       // Displaying Add Entry Window. 
       // Not allowing switching between forms so I am using ShowDialog(). 
       addWindow.ShowDialog(); 
      } 
      if (newSelected) 
      { 
       AddEntryWindow addWindow = new AddEntryWindow 
        (this, storedAuth.UserName, storedAuth.Password, 
        sfdNewFile.FileName); 

       // Displaying Add Entry Window. 
       // Not allowing switching between 
       // forms so I am using ShowDialog(). 
       addWindow.ShowDialog(); 
      } 
     } 

     private void tsmiDeleteEntry_Click(object sender, EventArgs e) 
     { 
      // Checking if the file is new or opened. 
      // This matter because we need to 
      // have appropriate path to the file. 
      if (openSelected) 
      { 
       // When open file. 
       DeleteEntryWindow deleteEntyWindow = new DeleteEntryWindow 
        (this, storedAuth.UserName, 
        storedAuth.Password, ofdOpenFile.FileName); 
       deleteEntyWindow.ShowDialog(); 
      } 
      else if (newSelected) 
      { 
       // When new file. 
       DeleteEntryWindow deleteEntyWindow = new DeleteEntryWindow 
        (this, storedAuth.UserName, 
        storedAuth.Password, sfdNewFile.FileName); 
       deleteEntyWindow.ShowDialog(); 
      } 
     } 
    } 
} 

答えて

1

私の推測では、あなたのInitializeComponent()のコードは、あなたのボタンの.Enabledプロパティをリセットしていることです偽にする。このメソッドのコードは、VSのフォームデザイナーによって作成されたコードです。

これを試してみてください

...

public MainWindow() 
     { 
      InitializeComponent(); 
      AuthenticationWindow login = new AuthenticationWindow(); 
      login.ShowDialog(); 
      storedAuth = login.Result; 


      if (storedAuth != null) 
      { 
       // Making Deleting and Adding possible 
       // when file was opened. 
       tsmiOpen.Enabled = true; 
       tsmiNew.Enabled = true; 
      } 


     } 
+0

は、それはまだ私のボタンが無効になったまま。 – HelpNeeder

+0

コードをステップ実行しようとしましたか? – dbugger

+0

私はそれを疑う、私は何をステップしているかわからない:) – HelpNeeder

関連する問題