problem changing text wxStaticText

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

problem changing text wxStaticText

Post by gorf25 » Mon Aug 21, 2017 3:21 pm

i have a panel created with wxStaticText that shows the text,
also a input textbox, a couple radio buttons and a couple toggle buttons,
everything is working good.

The thing i am trying to do is when i hit a toggle button i can change the
text in the panel to a new text phase, i want to write over the old text
I can get the new text to show in the panel. but i can't figure out how
to get it to the proper position in the panel, to over write the old text...
Hopefully this makes sense,
Any suggestions?

thanks gary

User avatar
doublemax
Moderator
Moderator
Posts: 14875
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem changing text wxStaticText

Post by doublemax » Mon Aug 21, 2017 3:34 pm

When creating the wxStaticText, store its pointer in a member variable, e.g. wxStaticText *m_statictext;
Then, in the button event handler you can change it like this:

Code: Select all

m_statictext->SetLabel( "new value" );
m_statictext->Refresh();   // this may not be necessary
Use the source, Luke!

coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: problem changing text wxStaticText

Post by coderrc » Mon Aug 21, 2017 3:51 pm

you might need to add the wxST_NO_AUTORESIZE flag to your static text.
By default, the control will adjust its size to exactly fit to the size of the text when SetLabel is called. If this style flag is given, the control will not change its size (this style is especially useful with controls which also have wxALIGN_RIGHT or CENTER style because otherwise they won't make sense any longer after a call to SetLabel).

gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

Re: problem changing text wxStaticText

Post by gorf25 » Mon Aug 21, 2017 8:20 pm

Thanks for the help i had that working where it changes what was in the text box...
I don't think i explained it clear enough my bad...

local staticText = wx.wxStaticText( panel, wx.wxID_ANY, name_string)
in front of the text box is a string in the panel name_string = "whatever"
then after the name_string is the textbox i am trying to change the name_string.

I tested the code SetLabel but that changed text in the textbox and not the name_string text..

Hopefully this is a little clearer

Thanks gary

User avatar
doublemax
Moderator
Moderator
Posts: 14875
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem changing text wxStaticText

Post by doublemax » Mon Aug 21, 2017 10:00 pm

Hopefully this is a little clearer
No, not really :)

Can you show your code?
Use the source, Luke!

gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

Re: problem changing text wxStaticText

Post by gorf25 » Tue Aug 22, 2017 4:41 pm

i attached the code

Thanks gary
Attachments
my.txt
(6.73 KiB) Downloaded 186 times

User avatar
doublemax
Moderator
Moderator
Posts: 14875
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: problem changing text wxStaticText

Post by doublemax » Tue Aug 22, 2017 5:10 pm

I really know Lua, but i guess i understood it well enough.

In m_XpartLength you store a pointer to the TextCtrl, so when you call SetValue() it will just change the content of the text field.
You also need a variable to store a pointer to the wxStaticText and then use that variable to change the label.

Here are the three functions i changed. I introduced a new variable mXpartLabel. I don't know if this code is syntactically correct, but you should get the idea.

Code: Select all

function Setupinputs()
  --Add all the inputs
  local val

  m_XpartLength, mXpartLabel = AddInputControl("Enter Distanct to travel",nil)
  m_XpartLength:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "X partLengh", "0.000") )
end



function AddInputControl(name_string, width)
  if(width == nil)then
    width = 100
  end
  local textCtrlID = GetNextID()
  local staticText = wx.wxStaticText( panel, wx.wxID_ANY, name_string)
  local textCtrl = wx.wxTextCtrl( panel, textCtrlID, "0.000", wx.wxDefaultPosition, wx.wxSize(width, -1), wx.wxTE_PROCESS_ENTER ,wx.wxTextValidator(wx.wxFILTER_NUMERIC))
  InputsGridSizer:Add( staticText, 0, wx.wxALIGN_CENTER_VERTICAL+wx.wxALL+wx.wxALIGN_RIGHT, 2)
  InputsGridSizer:Add( textCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL+wx.wxALIGN_LEFT, 2)
  return textCtrl, staticText, textCtrlID
