How can I set the insert cursor position in wxMaskedTextCtrl

Hello all

I'm new to wxPython programming and my native language is not english, so please forgive me if this is a silly question.

I'm trying to make my controls data-aware using the wxPyValidator class.

I have successfully set up a dialog with a wxMaskedTextCtrl field and a validator that fills in the data from a instance variable. My control (named ctrl_username) is initialized as follows (in the dialog's __init__ method):

        self.username='test'
        self.ctrl_username = wxFindWindowByName('ctrl_username',self) # field name in wxDesigner
        self.ctrl_username.SetFormatcodes('F>')
        self.ctrl_username.SetMask('X{30}')
        self.ctrl_username.SetValidRegex('^\w\S{1,12}')
        self.ctrl_validator=TxtValidator(obj=self, attrName='username') # will become the 'database record/field object' when everything else works...
        self.ctrl_username.SetValidator(self.ctrl_validator)

Here is my TransferTo/FromWindow method for the validator:

   def TransferToWindow(self):
        if self.obj and self.attrName: # stored at __init__ time from parameters
            hwnd=self.GetWindow()
            val = getattr(self.obj, self.attrName)
            dbg("hwnd.__class__=%s" % hwnd.__class__, "val='%s'" % val)
            hwnd.SetValue(val)
        return True
    
    def TransferFromWindow(self):
        if self.obj and self.attrName:
            hwnd=self.GetWindow()
            text = hwnd.GetValue()
            setattr(self.obj, self.attrName, string.atof(text))
        return True

the dbg code correctly puts out wxMaskedTextCtrl as class name and the proper value.

Now here is my problem:

The value is correcty transferred to the control, however, the cursor is positioned at the right end, as if the value was filled with spaces. But this is not the case.

What's the correct way to put the insert cursor at the end of the string?

Also, I have a problem with the TransferFromWindow not being called at all. A small example, e.g. in the wxPyWiki, would help me a lot. I already learned most wxWindows tricks from the demos, but for these data transfer tricks I did not find one.

Thanks for any help.

Regards
--Marcel

Marcel Gsteiger wrote:

I'm new to wxPython programming and my native language is not
english, so please forgive me if this is a silly question. I'm trying
to make my controls data-aware using the wxPyValidator class. I have
successfully set up a dialog with a wxMaskedTextCtrl field and a
validator that fills in the data from a instance variable. My control
(named ctrl_username) is initialized as follows (in the dialog's
__init__ method):

There is NOTE in maskededit.py source:

# wxMaskedEdit controls do not normally use validators, because they do
# careful manipulation of the cursor in the text window on each keystroke,
# and validation is cursor-position specific, so the control intercepts the
# key codes before the validator would fire. However, validators can be
# provided to do data transfer to the controls.

So, personally I decided not use masked controls in my apps ;-(

···

--
Oleg Deribas,
http://copi.ru/14000/

Marcel Gsteiger wrote:

Now here is my problem:

The value is correcty transferred to the control, however, the cursor
is positioned at the right end, as if the value was filled with
spaces. But this is not the case.

What's the correct way to put the insert cursor at the end of the
string?

I'm not sure how this will interact with wxMaskedTextCtrl, but normally you would just call SetInsertionPointEnd().

Also, I have a problem with the TransferFromWindow not being called
at all. A small example, e.g. in the wxPyWiki, would help me a lot. I
already learned most wxWindows tricks from the demos, but for these
data transfer tricks I did not find one.

wx.Window.TransferDataFromWindow is called automatically from a dialog's OK button handler, but for other types of container windows you will need to call it yourself. It will iterate through the child windows and if they have a validator it will call the validator's TransferFromWindow method. If the wxWS_EX_VALIDATE_RECURSIVELY flag is set then the child window's TransferDataFromWindow will also be called.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi Marcel,

Marcel Gsteiger wrote:

Hello all

I'm new to wxPython programming and my native language is not english, so please forgive me if this is a silly question.

I'm trying to make my controls data-aware using the wxPyValidator class.

I have successfully set up a dialog with a wxMaskedTextCtrl field and a validator that fills in the data from a instance variable. My control (named ctrl_username) is initialized as follows (in the dialog's __init__ method):

       self.username='test'
       self.ctrl_username = wxFindWindowByName('ctrl_username',self) # field name in wxDesigner
       self.ctrl_username.SetFormatcodes('F>')
       self.ctrl_username.SetMask('X{30}')
       self.ctrl_username.SetValidRegex('^\w\S{1,12}')
       self.ctrl_validator=TxtValidator(obj=self, attrName='username') # will become the 'database record/field object' when everything else works...
       self.ctrl_username.SetValidator(self.ctrl_validator)

Here is my TransferTo/FromWindow method for the validator:

  def TransferToWindow(self):
       if self.obj and self.attrName: # stored at __init__ time from parameters
           hwnd=self.GetWindow()
           val = getattr(self.obj, self.attrName)
           dbg("hwnd.__class__=%s" % hwnd.__class__, "val='%s'" % val)
           hwnd.SetValue(val)
       return True
      def TransferFromWindow(self):
       if self.obj and self.attrName:
           hwnd=self.GetWindow()
           text = hwnd.GetValue()
           setattr(self.obj, self.attrName, string.atof(text))
       return True

the dbg code correctly puts out wxMaskedTextCtrl as class name and the proper value.

Now here is my problem:

The value is correcty transferred to the control, however, the cursor is positioned at the right end, as if the value was filled with spaces. But this is not the case.

What's the correct way to put the insert cursor at the end of the string?

What you see here is that wxMaskedTextCtrl fills your control with spaces (they are expected to be Fixed Width). I therefore use the formatcode "S" to select all text when one moves to the control, but this might not work for you.

Looking through some older mail exchanges I had with Will Sadkin, I think that the hints I got from him at the time might work for you too.

       ip = len(self.GetValue().rstrip()*)*
        wxCallAfter(self._SetInsertionPoint, ip)
        wxCallAfter(self._SetSelection, ip, ip)

Also, I have a problem with the TransferFromWindow not being called at all. A small example, e.g. in the wxPyWiki, would help me a lot. I already learned most wxWindows tricks from the demos, but for these data transfer tricks I did not find one.

I am doing "panel.InitDialog()", this will call TransferFromWindow for all controls on that panel.

Thanks for any help.

Regards
--Marcel

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

See you
Werner