从Powershell表单文本框中删除字符限制

时间:2018-05-10 19:05:23

标签: .net powershell

这有点难以解析,并且不能直接回答我的问题。 TextBox maximum amount of characters (it's not MaxLength)

我有一个Powershell表单(现在看起来有点蠢),接受字符串并将它们输出到.txt文件。

运行文件,输入文本,它按照我喜欢的方式输出文件。唯一的麻烦是,大块的文本(假设多达30页)在某个字符限制后被切断。如何从文本字段中删除限制,以便可以输入任意数量的字符?

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'PCIP'
$form.Size = New-Object System.Drawing.Size(800,500)
$form.StartPosition = 'CenterScreen'


$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(710,785)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(785,785)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)


$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(15,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the author affiliations.'
$form.Controls.Add($label)

$objTextBox1 = New-Object System.Windows.Forms.TextBox 
$objTextBox1.Multiline = $True;
$objTextBox1.Location = New-Object System.Drawing.Size(15,50) 
$objTextBox1.Size = New-Object System.Drawing.Size(280,100)
$objTextBox1.Scrollbars = 3#Scrollbars.Vertical
$form.Controls.Add($objTextBox1)
$form.Topmost = $true

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(15,160)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter the section headings.'
$form.Controls.Add($label2)

$objTextBox2 = New-Object System.Windows.Forms.TextBox 
$objTextBox2.Multiline = $True;
$objTextBox2.Location = New-Object System.Drawing.Size(15,190) 
$objTextBox2.Size = New-Object System.Drawing.Size(280,100)
$objTextBox2.Scrollbars = 3#Scrollbars.Vertical
$form.Controls.Add($objTextBox2)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(15,300)
$label3.Size = New-Object System.Drawing.Size(280,20)
$label3.Text = 'Please enter the ISBN.'
$form.Controls.Add($label3)

$objTextBox3 = New-Object System.Windows.Forms.TextBox 
$objTextBox3.Multiline = $True;
$objTextBox3.Location = New-Object System.Drawing.Size(15,330) 
$objTextBox3.Size = New-Object System.Drawing.Size(280,100)
$objTextBox3.Scrollbars = 3#Scrollbars.Vertical
$form.Controls.Add($objTextBox3)

$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(15,440)
$label4.Size = New-Object System.Drawing.Size(280,20)
$label4.Text = 'Please enter the table of contents.'
$form.Controls.Add($label4)

$objTextBox4 = New-Object System.Windows.Forms.TextBox 
$objTextBox4.Multiline = $True;
$objTextBox4.Location = New-Object System.Drawing.Size(15,470) 
$objTextBox4.Size = New-Object System.Drawing.Size(280,100)
$objTextBox4.Scrollbars = 3#Scrollbars.Vertical
$form.Controls.Add($objTextBox4)

$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(15,580)
$label5.Size = New-Object System.Drawing.Size(280,20)
$label5.Text = 'Please enter the shortest section.'
$form.Controls.Add($label5)

$objTextBox5 = New-Object System.Windows.Forms.TextBox 
$objTextBox5.Multiline = $True;
$objTextBox5.Location = New-Object System.Drawing.Size(15,610) 
$objTextBox5.Size = New-Object System.Drawing.Size(280,100)
$objTextBox5.Scrollbars = 3#Scrollbars.Vertical
$form.Controls.Add($objTextBox5)

$form.Add_Shown({$objTextBox1.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $affiliations = $objTextBox1.Text
    $sectionHeadings = $objTextBox2.Text
    $ISBN = $objTextBox3.Text
    $TOC = $objTextBox4.Text
    $shortestSection = $objTextBox5.Text
    New-Item -name PCIP.txt -value "<tp>
    $affiliations 
    </tp>
    <sp> 
    $sectionHeadings
    </sp>
    <cp>

        ISBN-13: $ISBN

        Notice and Disclaimer

        The information in this product (`“Product`”) is provided as a reference for use by licensed medical professionals and no others. It does not and should not be construed as
        any form of medical diagnosis or professional medical advice on any matter. Receipt or use of this Product, in whole or in part, does not constitute or create a doctor-patient,
        therapist-patient, or other healthcare professional relationship between the copyright holders and any recipient. This Product may not reflect the most current medical
        developments, and the copyright holders (individually and jointly), make no claims, promises, or guarantees about accuracy, completeness, or adequacy of the information
        contained in or linked to the Product. The Product is not a substitute for or replacement of professional medical judgment. The copyright holders, and their affiliates, authors,
        contributors, partners, and sponsors, individually and jointly, disclaim all liability or responsibility for any injury and/or damage to persons or property in respect to actions
        taken or not taken based on any and all Product information.
        In the cases where drugs or other chemicals are prescribed, readers are advised to check the Product information currently provided by the manufacturer of each drug to be
        administered to verify the recommended dose, the method and duration of administration, and contraindications. It is the responsibility of the treating physician relying on
        experience and knowledge of the patient to determine dosages and the best treatment for the patient.
        To the maximum extent permitted by applicable law, the copyright holders provide the Product AS IS AND WITH ALL FAULTS, AND HEREBY DISCLAIMS ALL WARRANTIES
        AND CONDITIONS, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES OR CONDITIONS OF
        MERCHANTABILITY, OF FITNESS FOR A PARTICULAR PURPOSE, OF LACK OF VIRUSES, OR ACCURACY OR COMPLETENESS OF RESPONSES, OR RESULTS, AND OF
        LACK OF NEGLIGENCE OR LACK OF WORKMANLIKE EFFORT. ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, QUIET POSSESSION,
        CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT, WITH REGARD TO THE PRODUCT. THE ENTIRE RISK AS TO THE QUALITY OF OR ARISING OUT OF USE OR
        PERFORMANCE OF THE PRODUCT REMAINS WITH THE READER.
        The respective copyright holders, individually and jointly, disclaim all warranties of any kind if the Product was customized, repackaged or altered in any way by any third party.'
    </cp>

    <toc>
    $TOC
    </toc>

    <ch>
    $shortestSection
    </ch>
    "

}

编辑,答案

事实证明,即使文档将其标记为属于TextBoxBase(而不是TextBox,文档不太完整),默认字符限制实际上是〜32k,如海报所示。只需使用TextBox的MaxLength属性设置此值(我将我的限制设置为200000000)即可解决此问题。

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'PCIP'
$form.Size = New-Object System.Drawing.Size(800,500)
$form.StartPosition = 'CenterScreen'


$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(710,785)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(785,785)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)


$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(15,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the author affiliations.'
$form.Controls.Add($label)

$objTextBox1 = New-Object System.Windows.Forms.TextBox 
$objTextBox1.Multiline = $True;
$objTextBox1.Location = New-Object System.Drawing.Size(15,50) 
$objTextBox1.Size = New-Object System.Drawing.Size(280,100)
$objTextBox1.Scrollbars = 3#Scrollbars.Vertical
$objTextBox1.MaxLength = 2000000000
$form.Controls.Add($objTextBox1)
$form.Topmost = $true

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(15,160)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Please enter the section headings.'
$form.Controls.Add($label2)

$objTextBox2 = New-Object System.Windows.Forms.TextBox 
$objTextBox2.Multiline = $True;
$objTextBox2.Location = New-Object System.Drawing.Size(15,190) 
$objTextBox2.Size = New-Object System.Drawing.Size(280,100)
$objTextBox2.Scrollbars = 3#Scrollbars.Vertical
$objTextBox2.MaxLength = 2000000000
$form.Controls.Add($objTextBox2)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(15,300)
$label3.Size = New-Object System.Drawing.Size(280,20)
$label3.Text = 'Please enter the ISBN.'
$form.Controls.Add($label3)

$objTextBox3 = New-Object System.Windows.Forms.TextBox 
$objTextBox3.Multiline = $True;
$objTextBox3.Location = New-Object System.Drawing.Size(15,330) 
$objTextBox3.Size = New-Object System.Drawing.Size(280,100)
$objTextBox3.Scrollbars = 3#Scrollbars.Vertical
$objTextBox3.MaxLength = 2000000000
$form.Controls.Add($objTextBox3)

$label4 = New-Object System.Windows.Forms.Label
$label4.Location = New-Object System.Drawing.Point(15,440)
$label4.Size = New-Object System.Drawing.Size(280,20)
$label4.Text = 'Please enter the table of contents.'
$form.Controls.Add($label4)

$objTextBox4 = New-Object System.Windows.Forms.TextBox 
$objTextBox4.Multiline = $True;
$objTextBox4.Location = New-Object System.Drawing.Size(15,470) 
$objTextBox4.Size = New-Object System.Drawing.Size(280,100)
$objTextBox4.Scrollbars = 3#Scrollbars.Vertical
$objTextBox4.MaxLength = 2000000000
$form.Controls.Add($objTextBox4)

$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(15,580)
$label5.Size = New-Object System.Drawing.Size(280,20)
$label5.Text = 'Please enter the shortest section.'
$form.Controls.Add($label5)

$objTextBox5 = New-Object System.Windows.Forms.TextBox 
$objTextBox5.Multiline = $True;
$objTextBox5.Location = New-Object System.Drawing.Size(15,610) 
$objTextBox5.Size = New-Object System.Drawing.Size(280,100)
$objTextBox5.Scrollbars = 3#Scrollbars.Vertical
$objTextBox5.MaxLength = 2000000000
$form.Controls.Add($objTextBox5)

$form.Add_Shown({$objTextBox1.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $affiliations = $objTextBox1.Text
    $sectionHeadings = $objTextBox2.Text
    $ISBN = $objTextBox3.Text
    $TOC = $objTextBox4.Text
    $shortestSection = $objTextBox5.Text
    New-Item -name PCIP.txt -value "<tp>
    $affiliations 
    </tp>
    <sp> 
    $sectionHeadings
    </sp>
    <cp>

        ISBN-13: $ISBN

        Notice and Disclaimer

        The information in this product (`“Product`”) is provided as a reference for use by licensed medical professionals and no others. It does not and should not be construed as
        any form of medical diagnosis or professional medical advice on any matter. Receipt or use of this Product, in whole or in part, does not constitute or create a doctor-patient,
        therapist-patient, or other healthcare professional relationship between the copyright holders and any recipient. This Product may not reflect the most current medical
        developments, and the copyright holders (individually and jointly), make no claims, promises, or guarantees about accuracy, completeness, or adequacy of the information
        contained in or linked to the Product. The Product is not a substitute for or replacement of professional medical judgment. The copyright holders, and their affiliates, authors,
        contributors, partners, and sponsors, individually and jointly, disclaim all liability or responsibility for any injury and/or damage to persons or property in respect to actions
        taken or not taken based on any and all Product information.
        In the cases where drugs or other chemicals are prescribed, readers are advised to check the Product information currently provided by the manufacturer of each drug to be
        administered to verify the recommended dose, the method and duration of administration, and contraindications. It is the responsibility of the treating physician relying on
        experience and knowledge of the patient to determine dosages and the best treatment for the patient.
        To the maximum extent permitted by applicable law, the copyright holders provide the Product AS IS AND WITH ALL FAULTS, AND HEREBY DISCLAIMS ALL WARRANTIES
        AND CONDITIONS, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES OR CONDITIONS OF
        MERCHANTABILITY, OF FITNESS FOR A PARTICULAR PURPOSE, OF LACK OF VIRUSES, OR ACCURACY OR COMPLETENESS OF RESPONSES, OR RESULTS, AND OF
        LACK OF NEGLIGENCE OR LACK OF WORKMANLIKE EFFORT. ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, QUIET POSSESSION,
        CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT, WITH REGARD TO THE PRODUCT. THE ENTIRE RISK AS TO THE QUALITY OF OR ARISING OUT OF USE OR
        PERFORMANCE OF THE PRODUCT REMAINS WITH THE READER.
        The respective copyright holders, individually and jointly, disclaim all warranties of any kind if the Product was customized, repackaged or altered in any way by any third party.'
    </cp>

    <toc>
    $TOC
    </toc>

    <ch>
    $shortestSection
    </ch>
    "

}

1 个答案:

答案 0 :(得分:4)

根据documentation hereMaxLength的{​​{1}}默认为32767.将TextBox设置为更大的值可以解决您的问题。