A Simple Example

This first sample program will show you how fields are created and what happens when they are clicked on. The code shown here is contained in the following directories:

The program consists of a form with just one menu item, Insert Field!, with an exclamation mark to say that clicking on this item will cause an immediate action instead of dropping a menu. There are two Text Controls on the form, one of which is used as a normal text window (TextControl1), the other one as a pop-up window (TextControl2).

The following code is executed when the menu item is clicked on:

[C#]
private void mnuInsertField_Click(object sender, System.EventArgs e)
{
    TXTextControl.TextField newField = new TXTextControl.TextField();
    newField.Text = "--------";
    newField.ID = fieldID;
    fieldID += 1;
    textControl1.TextFields.Add(newField);
}
[Visual Basic]
Private Sub mnuInsertField_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuInsertField.Click
    Dim NewField As New TXTextControl.TextField()
    NewField.Text = "--------"
    NewField.ID = FieldID
    FieldID += 1
    TextControl1.TextFields.Add(NewField)
End Sub

This inserts a field at the current caret position and assigns a unique number to the fields ID property. If you move the cursor over the field, Text Control changes the mouse pointer to a hand cursor to indicate that there is something to click on.

If you click on the field, the application receives a TextFieldClicked event, to which it responds by popping up a window which displays the field number.

Only two lines of code are required for this:

[C#]
private void textControl1_TextFieldClicked(object sender, TXTextControl.TextFieldEventArgs e)
{
    // Field has been clicked on, update text of second TX and display it
    textControl2.Text = "Field clicked, ID: " + e.TextField.ID;
    textControl2.BringToFront();
}
[Visual Basic]
Private Sub TextControl1_TextFieldClicked(ByVal sender As System.Object, _
ByVal e As TXTextControl.TextFieldEventArgs) Handles TextControl1.TextFieldClicked
    TextControl2.Text = "Field clicked, ID: " + CStr(e.TextField.ID())
    TextControl2.BringToFront()
End Sub

When the mouse button is released, the text window is moved to the front again:

[C#]
private void textControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    textControl1.BringToFront();
}
[Visual Basic]
Private Sub TextControl1_MouseUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextControl1.MouseUp
    TextControl1.BringToFront()
End Sub

Next >>