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

标签 .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,而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>
    "

}

最佳答案

根据documentation hereMaxLengthTextBox默认为32767。将MaxLength设置为较大的值可以解决您的问题。

关于.net - 从Powershell表单文本框中删除字符限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50279870/

相关文章:

c# - 快速简单的对象序列化

powershell - 使用 Powershell 的 schtasks 和参数中带有空格的脚本

Powershell 从 URL 列表中提取域

c# - 我怎么知道哪个方法调用了我的方法?

c# - 使用正则表达式查找字符串

xml - 如何使用 Powershell 在 xml 中附加新属性?

powershell - PowerShell 中的特殊字符

git - Azure Web 应用程序部署导致 VS Team Services 中的 PowerShell 失败

.net - 带有标准短语的免费/开源 resx 文件

c# - 将 XML 可序列化公共(public)字段重构为自动属性是否会破坏旧实例的反序列化?