Step 3 of 9: Add Code to Update the User Interface
The simple designer has buttons that may be enabled or disabled, depending on whether the clipboard and undo buffer are empty, whether a file is loaded, and so on. All this functionality is implemented in a single method, called UpdateUI.
UpdateUI is called often to make sure the UI reflects the state of the application. The first call should be made in response to the Form_Load event, to initialize the toolbar and form caption. After pasting the following code into the project, remember to set the names of the buttons in the toolbar control to match the ones used in the UpdateUI routine.
Add the following code to update the user interface:
' update UI on startup to show form title and disable clipboard and
' undo/redo buttons
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UpdateUI()
End Sub
Private Sub UpdateUI()
' update caption
_fileName = _appName
If _fileName.Length > 0 Then
_fileName = String.Format("{0} - [{1}]", _appName, _fileName)
If _dirty Then _fileName = _fileName + " *"
End If
' push/release design/preview mode buttons
Dim design As Boolean = _c1rd.Visible AndAlso (Not IsNothing(_c1rd.Report))
_btnDesign.Pushed = design
_btnPreview.Pushed = Not design
' enable/disable buttons
_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#
// update UI on startup to show form title and disable clipboard and
// undo/redo buttons
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;
// enable/disable buttons
_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;
}
Notice how UpdateUI uses the CanCut, CanPaste, CanUndo, and CanRedo properties to enable and disable toolbar buttons.
|