English 中文(简体)
如何避免再三的法典提高效率
原标题:how to avoid the repeated code to increase the efficiency

我有<条码>DataGrid view1和ListView,而且我从未选择过名单(我通过<条码>)。 清单: 列入<代码>DataGrid的物项,根据该项目

我已经写过这样的一些法典。

 private void listview_selectedindexchanged(object sender event args)
 {
     if (listview.SelectedItems.Count > 0 && listview.SelectedItems[0].Group.Name == "abc")
     {
            if(lstview.SelectedItems[0].Text.ToString() == "sfs")
            {
              method1();
            }
            else
            {
                // datagrid view1 binding
               blah.....
             }
     }
     if (lstview.SelectedItems.Count > 0 && lstview.SelectedItems[0].Group.Name == "def")
     {

           if(lstview.SelectedItems[0].Text.ToString() == "xyz")
           {
               method 1();
           }
           if(lstview.SelectedItems[0].Text.ToString() == "ghi")
           {
               method 2(a,b);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "jkl")
           {
               method 2(c,d);
           }
           if(lstview.SelectedItems[0].Text.ToString() == "mno")
           {
               method 3();
           }

       }
   }  
private void method 1()
{ 
  // datagrid view1 binding
    blahh     
}
private void method 2(e,g)
{
  // datagrid view1 binding
  blah....blah..
}
private void method 3()
{

    // datagrid view1 binding
}

I have done it like above ... I think this is not an efficient way to do the coding. and this code consisits of a lot of repeated lines, is there any way to refractor this code to a small bunch of code ...... in order improve the efficiency?

任何提高守则效率的想法和样本都对我有帮助。

许多预告......。

我正在使用C#和WinForms的申请。

最佳回答

你可以把一名代表从名单上删除。 并且,在选用封顶物品时也称呼。 例如,你将填写你的名单箱如下:

ListViewItem item = new ListViewItem("abc");
item.Tag = new Delegate(method1);
lstview.Items.Add(item);

现在,在选定该项目时,你执行这样的方法:

private void listview_selectedindexchanged(object sender event args)
{
    ((Delegate)lstview.SelectedItems[0].Tag)(); // this will execute method1 if the item with text "abc" gets selected
}

NOTE: ! have not tested this code, but something along those lines should work and you don t have to write the If-statement, you only have to construct the items correctly.

还指出,这或许难以为新加入该法典的人读懂。

问题回答

你可以很容易地找到一种新的方法,以“数据网观点1具有约束力”。 因此,这种方法是从所有需要采用具有约束力方法中要求的。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签