end
    
    
function Getunitsper()
  local inst = mc.mcGetInstance();                   --Captures current instance of Mach 4
  local CountsPerUnit = 0;

  --m_XpartLength = NewText("Enter Distanct Travled",nil)
  mXpartLabel:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "DisTravled", "2.000") )

  CountsPerUnit, rc = mc.mcProfileGetDouble(inst,"Motor0", "CountsPerUnit", 2000.0);
  --rc = mc.mcProfileWriteDouble(inst,"Motor0", "CountsPerUnit", 2000.0);--12700.0
  wx.wxMessageBox("Get the units per",
  "About wxLua "..CountsPerUnit,
  wx.wxOK + wx.wxICON_INFORMATION,
  mainframe)
end
Use the source, Luke!

gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

Re: problem changing text wxStaticText

Post by gorf25 » Tue Aug 22, 2017 10:23 pm

when i add this it doesn't change the text and one other strange thing, the messagebox just after this code doesn't display any message but the program doesn't crash just no messagebox
if i change mXpartLabel: to m_Xpartlength it changes the textbox and the messagebox pops up also
but thats not what i want to change its the m_XpartLength, mXpartLabel = AddInputControl("Enter Distance to travel",nil)
I want to change the text "Enter Distance to travel" to a new - text like "Enter Distance to traveled"
then you can enter a keyboard input into the input textbox i don't seem to be having a problem with the input box just changing the phrase's
I can put the new text on the panel but its always in the top left corner doesn't overwrite the old text ...

Code: Select all

 mXpartLabel:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "DisTravled", "2.000") )
Thanks gary

gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

Re: problem changing text wxStaticText

Post by gorf25 » Wed Aug 23, 2017 12:56 am

Thanks for your help i got it working just used
m_XpartLabel:SetLabel("Hello button clicked"); --this worked
it changed the text in the correct position..

Gary

Code: Select all

function Setupinputs()
  --Add all the inputs
  local val
	
	
	
  m_XpartLength, mXpartLabel = AddInputControl("Enter Distanct to travel",nil)
  m_XpartLength:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "X partLengh", "0.000") )
end

function AddInputControl(name_string, width)
  if(width == nil)then
    width = 100
  end
  local textCtrlID = GetNextID()
  local staticText = wx.wxStaticText( panel, wx.wxID_ANY, name_string)
  local textCtrl = wx.wxTextCtrl( panel, textCtrlID, "0.000", wx.wxDefaultPosition, wx.wxSize(width, -1), wx.wxTE_PROCESS_ENTER ,wx.wxTextValidator(wx.wxFILTER_NUMERIC))
  InputsGridSizer:Add( staticText, 0, wx.wxALIGN_CENTER_VERTICAL+wx.wxALL+wx.wxALIGN_RIGHT, 2)
  InputsGridSizer:Add( textCtrl, 0, wx.wxGROW+wx.wxALIGN_CENTER+wx.wxALL+wx.wxALIGN_LEFT, 2)
  return textCtrl, staticText, textCtrlID
end

--wx.wxGetApp():MainLoop()
   
function Getunitsper()
  local inst = mc.mcGetInstance();                   --Captures current instance of Mach 4
  local CountsPerUnit = 0;

   --m_XpartLength = NewText("Enter Distanct Travled",nil)
   -- m_XpartLabel:SetValue( mc.mcProfileGetString(0 , tostring(m_iniName), "DisTravled", "2.000") )

   m_XpartLabel:SetLabel("Hello button clicked");  --this worked
 
 -- CountsPerUnit, rc = mc.mcProfileGetDouble(inst,"Motor0", "CountsPerUnit", 2000.0);
   --rc = mc.mcProfileWriteDouble(inst,"Motor0", "CountsPerUnit", 2000.0);--12700.0
  wx.wxMessageBox("Get the units per",
  "About wxLua "..CountsPerUnit,
  wx.wxOK + wx.wxICON_INFORMATION,
  mainframe)
end

Post Reply