2011-07-15 48 views
0

我正在學習C#編程語言,並且正在爲SAP業務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

你能使用TreeView AfterSelect事件代替你的「某物在這裏」?

treeview1.AfterSelect += new TreeViewEventHandler(treeview1_AfterSelect); 

..和..

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

對不起,我編輯了void treeview1_AfterSelect void tree View1_DoubleClick –

+0

那麼將它改爲NodeMouseDoubleClick,這仍然會給你e.Node的功能 – fatty

+0

修改你的代碼有點但我已經解決了這個問題。謝謝。 –

0

你可以加一個事件處理程序到您的TreeView的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 
     }