How to create a dialog
In the Site Manager there are two options for you to start with when creating a dialog:
Create the dialog from scratch
For a dialog to be a legal Site Manager dialog it needs to inherit from the dialog base:
public partial class SampleDialog : LSOne.ViewCore.Dialogs.DialogBase
Copy another dialog
The faster way to create a dialog is to copy another dialog from the Site Manager and modify it to fit your needs. The best dialog for you to copy will probably be a dialog that is similar in functionality to the dialog that you need.
In our sample plugin, HelloWorld, we have dialogs that are rather simple and are very convenient to copy. However if your dialog is very different from those and you need a dialog similar to another Site Manager dialog it might be better to simply copy that dialog.
Functions overview
A list of commonly used functions when creating a dialog:
Base functions |
Description |
void CheckEnabled(object sender, EventArgs e) |
Here we check if the requirements are met for the OK button to be enabled. |
void btnOK_Click(object sender, EventArgs e) |
When we press OK, the data in the business objects is altered and you save the instance of the business object to the database. |
void btnCancel_Click |
Closes a dialog and no data is changed. |
protected override IApplicationCallbacks OnGetFramework() |
Needs to return the Site Manager framework instance. This instance is usually kept in the: PluginEntry.Framework. |
Dialog Sample Code
Below we can see examples of each basic function of a dialog.
A dialog has either one or two constructors (one for adding and one for editing). Here we are using the dialog for both adding and editing and we have two constructors.
public SampleDialog(RecordIdentifier dataObjectId)
:this()
{
this.dataObjectId = dataObjectId;
dataObject = Providers.DataObjectData.Get(PluginEntry.DataModel, dataObjectId);
tbDescription.Text = dataObject.Description;
tbExtraInfo.Text = dataObject.ExtraInfo;
}
public SampleDialog()
{
InitializeComponent();
}
protected override IApplicationCallbacks OnGetFramework()
{
return PluginEntry.Framework;
}
The first constructor is used when editing an existing object and in that constructor we fetch the actual data about the existing object and populate fields on the dialog with that information. The second constructor is the default constructor and is used when creating a new object from scratch. The override of the OnGetFramework method should return the Site Manager framework instance.
Here we have the code that saves the new or edited object. This is done when the OK button is pressed.
private void btnOK_Click(object sender, EventArgs e)
{
if (dataObject == null)
{
dataObject = new DataObjectClass();
dataObject.Id = Guid.NewGuid();
}
dataObject.Description = tbDescription.Text;
dataObject.ExtraInfo = tbExtraInfo.Text;
Providers.DataObjectData.Save(PluginEntry.DataModel, dataObject);
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
We start by checking if we are editing or creating a new object. If we are not editing we start by creating a new data object and setting the ID of it to be a new Guid ID. Then we populate the object with data from the dialog.
We then call the Save function of our data access class and provide it with the connection to the database (PluginEntry.DataModel) and the data object.
Lastly we set the corresponding DialogResult close the dialog. If you press the cancel button, the dialog will be closed without any changes being saved.
Here we have the code that determines if the OK button should be enabled. This code is run when something changes in the dialog.
private void CheckEnabled(object sender, EventArgs e)
{
if (dataObject == null) // Creating new
{
btnOK.Enabled = tbDescription.Text.Length > 0 && tbExtraInfo.Text.Length > 0;
}
else // Editing existing
{
btnOK.Enabled = (tbDescription.Text.Length > 0 && tbDescription.Text != dataObject.Description) ||
(tbExtraInfo.Text.Length > 0 && tbExtraInfo.Text != dataObject.ExtraInfo);
}
}
As you can see, first we check if we are adding or editing an item. If we are adding a new item we are checking if the Description and ExtraInfo fields have any text. If we are editing an item, besides the checks that we do when adding a new item we also check if any of the fields are different than the original values.
Now you should exercise in creating a dialog in the second exercise: Lesson 5 - Plugins: views, tabs and dialogs