面向对象的方法来自vb6背景

时间:2012-06-17 12:08:39

标签: c# vb6

我来自vb6背景,我正在慢慢测试c#水。我的问题是我很难在我的程序中采用面向对象的方法,因为我倾向于按照我在vb6中编码的方式来模式化我的程序。以我正在创建的软件的数据输入部分为例,正如您所看到的那样,我仍然按照我在vb6中编码的方式对其进行编码。

namespace WLMS
{
public partial class frmBA : Form
{
    enum status
    {
        add,
        edit,
        delete,
        complete,
        datafill,
    }

    status stat;
    clsSqlCommands sqlCommands = new clsSqlCommands();
    string connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString;
    int dataID = 0;


    public frmBA()
    {
        InitializeComponent();

    }

    private void displayInGrid()
    { 
        DataTable dt = new DataTable();
        dt = sqlCommands.dataFill("select series,baName,baLoc from tblBA order by baName",connectionString);
        if (dt != null)
        {
            dgBA_List.DataSource = dt;
            dgBA_List.Columns[0].HeaderText = null;
            dgBA_List.Columns[1].HeaderText = "BA NAME";
            dgBA_List.Columns[2].HeaderText = "BA LOCATION";
            dgBA_List.Columns[0].Visible = false;
            dgBA_List.Columns[1].Width = 100;
            dgBA_List.Columns[2].Width = 200;
            dataID = 0;        
        }
    }

    private void frmBA_Load(object sender, EventArgs e)
    {
        displayInGrid();
    }

    private void tlADD_Click(object sender, EventArgs e)
    {
        groupBox1.Enabled = true;
        clearTextBoxes(groupBox1);
        txtBAName.Focus();
        stat = status.add;
    }

    private void tlEDIT_Click(object sender, EventArgs e)
    {
        if (dataID != 0)
        {
            groupBox1.Enabled = true;
            stat = status.edit;
        }
        else
            MessageBox.Show("click on item to edit");
    }


    private void tlDELETE_Click(object sender, EventArgs e)
    {
        deleteData();
    }

    private void tlSAVE_Click(object sender, EventArgs e)
    {
        if (checkFilledTextBoxes(groupBox1) == true)
        {
            switch (stat)
            {
                case status.add:
                {
                    addNewData();
                    break;
                }
                case status.edit:
                {
                    editData();
                    break;
                }
                default:
                {
                    break;
                }
            }
        }

    }

    private bool checkForDuplicates()
    {
        DataRow dtr = sqlCommands.getOneRow("select count(*) as cnt from tblba where baName = '" + txtBAName.Text + "' and baLoc = '" + txtBALoc.Text + "'", connectionString);
        if (Convert.ToInt16(dtr["cnt"]) < 1)
        {
            return false;
        }
        else
            return true;
    }

    private void editData()
    {
        if (!checkForDuplicates())
        {
            sqlCommands.dataManipulate("update tblBa set baName = '" + txtBAName.Text + "',baLoc =  '" + txtBALoc.Text + "' where series = " + dataID + "", connectionString);
            clearTextBoxes(groupBox1);
            groupBox1.Enabled = false;
            stat = status.complete;
            displayInGrid();
            MessageBox.Show("Record Edited");
        }
        else
            MessageBox.Show("Duplicate record");
    }

    private void deleteData()
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            sqlCommands.dataManipulate("delete from tblBa where series = " + dataID + "", connectionString);
            clearTextBoxes(groupBox1);
            groupBox1.Enabled = false;
            stat = status.complete;
            displayInGrid();
            MessageBox.Show("Record deleted");
        }
        else
            MessageBox.Show("Duplicate record");
    }

    private void addNewData()
    {
        if (!checkForDuplicates())
        {
            sqlCommands.dataManipulate("insert into tblBa (baName,baLoc) values ('" + txtBAName.Text + "','" + txtBALoc.Text + "')", connectionString);
            clearTextBoxes(groupBox1);
            txtBAName.Focus();
            stat = status.complete;
            displayInGrid();

            MessageBox.Show("Record Added");
        }
        else
            MessageBox.Show("Duplicate record");
    }

    private void clearTextBoxes(GroupBox gprx)
    {
        foreach (TextBox txtBx in gprx.Controls.OfType<TextBox>())
        {
            txtBx.Text = "";
        }
    }

    private Boolean checkFilledTextBoxes(GroupBox gprx)
    {
        foreach (TextBox txtBx in gprx.Controls.OfType<TextBox>())
        {
            if (txtBx.Text == "")
                return false;
        }
        return true;
    }

    private void tlEXIT_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

    private void dgBA_List_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        int rowIndex = e.RowIndex;
        DataGridViewRow row = dgBA_List.Rows[rowIndex];
        dataID = Convert.ToInt16(row.Cells[0].Value);
        txtBALoc.Text = row.Cells[1].Value.ToString();
        txtBAName.Text = row.Cells[2].Value.ToString();
        groupBox1.Enabled = false;
    }
}
}

它有两个位于组框内的文本框,一个数据网格视图和5个工具条按钮,用于添加,编辑,删除,保存和退出。我的问题是,我如何重构我的代码以采用面向对象的方法?

请帮助..谢谢

2 个答案:

答案 0 :(得分:2)

在使用GUI时,我建议稍微阅读一下MVC模式(Winforms)Looking for clean WinForms MVC tutorial for C#或MVVM模式(WPF)MVVM: Tutorial from start to finish?

我还建议阅读一本体面的设计模式书。我在第一次出发时发现非常有用的是Head First Design Patterns一书。很容易遵循。他们使用的语言是Java,但这在语法上非常接近C#。

也许还有关于GUI体系结构的这篇优秀Martin Fowler文章的阅读:http://martinfowler.com/eaaDev/uiArchs.html

编辑:继续丹尼斯的评论。您还可以查看Winform开发的MVP模式。实现该模式的几种不同方式:http://martinfowler.com/eaaDev/SupervisingPresenter.htmlhttp://martinfowler.com/eaaDev/PassiveScreen.html

答案 1 :(得分:0)

简单回答 - 你做不到。

取决于技术。

  • ASP.NET - 使用MVC,然后很自然。
  • WPF:嗯,无论如何,UI都在XAML中。 Winforms:你不能,控制是在设计师完成的,而数据bdinding也不是太好。

在WInForms中你最好的做法实际上并不是在表单中保存逻辑而是在另一个对象中,但是表单是按照设计者想要的方式完成的,或者你失去了所有的兼容性。