# 方法及範例

## Dispose

釋放 strucform2 資源

```csharp
// sample code for dispose()

private void RemoveStrucFormFromPanel()
{
    if (strucForm != null)
    {
        if (strucForm.IsEnlarged) // IsEnlarged is a property to check if the form is enlarged
        {
            strucForm.RestoreToOriginalParent();
        }
        panel.Controls.Remove(strucForm);
        strucForm.Dispose();
    }
}  
```

## GetTextAsync

取得病歷表單文字

#### 範例

帶回表單中的純文字內容。

```csharp
// Get the current text from the WebView2 control
    string text = await strucForm.GetTextAsync();
```

## InitAsync

起始表單，參見 [起始方法及參數設定](https://cgmh-kj.gitbook.io/strucmed/his-jiao-huan-zheng-he-gui-ge/strucform-kong-jian-shuo-ming-wen-jian/qi-shi-fang-fa)。

## InsertTemplateAsync

### 插入子表單

表單中的段落輸入框可以支持插入子表單，帶回純文字，除了段落輸入框內建的快捷鍵，調出表單清單選用外，也可利用 InsertTemplateText(option) 呼叫選用子表單，其中 option 為以 '##' 包裹 子表單ID 的文字。

#### 範例

以下範例示範 InsertTemplateAsync ("##FormID##") 帶入預設子表單 FormID

```csharp
// 呼叫 insertTempalteTextAsync 帶入子表單以及表單內容(option)
    bool insertSuccess = await strucForm.InsertTemplateAsync("##781aaca9-7ead-405a-ac29-1050daed47d5##");
```

### 插入文字

可以將純文字範本插入基礎表單段落輸入框的游標位置。

#### 範例

```csharp
// Insert template text at a specific position
    bool insertSuccess = await strucForm.InsertTemplateAsync("Sample text", 5);
```

### 插入子表單/文字組合

使用者也可利用 InsertTemplateAsync ("##FormID##") 動態呼叫快捷表單 FormID 填寫，並將表單的文字結果插入基礎表單段落輸入框的游標位置。

#### 範例

```csharp
// 僅插入子表單範本
    bool insertSuccess = await strucForm.InsertTemplateAsync("##781aaca9-7ead-405a-ac29-1050daed47d5##");

// 插入文字以及子表單組合
    bool insertSuccess = await strucForm.InsertTemplateAsync("more text before form ##781aaca9-7ead-405a-ac29-1050daed47d5## more text after form");

// 插入多個文字以及子表單範本
    bool insertSuccess = await strucForm.InsertTemplateAsync("happy to new textbox \nmoretext\n");
    bool insertSuccess = await strucForm.InsertTemplateAsync("##781aaca9-7ead-405a-ac29-1050daed47d5##");
```

## LoadDocAsync

可依照 Configuration 檔頭指定的 chtno, deptno, opdno, templateID, docType 參數取出最近一次儲存結構化文件

#### 範例

```csharp
// Load the document in the WebView2 control
    bool loadDocSuccess = await strucForm.LoadDocAsync();
```

## MoveToNewWindow

開啟放大視窗，提供更大的結構化輸入畫面。

```csharp
// Move the control to a new window
    strucForm.MoveToNewWindow();
    Console.WriteLine("Moved to New Window");
```

## RestoreToOriginalParent

關閉放大視窗，將結構化控件移回原本的視窗。

```csharp
// Restore the control to its original parent
    strucForm.RestoreToOriginalParent();
    Console.WriteLine("Restored to Original Parent");
```

## RequiredDoneAsync

查詢必填欄位是否完成，完成的話回傳 true

#### 範例

```csharp
private async void SaveSubjective(object sender, EventArgs e)
{
    try
    {
        // 如果必填欄位完成才存檔
        bool isDone = await strucForm.RequiredDoneAsync();
        if (isDone ) {
            bool saveDocSuccess = await strucForm.SaveDocAsync();
            Console.WriteLine($"Save Document: {saveDocSuccess}");
        }        
    }
    catch (Exception ex)
    {
        // Handle any exceptions that may occur during the operation.
        MessageBox.Show($"An error occurred: {ex.Message}");
    }
}
```

## ResetFreeTextFormAsync

清空表單段落文字內容。

#### 範例

```csharp
private async void ResetObjective(object sender, EventArgs e)
{
    try
    {
        // Reset the free text form
        bool resetSuccess = await strucForm.ResetFreeTextFormAsync();
    }
    catch (Exception ex)
    {
        // Handle any exceptions that may occur during the operation.
        MessageBox.Show($"An error occurred: {ex.Message}");
    }
}
```

## SaveDocAsync

儲存文件，可依照 Configuration 檔頭指定的 chtno, deptno, opdno, templateID, docType 參數儲存 xml 以及 key-value 資訊。

```csharp
private async void SaveSubjective(object sender, EventArgs e)
{
    try
    {
        // Check if the document is saved
        bool isDocSaved = await strucForm.DocSavedAsync();
        Console.WriteLine($"Document Saved: {isDocSaved}");
    }
    catch (Exception ex)
    {
        // Handle any exceptions that may occur during the async operation.
        MessageBox.Show($"An error occurred: {ex.Message}");
    }
}
```

## SetTextAsync

設定病歷表單文字

```csharp
// Set some text in the WebView2 control
    bool setTextSuccess = await strucForm.SetTextAsync("This is the new text to set in the WebView2 control.");
    Console.WriteLine($"Set Text Success: {setTextSuccess}");
```
