使用C1ReportDesigner控件 > 步骤三:添加代码以更新用户界面 |
该简单的设计器具有可以切换启用或者禁用状态的按钮,取决于剪贴板以及Undo缓冲区是否为空,是否有文件被加载,等等。所有这些功能在一个单一的方法中实现,称为UpdateUI。
UpdateUI被频繁调用以确保UI真实地反映应用程序的状态。第一次调用应该在响应Form_Load事件的方法中,用来初始化工具栏以及窗体的标题。在将下面的代码粘贴到工程之后,请记得将工具栏控件内的按钮名称匹配UpdateUI例程中使用到的名称。
将下面的代码添加进来以更新用户界面:
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
' 在启动时更新界面,以显示窗体标题以并禁用剪贴板以及 ' 撤销/重做按钮 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load UpdateUI() End Sub Private Sub UpdateUI() ' 更新标题 _fileName = _appName If _fileName.Length > 0 Then _fileName = String.Format("{0} - [{1}]", _appName, _fileName) If _dirty Then _fileName = _fileName + " *" End If ' 按下/释放表示设计或者预览模式的按钮 Dim design As Boolean = _c1rd.Visible AndAlso (Not IsNothing(_c1rd.Report)) _btnDesign.Pushed = design _btnPreview.Pushed = Not design ' 启用/禁用按钮 _btnCut.Enabled = design AndAlso _c1rd.ClipboardHandler.CanCut _btnCopy.Enabled = design AndAlso _c1rd.ClipboardHandler.CanCut _btnPaste.Enabled = design AndAlso _c1rd.ClipboardHandler.CanPaste _btnUndo.Enabled = design AndAlso _c1rd.UndoStack.CanUndo _btnRedo.Enabled = design AndAlso _c1rd.UndoStack.CanRedo Dim reportSelected As Boolean = design AndAlso Not (IsNothing(_list.SelectedItem)) _btnAddReport.Enabled = _c1rd.Visible _btnDelReport.Enabled = reportSelected _btnAddField.Enabled = reportSelected _btnAddLabel.Enabled = reportSelected End Sub |
C#
C# |
拷贝代码
|
---|---|
// 在启动时更新界面,以显示窗体标题以并禁用剪贴板以及 // 撤销/重做按钮 private void Form1_Load(object sender, System.EventArgs e) { UpdateUI(); } private void UpdateUI() { // update caption Text = (_fileName != null && _fileName.Length > 0) ? string.Format("{0} - [{1}] {2}", _appName, _fileName, _dirty? "*": "") : _appName; // push/release design/preview mode buttons bool design = _c1rd.Visible && _c1rd.Report != null; _btnDesign.Pushed = design; _btnPreview.Pushed = !design; // 启用/禁用按钮 _btnCut.Enabled = design && _c1rd.ClipboardHandler.CanCut; _btnCopy.Enabled = design && _c1rd.ClipboardHandler.CanCut; _btnPaste.Enabled = design && _c1rd.ClipboardHandler.CanPaste; _btnUndo.Enabled = design && _c1rd.UndoStack.CanUndo; _btnRedo.Enabled = design && _c1rd.UndoStack.CanRedo; bool reportSelected = design && _list.SelectedItem != null; _btnAddReport.Enabled = _c1rd.Visible; _btnDelReport.Enabled = reportSelected; _btnAddField.Enabled = reportSelected; _btnAddLabel.Enabled = reportSelected; } |
请注意在这里UpdateUI方法是如何通过使用 CaCanCut, CanPaste, CanUndo, 以及CanRedo属性启用或禁用工具栏按钮的。