Forms and controls

  • I'm trying to generate a dynamic control from data in SQL Server.

    The number of rows is variable, as is the visibility of the individual radiobuttons.

    criteria1 radiobutton1 radiobutton2 radiobutton3 inputbox

    criteria2 radiobutton1 radiobutton2 radiobutton3 inputbox

    ....

    I've got this working -- except that since all the radiobuttons are on the same control, clicking radiobutton1 on criteria2 turns off any other radiobuttons, on all rows.

    So what I need to do now is, for each row, to put the controls into a ...container control? and then add each container to my form.

    I tried a UserControl and GroupBox, to no avail.

    Bonus marks: assuming I get my container control set up, how do I determine the selected radiobutton? Or do I set a property on the container when then radio button becomes checked?

  • This doesn't sound like a PowerShell question.

    Assuming that I am wrong and this is a PowerShell question, please can you post some more information in particular concrete examples of the data driving this process, concrete examples of the output as well as the script itself (redact it as necessary).

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Hi Gaz,

    I'm doing this in PowerGUI, BTW.

    I guess strictly speaking it's more of a .net / Windows forms question, but I am doing this all in PoSh.

    I've replaced a sproc call w/ a select statement, I think this should work if you sub in a server and db.

    function Make-EMSRadioButton ()

    {

    param ($Criteria, $Type, $Value, $IsVisible, $RequiresComment, $i, $j)

    $rb = New-Object System.Windows.Forms.RadioButton

    $x = 120 + (($i-1) * 60)

    $y = $j * 25

    $rb.Location = New-Object System.Drawing.Point($x,$y)

    $rb.Size = New-Object System.Drawing.Size(44,24)

    $rb | Add-Member -type NoteProperty -name ButtonType -Value $Type

    $rb | Add-Member -type NoteProperty -name CVId -Value $Value

    $rb | Add-Member -type NoteProperty -name RequiresComment -Value $RequiresComment

    $rb.Visible = $IsVisible

    #$rb.add_CheckedChanged({$Required.Criteria[$Criteria].Visible = $false})

    return ($rb)

    }

    clear

    [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

    [void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

    $form1 = New-Object System.Windows.Forms.Form

    $form1.ClientSize = New-Object System.Drawing.Size(900,500)

    $ServerName = "<yourserver>"

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null

    $Srv = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName

    $Query = "SELECTcCriteria = 'Greeting',`

    cInclude_ME = 1,nME_Id = 70, cInclude_RI = 1, nRI_Id = 72, cInclude_DNME = 1, nDNME_Id = 69, cInclude_NA = 0, nNA_Id = 71 `

    UNION`

    SELECT'Language',1, 74, 0, 76, 1, 77, 0, 78 "

    $Data = Invoke-Sqlcmd -ServerInstance $srv -Database "<yourdb>" -Query $Query

    $Types = @()

    $Types += "ME"

    $Types += "RI"

    $Types += "DNME"

    $Types += "NA"

    $j = 0

    foreach ($Datum in $Data)# | ? {$_.cCriteria -eq "Greeting"})

    {

    #$Group = New-Object System.Windows.Forms.groupbox

    #$Group.ClientSize = New-Object System.Drawing.Size(900,25)

    #$Group.Visible = $true

    $j++

    $i = 0

    $x = 10

    $y = $j * 25

    $strCriteria = $Datum.cCriteria

    $Criteria = New-Object System.Windows.Forms.Label

    $Criteria.Text = $strCriteria

    $Criteria.Autosize = $false

    $Criteria.Location = New-Object System.Drawing.Point($x,$y)

    $form1.Controls.Add($Criteria)

    #$Group.Controls.Add($Criteria)

    foreach ($Type in $Types)

    #for($i=1; $i -le 4; $i++)

    {

    $i++

    $strValue = "n" + $Type + "_Id"

    $Value = $datum.$strValue

    $IsVisible = $datum.("cInclude_$Type")

    $RequiresComment = switch ($Type)

    {

    "ME" {"N"}

    default {"Y"}

    }

    $rb = Make-EMSRadioButton $strCriteria $Type $Value $IsVisible $RequiresComment $i $j

    $form1.Controls.Add($rb)

    #$Group.Controls.Add($rb)

    }

    $Required = New-Object System.Windows.Forms.Label

    $Required | Add-Member -type NoteProperty -name Criteria -Value $strCriteria

    $Required.Text = "*"

    $Required.Size = New-Object System.Drawing.Size(20,20)

    $Required.Location = New-Object System.Drawing.Point(345,$y)

    #$Group.Controls.Add($Required)

    $form1.Controls.Add($Required)

    $objTextBox = New-Object System.Windows.Forms.TextBox

    $x = 350

    $objTextBox.Location = New-Object System.Drawing.Size($x,$y)

    $objTextBox.Size = New-Object System.Drawing.Size(260,20)

    $form1.Controls.Add($objTextBox)

    #$Group.Controls.Add($objTextBox)

    $form1.Controls.Add($Group)

    }

    $form1.ShowDialog()

  • The following works so perhaps you can step by step change it to what you are looking for:

    [void][Reflection.Assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

    [void][Reflection.Assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

    $form1 = New-Object System.Windows.Forms.Form

    $form1.ClientSize = New-Object System.Drawing.Size(900,500)

    $group1 = New-Object System.Windows.Forms.GroupBox

    $group1.ClientSize = New-Object System.Drawing.Size(900,50)

    $group1.Location = New-Object System.Drawing.Point(1,1)

    $group1.Visible = $true

    $group2 = New-Object System.Windows.Forms.GroupBox

    $group2.ClientSize = New-Object System.Drawing.Size(900,50)

    $group2.Location = New-Object System.Drawing.Point(1,51)

    $group2.Visible = $true

    $label1 = New-Object System.Windows.Forms.Label

    $label1.Text = "Label 1"

    $label1.Location = New-Object System.Drawing.Point(3,15)

    $label1.Size = New-Object System.Drawing.Size(50,20)

    $group1.Controls.Add($label1)

    $label2 = New-Object System.Windows.Forms.Label

    $label2.Text = "Label 2"

    $label2.Location = New-Object System.Drawing.Point(3,15)

    $label2.Size = New-Object System.Drawing.Size(50,20)

    $group2.Controls.Add($label2)

    for($i=1; $i -le 4; $i++)

    {

    $radioButton1 = New-Object System.Windows.Forms.RadioButton

    $radioButton1.Text = "RB1" + $i.ToString()

    $radioButton1.Location = New-Object System.Drawing.Point(($i * 60),15)

    $radioButton1.Size = New-Object System.Drawing.Size(50,20)

    $radioButton2 = New-Object System.Windows.Forms.RadioButton

    $radioButton2.Text = "RB2" + $i.ToString()

    $radioButton2.Location = New-Object System.Drawing.Point(($i * 60),15)

    $radioButton2.Size = New-Object System.Drawing.Size(50,20)

    $group1.Controls.Add($radioButton1)

    $group2.Controls.Add($radioButton2)

    }

    $form1.Controls.Add($group1)

    $form1.Controls.Add($group2)

    $form1.ShowDialog()

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks Gaz, that's got it!

  • You're welcome. Did you work out what was wrong in the first place?

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Nope. I had apparently stumbled upon the correct control, GroupBox, but had not been able to make it work then, but it's working now...

    P

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply