2010-05-27 56 views
2

我在C#中創建了一個WinForm應用程序,它的一個功能是在文本框中顯示文本。我正在編寫查詢單獨的類中的數據庫的邏輯,並且無法訪問我正在創建的類中的文本框元素(我得到的「名稱」在當前上下文錯誤中不存在)。我是否將所有表單邏輯放入Form1.cs文件中?正確的表單應用程序設計

回答

3

您應該儘量讓顯示邏輯與應用程序的其餘部分分開 - 最簡單的方法就是讓表單類處理獲取/設置表單值。這意味着您的數據訪問組件將查詢數據庫,並且表單必須將輸出映射到可以顯示的內容,例如,

public class Form1 : Form 
{ 
    public DataAccess Db { get; set; } 

    public void UpdateSomething() 
    { 
     this.textbox.Text = this.Db.GetSomeDatabaseValue(); 
    } 
} 
+0

這個簡單的例子很有意義,謝謝! – sooprise 2010-05-27 13:21:44

0

如果他們不嘗試將屬性中的修飾符設置爲public或internal。

編輯 - 編輯,以適應答案格式

+2

這應該是一條評論。 – 2010-05-27 13:14:04

+0

是否通過字段「修飾符」設置?如果是的話,我嘗試設置公共和內部的「修飾符」,並且都沒有工作... – sooprise 2010-05-27 13:16:55

0

如果您要訪問的文本框在另一大類更改訪問修飾符爲

公開或內部的(如果是在相同的程序集)

。 默認它是私人的

更好的是你可以將價值傳遞給業務邏輯層。不是整個控制,總是不好。

B.I是做所有的業務,所以文本框的價值就夠了。

+0

我是OOP的超級新手,你能解釋一下業務邏輯層的含義嗎? 商業邏輯是否在一個單獨的類中,並且形式文件中的GUI邏輯? – sooprise 2010-05-27 13:19:58

1

除了UI邏輯之外,不保留業務邏輯。您應該在Business類中引發一個事件並在UI表單中捕獲它。從那裏顯示它。

0

你看過backgroundworker?有了這個,你可以在你單擊表單上的一個按鈕時異步運行。您表單上的所有更新內容都將在您的表單上完成。您的其他代碼(您無法訪問Form1)將「報告進度」。當報告進度時,您可以發送任何想要Form1的對象,然後在表單上的事件處理程序中,您可以從該對象獲取信息並更新視圖。例如,您可以使用它來更新進度條,同時保持UI的響應。

0

我們目前正在用winforms做一個MVP模式的應用程序。我們在winforms中使用綁定,以便UI在數據執行時更新。我們的表單使用BindingSources和BindingLists。我們將主BindingSource綁定到我們的演示者類。表單代碼隱藏的

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; 

using SomeNameSpace.Utilities.UI; 
using SomeNameSpace.Utilities.Validation; 

namespace Management.UI 
{ 
    public partial class ManualControl : UserControl, IManualRaffleView 
    { 


     private ManualPresenter _presenter; 

     public ManualControl() 
     { 
      InitializeComponent(); 
     } 


     [Browsable(false)] 
     public ManualPresenter Presenter 
     { 
      get 
      { 
       return _presenter; 
      } 
      set 
      { 
       _presenter = value; 
       if(_presenter != null) 
       { 
        _manualPresenterBindingSource.DataSource = _presenter; 
        _ListBindingSource.DataSource = _presenter; 
        _ListBindingSource.DataMember = "Something"; 
        _KindListBindingSource.DataSource = _presenter; 
        _KindListBindingSource.DataMember = "SomethingElse"; 

        _presenter.CloseView += new Action(CloseMe); 
        _presenter.VerifyingCancel += new Func<bool>(VerifyingCancel); 
        _presenter.Showing += new Action(ShowMe); 
        _presenter.Synchronizer = this; 
       } 
      } 
     } 


     void CloseMe() 
     { 
      this.Enabled = false; 
     } 

     private void ManualRaffleForm_FormClosing(object sender, FormClosingEventArgs e) 
     { 
      e.Cancel = false; 
     } 

     private void ShowMe() 
     { 
      this.Enabled = true; 
     } 

     bool VerifyingCancel() 
     { 
      return MessageBox.Show("Cancel?", Notifier.ApplicationName, 
       MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
       MessageBoxDefaultButton.Button2) == DialogResult.Yes; 
     } 

     private void _cancelButton_Click(object sender, EventArgs e) 
     { 
      Presenter.HandleCancel(); 
     } 

     private void _initiateButton_Click(object sender, EventArgs e) 
     { 
      Presenter.HandleInitiate(); 
     } 

     private void _saveButton_Click(object sender, EventArgs e) 
     { 
      if(Presenter.Error == true.ToString()) 
       Presenter.HandleDone(); 
      else 
       _manualPresenterBindingSource.ResetBindings(false); 
     } 

    } 
} 

然後我們演示實現INotifyPropertyChanged,並可能是這個樣子

namespace SomeCompany.UI 
    { 
    public class ManualPresenter : INotifyPropertyChanged, IDataErrorInfo 
     { 
      #region Fields 
        //fields 
        #endregion Fields 

        public string SomeFormField 
        { get{ return _someFormField;} 
         set{ 
          if(_someFormField != value) 
           { 
           _someFormField = value; 
            //Update Model if Needed 
            _model.SomeFormField = _someFormField; 
           NotifyCHanged("SomeFormField"); 
           } 
          } 
        } 

的格式文本框將綁定到主持人的屬性和任何列表框或組合框將綁定到BindingLists。 然後,我們對我們的模型使用Linq to Sql。表格中的邏輯很少。大部分只是驗證所需的一點點。