winforms textbox cursor / caret positioning for data field / file maintenance

Question for Chris Glazier , et al... 

Either I am confused or Managed WinForms COBOL seems to present some unique aspects perhaps not normally found in other .NET or OOP languages, due to the possibility of a mix of COBOL's tradition of fixed-length fields ( example: CONTACT PIC X(30) ) along with the aspect of variable length strings, winforms textbox controls, etc.

Scenario:  A winforms-with-textboxes file maintenance program used to maintain a traditional COBOL Indexed file which has fixed-length fields.

Look at your shareData example program (although it doesn't use a file, it is an example of doing maintenance to fixed length fields via winforms textboxes).

Let's say you had a similar maintenance program using this/these screens to do maintenance against existing records already in the file.  Once you bring the existing record up and show the fields in textboxes, you would be allowed to make changes to the fields (and the record would be rewritten the file, etc.).

Question1:  In this scenario, where would you want the cursor / caret / insertion position to be initially located within the textbox showing the existing data field ???

Answer:  I submit that you would want it to be located to the immediate right of the right-most non-space character.  (as a weak 2nd choice, have it located at the very front of the field in front of the 1st character.

Question 2:  Looking again at shareData and how it currently (and undesirably) behaves in this regard... Is there some known way to do this? (have the cursor / caret / insertion position positioned to the immediate right of the right-most non-space character of the existing field displayed in the textbox)?


  • Verified Answer

    The .NET controls used in a COBOL Winform application are the same controls used in a C# or VB Winforms application as they all use the underlying .NET Framework class assemblies. The behavior in COBOL is no different than the behavior in C#.

    I do not see the demo program that you reference. Can you please attach it?

    You can explicitly set the cursor position within a textbox by using the SelectionStart and SelectionLength properties of the TextBox class.

    The following example will set the cursor position to the end of the field when focus is given to the control.

              set textBox1::Text to "ABCDE"

              set textBox1::SelectionStart to textBox1::Text::Count

              set textBox1::SelectionLength to 0

              invoke textBox1::Focus

    You should probably google the term "winform textbox cursor position" and see what it comes up with as there are a number of different caveats to this depending on how you enter the textbox controls on your form.


  • Verified Answer

    The demo program... It's the wiki article "Share data between multiple Windows Forms programs using EXTERNAL" dated 14 Aug 2012 and it has the attached.  Here's the link:

    First, run this program as is.  The point that I am trying to make is this:

    1. In the file-maintenance-of-existing-data scenario I described earlier, you would probably be initially populating a textbox using the contents of a fixed length field described as say, PIC X(30).  This data would be coming from an existing record on the file.  When you do this, you will be populating textBox1::Text with a value of "ABCDE_________________________" , i.e., all 30 characters.  Assuming you don't do something like you described above, when the textbox is then presented to the user, the cursor / caret / insert position will not be in an optimum or desirable position.

    Similar to your reply above, I came up with the following:

    When initially populating the textbox and just prior to presenting the data to the user, instead of the:

              set txtContact::Text to contact

    you would have something like this:

              set txtContact::Text to contact::TrimEnd(' ')

              set txtContact::SelectionStart to txtContact::Text::Length

              set txtContact::SelectionLength = 0

    which is similar to what you had described.