我有一个表格,上面有一个表格布局控件。表格布局有3列和多行。
第一列包含标签,第二列是文本框,组合框或日期时间选择器。第三列包含一张图片,如果用户输入的信息包含错误,则该图片会打开或关闭。这是为了让他们能够看到他们错误填写或错过了哪些字段。
第五行包含一个comboBox,用户可以在其中选择要输入的交易类型。根据用户选择的内容,控件可能必须重新安排自己。
让我举一个使用截图的例子:
http://i.stack.imgur.com/OZmFH.png
这是运行表单的标准版本,用户未选择事务类型。
现在,假设用户选择贷款付款。表格将重新安排以显示以下内容:
http://i.stack.imgur.com/j76wG.png
你可以看到叙述和总数已经向下移动了几行,并且显示了一个新的框,允许用户选择贷款和支付的数量。
根据所选的交易类型,可以显示3种不同的布局。
现在我通过在Transaction Type组合框上检查SelectedIndexChanged事件来执行此操作,并调用方法来设置视图。
实现这一目标的最佳方法是什么?现在我有代码可以做到,但它非常混乱。
我应该看一下桌面布局面板以外的东西吗?
readonly TableLayoutPanelCellPosition tlCCccLbl = new TableLayoutPanelCellPosition(0, 5); readonly TableLayoutPanelCellPosition tlCCccList = new TableLayoutPanelCellPosition(1, 5); readonly TableLayoutPanelCellPosition tlCCccError = new TableLayoutPanelCellPosition(2, 5); readonly TableLayoutPanelCellPosition tlCCnarrativeLbl = new TableLayoutPanelCellPosition(0, 6); readonly TableLayoutPanelCellPosition tlCCnarrativeTxt = new TableLayoutPanelCellPosition(1, 6); readonly TableLayoutPanelCellPosition tlCCgrossLbl = new TableLayoutPanelCellPosition(0, 7); readonly TableLayoutPanelCellPosition tlCCgrossTxt = new TableLayoutPanelCellPosition(1, 7); readonly TableLayoutPanelCellPosition tlCCgrossError = new TableLayoutPanelCellPosition(2, 7); readonly TableLayoutPanelCellPosition tlStdnarrativeLbl = new TableLayoutPanelCellPosition(0, 5); readonly TableLayoutPanelCellPosition tlStdnarrativeTxt = new TableLayoutPanelCellPosition(1, 5); readonly TableLayoutPanelCellPosition tlStdgrossLbl = new TableLayoutPanelCellPosition(0, 6); readonly TableLayoutPanelCellPosition tlStdgrossTxt = new TableLayoutPanelCellPosition(1, 6); readonly TableLayoutPanelCellPosition tlStdgrossError = new TableLayoutPanelCellPosition(2, 6); readonly TableLayoutPanelCellPosition tlStdLoanPymntsLbl = new TableLayoutPanelCellPosition(0, 8); readonly TableLayoutPanelCellPosition tlStdLoanPymntsCmb = new TableLayoutPanelCellPosition(1, 8); readonly TableLayoutPanelCellPosition tlStdLoanPymntsError = new TableLayoutPanelCellPosition(2, 8); readonly TableLayoutPanelCellPosition tlStdLoanLbl = new TableLayoutPanelCellPosition(0, 9); readonly TableLayoutPanelCellPosition tlStdLoanError = new TableLayoutPanelCellPosition(2, 9); readonly TableLayoutPanelCellPosition tlStdccLbl = new TableLayoutPanelCellPosition(0, 10); readonly TableLayoutPanelCellPosition tlStdccList = new TableLayoutPanelCellPosition(1, 10); readonly TableLayoutPanelCellPosition tlStdccError = new TableLayoutPanelCellPosition(2, 10); readonly TableLayoutPanelCellPosition tlLnLoanLbl = new TableLayoutPanelCellPosition(0, 5); readonly TableLayoutPanelCellPosition tlLnLoanCmb = new TableLayoutPanelCellPosition(1, 5); readonly TableLayoutPanelCellPosition tlLnLoanError = new TableLayoutPanelCellPosition(2, 5); readonly TableLayoutPanelCellPosition tlLnLoanPymntsLbl = new TableLayoutPanelCellPosition(0, 6); readonly TableLayoutPanelCellPosition tlLnLoanPymntsCmb = new TableLayoutPanelCellPosition(1, 6); readonly TableLayoutPanelCellPosition tlLnLoanPymntsError = new TableLayoutPanelCellPosition(2, 6); readonly TableLayoutPanelCellPosition tlLnnarrativeLbl = new TableLayoutPanelCellPosition(0, 7); readonly TableLayoutPanelCellPosition tlLnnarrativeTxt = new TableLayoutPanelCellPosition(1, 7); readonly TableLayoutPanelCellPosition tlLngrossLbl = new TableLayoutPanelCellPosition(0, 8); readonly TableLayoutPanelCellPosition tlLngrossTxt = new TableLayoutPanelCellPosition(1, 8); readonly TableLayoutPanelCellPosition tlLngrossError = new TableLayoutPanelCellPosition(2, 8); private void cmbTransactionType_SelectedIndexChanged(object sender, EventArgs e) { ToggleLoanControls(false); ToggleCreditCardControls(false); ToggleReceiptNumbers(); if (clsTransactionTypes.TransactionStringToTransactionID(cmbTransactionType.Text) == clsTransactionTypes.LoanPayments || clsTransactionTypes.TransactionStringToTransactionID(cmbTransactionType.Text) == clsTransactionTypes.HpPayment) ToggleLoanControls(true); else if(clsTransactionTypes.TransactionStringToTransactionID(cmbTransactionType.Text) == clsTransactionTypes.TransferToCreditCardCard) ToggleCreditCardControls(true); SetupViews(cmbTransactionType.Text); } private void SetupViews(string transactionTypeSelected) { if (transactionTypeSelected == "Payment to Credit Card") SetupCreditCardsLayouts(); else if(transactionTypeSelected == "HP Payment" || transactionTypeSelected == "Loan Payment") SetupLoanLayouts(); else SetupStandardLayouts(); } private void SetupLoanLayouts() { tableBank1Income.SetCellPosition(panelLoans, tlLnLoanCmb); tableBank1Income.SetCellPosition(lblLoans, tlLnLoanLbl); tableBank1Income.SetCellPosition(lblErrorLoanName, tlLnLoanError); tableBank1Income.SetCellPosition(cmbNumberOfLoanPayments, tlLnLoanPymntsCmb); tableBank1Income.SetCellPosition(lblLoanPayments, tlLnLoanPymntsLbl); tableBank1Income.SetCellPosition(lblErrorLoanPayments, tlLnLoanPymntsError); tableBank1Income.SetCellPosition(txtTransactionGross, tlLngrossTxt); tableBank1Income.SetCellPosition(lblTransactionAmount, tlLngrossLbl); tableBank1Income.SetCellPosition(lblErrorTransactionGross, tlLngrossError); tableBank1Income.SetCellPosition(txtTransactionNarrative, tlLnnarrativeTxt); tableBank1Income.SetCellPosition(lblTransactionNarrative, tlLnnarrativeLbl); tableBank1Income.SetCellPosition(panelCreditCards, tlStdccList); tableBank1Income.SetCellPosition(lblCreditCard, tlStdccLbl); tableBank1Income.SetCellPosition(lblCCError, tlStdccError); } private void SetupStandardLayouts() { tableBank1Income.SetCellPosition(txtTransactionGross, tlStdgrossTxt); tableBank1Income.SetCellPosition(lblTransactionAmount, tlStdgrossLbl); tableBank1Income.SetCellPosition(lblErrorTransactionGross, tlStdgrossError); tableBank1Income.SetCellPosition(txtTransactionNarrative, tlStdnarrativeTxt); tableBank1Income.SetCellPosition(lblTransactionNarrative, tlStdnarrativeLbl); tableBank1Income.SetCellPosition(txtTransactionGross, tlStdgrossTxt); tableBank1Income.SetCellPosition(lblTransactionAmount, tlStdgrossLbl); tableBank1Income.SetCellPosition(lblErrorTransactionGross, tlStdgrossError); tableBank1Income.SetCellPosition(panelLoans, tlStdLoanLbl); tableBank1Income.SetCellPosition(lblLoans, tlStdLoanLbl); tableBank1Income.SetCellPosition(lblErrorLoanName, tlStdLoanError); tableBank1Income.SetCellPosition(cmbNumberOfLoanPayments, tlStdLoanPymntsCmb); tableBank1Income.SetCellPosition(lblLoanPayments, tlStdLoanPymntsLbl); tableBank1Income.SetCellPosition(lblErrorLoanPayments, tlStdLoanPymntsError); tableBank1Income.SetCellPosition(panelCreditCards, tlStdccList); tableBank1Income.SetCellPosition(lblCreditCard, tlStdccLbl); tableBank1Income.SetCellPosition(lblCCError, tlStdccError); lblCCError.Visible = false; lblErrorLoanName.Visible = false; lblErrorLoanPayments.Visible = false; } private void SetupCreditCardsLayouts() { tableBank1Income.SetCellPosition(panelCreditCards, tlCCccList); tableBank1Income.SetCellPosition(lblCreditCard, tlCCccLbl); tableBank1Income.SetCellPosition(lblCCError, tlCCccError); tableBank1Income.SetCellPosition(txtTransactionGross, tlCCgrossTxt); tableBank1Income.SetCellPosition(lblTransactionAmount, tlCCgrossLbl); tableBank1Income.SetCellPosition(lblErrorTransactionGross, tlCCgrossError); tableBank1Income.SetCellPosition(txtTransactionNarrative, tlCCnarrativeTxt); tableBank1Income.SetCellPosition(lblTransactionNarrative, tlCCnarrativeLbl); tableBank1Income.SetCellPosition(panelLoans, tlStdLoanLbl); tableBank1Income.SetCellPosition(lblLoans, tlStdLoanLbl); tableBank1Income.SetCellPosition(lblErrorLoanName, tlStdLoanError); tableBank1Income.SetCellPosition(cmbNumberOfLoanPayments, tlStdLoanPymntsCmb); tableBank1Income.SetCellPosition(lblLoanPayments, tlStdLoanPymntsLbl); tableBank1Income.SetCellPosition(lblErrorLoanPayments, tlStdLoanPymntsError); }
代码通过将不需要的控件移动到表底部并移动所需的控件来工作。这样,表中没有空白。例如我不想只是打开和关闭Loan组合框的可见性,因为那时交易类型和两行的叙述之间会有一个间隙。我总是希望它尽可能紧凑。
当前系统只有问题是底部有空行,包含不可见的控件。添加行也很困难,因为我必须更改所有处理移动内容的代码。
感谢阅读,如果你走得这么远。
有一个> phew<
由于
未列出切换控件方法。它们基本上可以打开/关闭不需要可见性的控件。
答案 0 :(得分:1)
我偶尔会发现自己处于类似情况,并且更喜欢不那么混乱的方法:
要调整表单的布局,您现在只需要设置9个面板的Visible属性。如果Panel被隐藏,表格布局的整行将因AutoSizing行而消失,其他行将向上移动。如果您显示新行,则相同。因此布局是自动化的,您只需要决定要显示哪一行。
此外,TableLayoutPanel现在将缩小到其内容。如果您愿意,您现在还可以使用包含表单的AutoSize *属性来使表单自动调整大小,但要小心避免因此行为而烦扰您的用户。您还可以处理TableLayoutPanel的SizeChanged事件,以根据需要调整表单的大小。
我希望我的描述是可以理解的,否则可以随意询问细节。