Events
Im unten gezeigten Codefragment (aus dem Beispiel AutomateStyleVision) wird der Code für zwei Event Handler aufgelistet. Das Beispiel AutomateStyleVision (siehe Datei form1.cs) befindet sich im Unterordner C# des Ordners API Examples:
Windows 7, Windows 8, Windows 10, Windows 11 | C:\Benutzer\<Benutzername>\Dokumente\ |
Sie können das Projekt von Visual Studio 2012/2013/2015/2017/2019/2022 aus kompilieren und ausführen.
Codefragment
delegate void addListBoxItem_delegate(string sText);
// Called from the UI thread
private void addListBoxItem(string sText)
{
listBoxMessages.Items.Add(sText);
}
// Wrapper method to call UI control methods from a worker thread
void syncWithUIthread(Control ctrl, addListBoxItem_delegate methodToInvoke, String sText)
{
// Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
// Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
if (ctrl.InvokeRequired)
ctrl.BeginInvoke(methodToInvoke, new Object[] { sText });
}
// Event handler for OnDocumentClosed event
private void handleOnDocumentClosed(StyleVisionLib.Document i_ipDocument)
{
String sText = "";
if (i_ipDocument.Name.Length > 0)
sText = "Document " + i_ipDocument.Name + " was closed!";
// Synchronize the calling thread with the UI thread because
// COM events are triggered from a working thread
addListBoxItem_delegate methodToInvoke = new addListBoxItem_delegate(addListBoxItem);
// Call syncWithUIthread with the following arguments:
// 1 - listBoxMessages - list box control to display messages from COM events
// 2 - methodToInvoke - a C# delegate which points to the method which will be called from the UI thread
// 3 - sText - the text to be displayed in the list box
syncWithUIthread(listBoxMessages, methodToInvoke, sText);
}
private void updateListBox()
{
// Iterate through all open documents
listBoxMessages.Items.Clear();
for (int i = 1; i <= StyleVision.Documents.Count; i++)
{
StyleVisionLib.Document doc = StyleVision.Documents[i];
if (doc != null)
{
if (checkBoxEventOnOff.Checked)
doc.OnDocumentClosed += new StyleVisionLib._IDocumentEvents_OnDocumentClosedEventHandler(handleOnDocumentClosed);
else
doc.OnDocumentClosed -= new StyleVisionLib._IDocumentEvents_OnDocumentClosedEventHandler(handleOnDocumentClosed);
listBoxMessages.Items.Add(doc.Name);
StyleVisionLib.SchemaSources sources = doc.SchemaSources;
for (int j = 1; j <= sources.Count; j++)
{
StyleVisionLib.SchemaSource source = sources[j];
if (source != null)
{
listBoxMessages.Items.Add("\tSchema file name : " + source.SchemaFileName + "\n");
listBoxMessages.Items.Add("\tWorking XML file name : " + source.WorkingXMLFileName + "\n");
listBoxMessages.Items.Add("\tIs main schema source : " + source.IsMainSchemaSource + "\tType name : " + source.TypeName + "\n");
}
}
}
}
}