A Simple Example

Windows Forms User's Guide > Howto: Use Text Fields

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:

private void mnuInsertField_Click(object sender, System.EventArgs e)
    TXTextControl.TextField newField = new TXTextControl.TextField();
    newField.Text = "--------";
    newField.ID = fieldID;
    fieldID += 1;
[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
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:

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;
[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())
End Sub

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

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