SKUI - Retrieve text from Textbox when clicking button

sketchup
text
webdialog

#1

I’m trying SKUI and I have created a Window with a Textbox in this way:

    w = SKUI::Window.new
    txt_box = SKUI::Textbox.new( "" )
    txt_box.name = :txt_name
    txt_box.position( 155, 27 )
    txt_box.width = 200
    w.add_control( txt_box )

    label = SKUI::Label.new( 'Label', txt_box )
    label.position( 10, 30 )
    w.add_control( label )

    btn_cancel = SKUI::Button.new( 'Cancel' ) { |control|
      return nil
      control.window.close
    }
    btn_cancel.position( -5, -5 )
    w.add_control( btn_cancel )

    btn_ok = SKUI::Button.new( 'OK' ) { |control|
      control.window.close
    }
    btn_ok.position( -100, -5 )
    w.add_control( btn_ok )

    w.cancel_button = btn_cancel
    w.default_button = btn_ok
    w.show

After that, I type something in the Textbox and I would like to retrieve the string when I click btn_ok, but how?
I know the string should be in txt_box.value, but I don’t know how to update the value of the TextBox when clicking a button. It is always empty, as I set by default.


#2

Hi @Finfa811

Currently, the code closes the window when the user click “OK”. You need to do something in addition to, or intead of closing the window. You can set a default value when creating the text control.

btn_ok = SKUI::Button.new( 'OK' ) { |control|
    # Do something with the value
    puts "value: #{control.value}"
    # Update the value
    control.value = "New Value"
    # Then close the window if needed
    control.window.close
}

In the SketchUp STL Exporter, the Export button does a few things:

  1. Saves the selected options for next time.
  2. Calls the main export method.
  3. Closes the dialog.

#3

Thank you @jim_foltz, I have already figured out how to fix it. Another option is including the following line in the button control:

   x = control.window[:txt_name].value

that retrieves the value of the element by its name. In the same way I’m able to control now more complex structures. I’ll have a look to the STL Exporter too, it looks interesting for future tasks. Thanks.