Accessing Form Controls from a function

This topic contains 1 reply, has 1 voice, and was last updated by  McSqed 2 weeks, 4 days ago.

  • Author
    Posts
  • #80936

    McSqed
    Participant

    I am trying to write to a textbox from a function. Can you please help me to do that.
    I am not sure how this line should be >>> $Form.Controls.textbox.Text = "test" < <<<

      cls
      Function fnc_Generate_Form {

      Add-Type -AssemblyName System.Windows.Forms
      Add-Type -AssemblyName System.Drawing

      $Form = New-Object system.Windows.Forms.Form
      $Form.StartPosition = "CenterScreen"
      $Form.Topmost = $True
      $Form.size = New-Object System.Drawing.Size(200,200)
      $Form.Text = "DBA Assist"
      $Form.DataBindings.DefaultDataSourceUpdateMode = 0

      $textbox = New-Object System.Windows.Forms.TextBox
      $textbox.Location ='10,10'
      $textbox.Size = '100,100'
      $Form.Controls.Add($textbox)

      $WriteToTextBox = New-Object System.Windows.Forms.Button
      $WriteToTextBox.Location = New-Object System.Drawing.Size(35,100)
      $WriteToTextBox.Size = New-Object System.Drawing.Size(120,23)
      $WriteToTextBox.Text = "Write To TextBox"
      $Form.Controls.Add($WriteToTextBox)

      #Add Button event
      $WriteToTextBox.Add_Click(
      {
      fnc_WriteToTextBox $Form

      }
      )

      $Form.ShowDialog()
      }

      Function fnc_WriteToTextBox
      { Param($Form)

      $Form.Controls.textbox.Text = "test"

      }

      fnc_Generate_Form

  • #80939

    McSqed
    Participant

    Ok, So i have a solution , but hoping someone replies with something better

      Function fnc_WriteToTextBox
      { Param($Form, $textbox)

      #$Form.Controls.textbox.Text = "test"
      $textbox.Text = "test"

      }

You must be logged in to reply to this topic.