Показать полную графическую версию : [решено] Подсчет символов в поле $TextBox.Text и вывод полученного значения в $label.Text
есть Voice_config.ps1, содержащий форму.
В ней textbox:
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Width = 628
$TextBox.Height = 480
$TextBox.AutoSize = $true
$TextBox.Location = New-Object System.Drawing.Point(25,160)
$TextBox.Multiline = $True;
$TextBox.Text = Get-Clipboard -Format Text
$TextBox.Add_KeyDown({
if ($_.Modifiers -eq [System.Windows.Forms.Keys]::Control -and $_.KeyCode -eq [System.Windows.Forms.Keys]::A) {
$TextBox.SelectAll()
}
})
$main_form.Controls.Add($TextBox)
нужно получать текущее значение количества символов непосредственно из поля TextBox и выводить значение в $label.Text в режиме реального времени.
Пример как должно быть:
Скопировал в буфер "text", запускаю Voice_config.ps1, в TextBox автоматом отображается "text", количество символов отображает "4" (в любом $label.Text в форме)
Далее в поле TextBox дописываю руками "one more". В поле TextBox отображается уже "text one more", количество символов отображает уже "10" (в любом $label.Text в форме).
Foreigner
19-12-2020, 13:27
$main_form »
Это откуда и куда?
Foreigner, это главная форма, в которой textbox находится
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='google text-to-speech'
$main_form.Width = 700
$main_form.Height = 700
$main_form.AutoSize = $true
она оединственная, просто назвал не $form а $main_form
Foreigner
19-12-2020, 14:03
Приведите весь код этой формы. У меня она не реагирует вообще:
using assembly System.Windows.Forms
$text = Get-Clipboard | Out-String
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='google text-to-speech' + ": $($text.Length)"
$main_form.Width = 700
$main_form.Height = 400
$main_form.AutoSize = $true
$main_form.Visible = $true
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Width = 628
$TextBox.Height = 350
$TextBox.AutoSize = $true
$TextBox.Location = New-Object System.Drawing.Point(25,160)
$TextBox.Multiline = $True;
$TextBox.Text = $text
$TextBox.Add_KeyDown({
if ($_.Modifiers -eq [System.Windows.Forms.Keys]::Control -and $_.KeyCode -eq [System.Windows.Forms.Keys]::A) {
$TextBox.SelectAll()
}
})
$main_form.Controls.Add($TextBox)
Foreigner,
Add-Type -assembly System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$p=$MyInvocation.MyCommand.Path | Split-Path -Parent
$s=$p.TrimEnd("\bin")
$json = Get-Content $p\text.json | ConvertFrom-Json
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='google text-to-speech'
$main_form.Width = 700
$main_form.Height = 700
$main_form.AutoSize = $true
$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(300, 135)
$label5.Size = New-Object System.Drawing.Size(200, 35)
$label5.Text = "Кол-во символов:
Максимум - 9000"
$main_form.Controls.Add($label5)
$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(23, 135)
$label3.Size = New-Object System.Drawing.Size(98, 23)
$label3.Text = "Ввод Текста:"
$main_form.Controls.Add($label3)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Width = 628
$TextBox.Height = 460
$TextBox.AutoSize = $true
$TextBox.Location = New-Object System.Drawing.Point(25,180)
$TextBox.Multiline = $True;
$TextBox.Text = Get-Clipboard -Format Text
$TextBox.Add_KeyDown({
if ($_.Modifiers -eq [System.Windows.Forms.Keys]::Control -and $_.KeyCode -eq [System.Windows.Forms.Keys]::A) {
$TextBox.SelectAll()
}
})
$main_form.Controls.Add($TextBox)
$button = New-Object System.Windows.Forms.Button
$button.Text = 'Start'
$button.Location = New-Object System.Drawing.Point(470,54)
$button.add_Click({Click_Button});
$main_form.Controls.Add($button)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(40, 32)
$label.Size = New-Object System.Drawing.Size(98, 23)
$label.Text = "Выбор голоса:"
$main_form.Controls.Add($label)
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Items.Insert(0, "ru-RU-Wavenet-A");
$comboBox.Items.Insert(1, "ru-RU-Wavenet-B");
$comboBox.Items.Insert(2, "ru-RU-Wavenet-C");
$comboBox.Items.Insert(3, "ru-RU-Wavenet-D");
$comboBox.Items.Insert(4, "ru-RU-Wavenet-E");
$comboBox.SelectedIndex = "1"
$comboBox.Location = New-Object System.Drawing.Point(25, 55)
$comboBox.Size = New-Object System.Drawing.Size(120, 310)
$main_form.Controls.Add($comboBox)
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(205, 32)
$label1.Size = New-Object System.Drawing.Size(98, 23)
$label1.Text = "Скорость:"
$main_form.Controls.Add($label1)
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.Items.Insert(0, 0.25)
$comboBox1.Items.Insert(1, 0.5)
$comboBox1.Items.Insert(2, 0.75)
$comboBox1.Items.Insert(3, 1)
$comboBox1.Items.Insert(4, 1.1)
$comboBox1.Items.Insert(5, 1.5)
$comboBox1.Items.Insert(6, 1.75)
$comboBox1.Items.Insert(7, 2)
$comboBox1.Items.Insert(8, 2.25)
$comboBox1.Items.Insert(9, 2.5)
$comboBox1.Items.Insert(10, 2.75)
$comboBox1.Items.Insert(11, 3)
$comboBox1.Items.Insert(12, 3.5)
$comboBox1.Items.Insert(13, 3.75)
$comboBox1.Items.Insert(14, 4)
$comboBox1.SelectedIndex = "3"
$comboBox1.Location = New-Object System.Drawing.Point(175, 55)
$comboBox1.Size = New-Object System.Drawing.Size(120, 310)
$main_form.Controls.Add($comboBox1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(340, 32)
$label2.Size = New-Object System.Drawing.Size(98, 23)
$label2.Text = "Тональность:"
$main_form.Controls.Add($label2)
$comboBox2 = New-Object System.Windows.Forms.ComboBox
$comboBox2.Items.Insert(0, -5);
$comboBox2.Items.Insert(1, -4);
$comboBox2.Items.Insert(2, -3);
$comboBox2.Items.Insert(3, -2);
$comboBox2.Items.Insert(4, -1);
$comboBox2.Items.Insert(5, 0);
$comboBox2.Items.Insert(6, 1);
$comboBox2.Items.Insert(7, 2);
$comboBox2.Items.Insert(8, 3);
$comboBox2.Items.Insert(9, 4);
$comboBox2.Items.Insert(10, 5);
$comboBox2.SelectedIndex = "5"
$comboBox2.Location = New-Object System.Drawing.Point(325, 55)
$comboBox2.Size = New-Object System.Drawing.Size(120, 310)
$main_form.Controls.Add($comboBox2)
function Click_Button
{
$json.input.text = $TextBox.Text;
$json.voice.name = $comboBox.SelectedItem;
$json.audioConfig.pitch = $comboBox2.SelectedItem;
$json.audioConfig.speakingRate = $comboBox1.SelectedItem;
$json | ConvertTo-Json | Set-Content $p\text.json;
$file = gc $p/text.json
$enc = [system.text.encoding]
function psiconv ( $f, $t, $string ) {
$cp1 = $enc::getencoding( $f )
$cp2 = $enc::getencoding( $t )
$inputbytes = $enc::convert( $cp1, $cp2, $cp2.getbytes( $string ))
$outputstring = $cp2.getstring( $inputbytes )
$outputstring | add-content $p/text1.json
}
foreach ($string in $file) {
psiconv -f "windows-1251" -t "utf-8" $string
}
del $p/text.json
Rename-Item -path $p/text1.json -NewName $p/text.json
del $p/source.txt
del $p/out.txt
$main_form.Close()
.\params.ps1
}
$main_form.ShowDialog()
в данном случае, вывод нужен в $Label5.Text
вывод нужен в $Label5.Text »
Add-Type -assembly System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$p=$MyInvocation.MyCommand.Path | Split-Path -Parent
$s=$p.TrimEnd("\bin")
$json = Get-Content $p\text.json | ConvertFrom-Json
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='google text-to-speech'
$main_form.Width = 700
$main_form.Height = 700
$main_form.AutoSize = $true
$label5 = New-Object System.Windows.Forms.Label
$label5.Location = New-Object System.Drawing.Point(300, 135)
$label5.Size = New-Object System.Drawing.Size(200, 35)
$label5.Text = "Кол-во символов(максимум - 9000):"
$label5.AutoSize = $true
$main_form.Controls.Add($label5)
$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(23, 135)
$label3.Size = New-Object System.Drawing.Size(98, 23)
$label3.Text = "Ввод Текста:"
$main_form.Controls.Add($label3)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Width = 628
$TextBox.Height = 460
$TextBox.AutoSize = $true
$TextBox.Location = New-Object System.Drawing.Point(25,180)
$TextBox.Multiline = $True;
$TextBox.Text = Get-Clipboard -Format Text
$TextBox.Add_KeyDown({
if ($_.Modifiers -eq [System.Windows.Forms.Keys]::Control -and $_.KeyCode -eq [System.Windows.Forms.Keys]::A) {
$TextBox.SelectAll()
}
})
$main_form.Controls.Add($TextBox)
$button = New-Object System.Windows.Forms.Button
$button.Text = 'Start'
$button.Location = New-Object System.Drawing.Point(470,54)
$button.add_Click({Click_Button});
$main_form.Controls.Add($button)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(40, 32)
$label.Size = New-Object System.Drawing.Size(98, 23)
$label.Text = "Выбор голоса:"
$main_form.Controls.Add($label)
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Items.Insert(0, "ru-RU-Wavenet-A");
$comboBox.Items.Insert(1, "ru-RU-Wavenet-B");
$comboBox.Items.Insert(2, "ru-RU-Wavenet-C");
$comboBox.Items.Insert(3, "ru-RU-Wavenet-D");
$comboBox.Items.Insert(4, "ru-RU-Wavenet-E");
$comboBox.SelectedIndex = "1"
$comboBox.Location = New-Object System.Drawing.Point(25, 55)
$comboBox.Size = New-Object System.Drawing.Size(120, 310)
$main_form.Controls.Add($comboBox)
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(205, 32)
$label1.Size = New-Object System.Drawing.Size(98, 23)
$label1.Text = "Скорость:"
$main_form.Controls.Add($label1)
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.Items.Insert(0, 0.25)
$comboBox1.Items.Insert(1, 0.5)
$comboBox1.Items.Insert(2, 0.75)
$comboBox1.Items.Insert(3, 1)
$comboBox1.Items.Insert(4, 1.1)
$comboBox1.Items.Insert(5, 1.5)
$comboBox1.Items.Insert(6, 1.75)
$comboBox1.Items.Insert(7, 2)
$comboBox1.Items.Insert(8, 2.25)
$comboBox1.Items.Insert(9, 2.5)
$comboBox1.Items.Insert(10, 2.75)
$comboBox1.Items.Insert(11, 3)
$comboBox1.Items.Insert(12, 3.5)
$comboBox1.Items.Insert(13, 3.75)
$comboBox1.Items.Insert(14, 4)
$comboBox1.SelectedIndex = "3"
$comboBox1.Location = New-Object System.Drawing.Point(175, 55)
$comboBox1.Size = New-Object System.Drawing.Size(120, 310)
$main_form.Controls.Add($comboBox1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(340, 32)
$label2.Size = New-Object System.Drawing.Size(98, 23)
$label2.Text = "Тональность:"
$main_form.Controls.Add($label2)
$comboBox2 = New-Object System.Windows.Forms.ComboBox
$comboBox2.Items.Insert(0, -5);
$comboBox2.Items.Insert(1, -4);
$comboBox2.Items.Insert(2, -3);
$comboBox2.Items.Insert(3, -2);
$comboBox2.Items.Insert(4, -1);
$comboBox2.Items.Insert(5, 0);
$comboBox2.Items.Insert(6, 1);
$comboBox2.Items.Insert(7, 2);
$comboBox2.Items.Insert(8, 3);
$comboBox2.Items.Insert(9, 4);
$comboBox2.Items.Insert(10, 5);
$comboBox2.SelectedIndex = "5"
$comboBox2.Location = New-Object System.Drawing.Point(325, 55)
$comboBox2.Size = New-Object System.Drawing.Size(120, 310)
$main_form.Controls.Add($comboBox2)
$timer = [Windows.Forms.Timer]::new()
$timer.Interval = 64
$timer.add_tick({
$Label5.Text="Кол-во символов(максимум - 9000):`n"+$TextBox.Text.Length
if($TextBox.Text.Length-gt9000){$Label5.ForeColor='red'}
else{$Label5.ForeColor='ControlText'}
})
$Timer.Start()
function Click_Button
{
$json.input.text = $TextBox.Text;
$json.voice.name = $comboBox.SelectedItem;
$json.audioConfig.pitch = $comboBox2.SelectedItem;
$json.audioConfig.speakingRate = $comboBox1.SelectedItem;
$json | ConvertTo-Json | Set-Content $p\text.json;
$file = gc $p/text.json
$enc = [system.text.encoding]
function psiconv ( $f, $t, $string ) {
$cp1 = $enc::getencoding( $f )
$cp2 = $enc::getencoding( $t )
$inputbytes = $enc::convert( $cp1, $cp2, $cp2.getbytes( $string ))
$outputstring = $cp2.getstring( $inputbytes )
$outputstring | add-content $p/text1.json
}
foreach ($string in $file) {
psiconv -f "windows-1251" -t "utf-8" $string
}
del $p/text.json
Rename-Item -path $p/text1.json -NewName $p/text.json
del $p/source.txt
del $p/out.txt
$main_form.Close();.\params.ps1
}
$main_form.ShowDialog()
Fors1k, правильнее будет здесь:
TextBoxBase.TextChanged Event (System.Windows.Controls.Primitives) | Microsoft Docs (https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.textboxbase.textchanged?view=net-5.0)
How to: Detect When Text in a TextBox Has Changed - WPF .NET Framework | Microsoft Docs (https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/how-to-detect-when-text-in-a-textbox-has-changed?view=netframeworkdesktop-4.8)
$timer = [Windows.Forms.Timer]::new()
$timer.Interval = 64
$timer.add_tick({
$Label5.Text="Кол-во символов(максимум - 9000):`n"+$TextBox.Text.Length
if($TextBox.Text.Length-gt9000){$Label5.ForeColor='red'}
else{$Label5.ForeColor='ControlText'}
})
$Timer.Start() »
Спасибо, это работает.
Отдельная благодарность за выделение красным при превышении.
© OSzone.net 2001-2012
vBulletin v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.