点击添加到文本框中的“已选择”节点内容

时间:2011-07-15 06:30:03

标签: c# .net treeview

我正在学习C#编程语言,正在为SAP business One制作工资单应用程序插件。我有一个TreeView,需要在用户点击该节点或按下“添加”按钮后,在TextBox上显示一个节点的名称,最好只在点击时。我正在使用Visual Studio 2010和Microsoft SQL Server 2008。

情景:

- Component                        - Parent
          Earnings                     - child
              Housing Allowance                  - content of child
              Mobile Phone Allowance             - clicked and highlighted node
              Mileage Allowance
          Deductions                   - child

在上文中,我想要一种方式,如果用户点击“移动电话免税额”,并且突出显示,则“移动电话免税额”会出现在文本框中。我不确定这是否可以在没有添加按钮的情况下完成。

收入和扣减子项是从数据库中填充的。我需要以上来制作薪资计算器。

我的代码:

private void PayrollFormulaBuilder_Load(object sender, EventArgs e)
{
    // Get service instance
    var earnDeductMasterService = Program.Kernel.Get<IEarnDeductMasterService>();

    //Query database for all records that have earnings
    var earnings = from ed in earnDeductMasterService.GetAllEarnDeductMasters()
                   where ed.U_PD_type.Trim().Equals("Earnings".Trim(), StringComparison.CurrentCultureIgnoreCase)
                   select ed.U_PD_description;

    if (earnings.Any(x => x != null))
    {
        //To populate subtree Earnings with U_PD_description  = Earnings results
        List<string> earningList = new List<string>(earnings) { };

        //adding all earnings to "Earnings" node
        foreach (string earning in earningList)
        {
            treeView1.Nodes[0].Nodes[0].Nodes.Add(earning);
        }
    }
    else
    {
        //Nothing to populate            
    }

    //Query database for all records that have deductions
    var deductions = from ed in earnDeductMasterService.GetAllEarnDeductMasters()
                     where ed.U_PD_type.Trim().Equals("Deductions".Trim(), StringComparison.CurrentCultureIgnoreCase)
                     select ed.U_PD_description;

    if (deductions.Any(x => x != null))
    {
        //To populate subtree Deductions with U_PD_description = Deductions results
        List<string> deductionList = new List<string>(deductions) { };

        //adding all earnings to "Earnings" node
        foreach (string deduction in deductionList)
        {
            treeView1.Nodes[0].Nodes[1].Nodes.Add(deduction);
        }
    }
    else
    {
        //Nothing to populate            
    }
}

我想我必须设置一个方法来捕捉这个...但我不确定

private void treeView1_DoubleClick(object sender, EventArgs e)
{
    if (inputStatus)
    {
        formula_display.Text += "something here"  // my Richtextbox for showing input
    }
    else
    {
        formula_display.Text = "something here"
        inputStatus = true;
    }
}

2 个答案:

答案 0 :(得分:2)

您是否可以使用TreeView AfterSelect事件代替“在这里”?

treeview1.AfterSelect += new TreeViewEventHandler(treeview1_AfterSelect);

..和..

void treeview1_AfterSelect(object sender, TreeViewEventArgs e)
{
    formula_display.Text = e.Node.Text;
}

答案 1 :(得分:0)

您可以向树视图的NodeMouseClick事件添加事件处理程序:

treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);

事件处理程序应如下所示:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //Identify whether the user has clicked the MobilePhoneAllowance node by
            //using the properties of e.Node

            //Then set TextBox.Text to the required text in the node
        }