移除PowerShell窗体TextBox的字符限制
解决PowerShell WinForms TextBox大段文本截断问题
嘿,我明白你遇到的麻烦了——WinForms的多行TextBox默认有个32767字符的长度限制,这就是你输入大段(比如30页)内容时被截断的原因!
解决方案
给每个多行TextBox设置MaxLength属性为一个足够大的值,比如直接用[int]::MaxValue(这个值足够容纳绝大多数场景的文本输入,除非你要处理极端庞大的内容),或者指定一个具体的大数(比如1048576,对应100万字符)。
修改后的代码示例
我调整了你的代码,给每个多行TextBox添加了MaxLength设置,同时也帮你修正了按钮位置(原来的按钮坐标超出了窗体范围,会显示不出来):
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(900,800) # 放大窗体以容纳所有控件 $form.StartPosition = 'CenterScreen' $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Point(710,730) # 调整按钮到窗体内 $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(790,730) # 调整按钮到窗体内 $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 = [int]::MaxValue # 移除长度限制 $form.Controls.Add($objTextBox1) $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 = [int]::MaxValue # 移除长度限制 $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 = [int]::MaxValue # 移除长度限制 $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 = [int]::MaxValue # 移除长度限制 $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 = [int]::MaxValue # 移除长度限制 $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" }
补充说明
- 默认情况下,多行TextBox的
MaxLength属性值是32767,超过这个长度的输入会被直接截断。 - 设置
[int]::MaxValue相当于移除了实际使用中的长度限制,这个值对于常规的文本输入完全足够。 - 我顺便调整了窗体大小和按钮位置,因为原来的按钮坐标超出了窗体范围,会导致按钮无法显示。
内容的提问来源于stack exchange,提问作者A. Eakins
相关产品推荐
相关产品推荐

