Konfiguration laden

Das nun ausführbare Projekt zeigt beim Start eine graue Fläche, da noch keine Konfiguration geladen oder erstellt ist. Prinzipiell kann eine Konfiguration programmatisch erstellt oder eine mit dem TwinCAT Scope erstellte Datei geladen werden. In diesem Fall wird bei einem Klick auf den Button Load eine bereits vorhandene Konfiguration aus einer Datei geladen.

Das C#-Codebeispiel zeigt den Aufruf aus einem „ButtonClick-EventHandler“:

private string filename = @"ScopeTestChart.tcscopex";
private void btnLoad_Click(object sender, EventArgs e)
{
  FileInfo fInfo = new FileInfo(filename);
  if (!fInfo.Exists)
  {
    MessageBox.Show("File not found! Please use the Add Chart button to create a config! Once a config is created and saved it can be load using the Load button!", "File not found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }
  else
  {
    //delete old configuration
    scopeProjectPanel.ScopeProject.Dispose();

    //load configuration
    ScopeProject Project = ScopeProject.LoadScopeProject(filename);
    scopeProjectPanel.ScopeProject = Project;
  }
}

In diesem Beispiel wird die Verwendung eines ScopeProjectPanels gezeigt. Wenn jedoch nur ein Chart angezeigt werden soll ist die Funktionalität dieselbe, nur, dass ein ChartPanel auf der Oberfläche erstellt werden muss anstelle des ScopeProjectPanels. Außerdem besitzt die ChartPanel-Klasse kein ScopeProject-Objekt, sondern nur ein Chart-Objekt. Daher muss anstelle dieser Zeile:

scopeProjectPanel.ScopeProject = Project;

folgender Code erstellt werden:

chartPanel.ModelChart = Project.SubMember.OfType<Chart>().First();