Is It possible that if I create two TextBoxes.
When the first TextBox is modified from input, the second text box is set to be read only and its value will update depending on what you had written in the first text box.
It's like when I am posting here in stackoverflow there is also a read only area that follows what I'm typing (The preview window). :)) Thanks!!!
If it's win-form application, it's so simple. try this :
private void txtFirstTextBox_TextChanged(object sender, EventArgs e) {
if (string.IsNullOrEmpty(txtFirstTextBox.Text)) {
txtSecondTextBox.Clear();
return;
}
txtSecondTextBox.Text = txtFirstTextBox.Text;
}
hope this help.
I should note: This is a solution if you're using WPF for your UI.
Yes that's easily possible if you have, for example the first textbox:
<TextBox x:Name="FirstBox"/>
You can bind to this text box's content via:
<TextBox x:Name"SecondBox" Text="{Binding ElementName="FirstBox", Path="Text", UpdateSourceTrigger=PropertyChanged}" IsEnabled="False"/>
And when the first text box changes, the second one should follow suit. This is all handled automatically for you via binding, it connects to the Text property on the TextBox named "FirstBox". This second TextBox is disabled by setting the IsEnabled property to "False"
Since there is already a WPF Solution and you didn't specify which you are using, I'll go ahead and post a WinForms solution.
Luckily, this is relatively simple in WinForms as well. You simply wire a TextChanged event handler for the first text box which updates the text of the second:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = ((TextBox)sender).Text;
}
Related
I am 100% sure I am doing this the wrong way and this problem is "by design".
I want to have a Slider and a TextBox that displays its value. The user can either use the Slider, or manually enter a number in the TextBox.
I also wanted to take advantage of the TextChanging event to ignore any non-numerical entries. TextChanged would only function after a user has entered something and it's not a preferable scenario, and KeyDown would not capture other methods of input like ink or speech.
So I have this:
<StackPanel>
<Slider x:Name="Size" Value="100" Maximum="100" Minimum="0" />
<TextBox x:Name="SizeText" Text="{Binding ElementName=Size,Path=Value,Mode=TwoWay}" TextChanging="SizeText_TextChanging" />
</StackPanel>
Where "SizeText_TextChanging" is simply an empty block of code right now:
private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
// Nothing Here.
}
This code builds, but at startup the app throws a JIT unhandled win32 exception and closes.
Changing TextChanging to TextChanged works fine, but again I prefer to get TextChanging to work (or something similar) to give a better user experience.
"Mode" also has no effect. I tried all three different Modes, all crash. By removing the binding altogether and giving the Text property any value works fine.
I also thought that maybe having the TextChanging event handler empty is the problem, so I borrowed the code below from here but the app still crashes:
private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (!Regex.IsMatch(sender.Text, "^\\d*\\.?\\d*$") && sender.Text != "")
{
int pos = sender.SelectionStart - 1;
sender.Text = sender.Text.Remove(pos, 1);
sender.SelectionStart = pos;
}
}
Like I said, I am probably approaching this the wrong way. I am just starting to learn UWP and C# so I am a total noob. But I have read everything I could about TextChanging and it simply talks about rendering the value and the associated cautions of what not to write within the TextChanging event. So while it sounds like the app is being thrown into a loop trying to read the value of the slider and trying to see what the TextChanging event says, I don't see how to fix it. Please help!
Thank you
I don't know why this is happening, but a workaround is to register the TextChanging event handler only once SizeText (or the page) has loaded and using x:Bind instead of Binding:
XAML
<TextBox x:Name="SizeText" Text="{x:Bind Size.Value, Mode=TwoWay}" Loaded="SizeText_Loaded"/>
CS
private void SizeText_Loaded(object sender, RoutedEventArgs e)
{
SizeText.TextChanging += SizeText_TextChanging;
}
private void SizeText_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
}
I meet the same problem but this solution doesn't works for me.
As #Mushriq, I use _TextChanging() to ignore any non-numerical entries in a form.
But my form contains a lot of numeric fields and also a master-detail part, which contains 2 of these fields.
The problem is that I enter in the _Loaded the first time that I display a part, but not for the other parts. When I display existing parts there is no problem, but if I add a new part I get the exception.
Is there a way to adapt the solution to my case?
Can i make ListView or ListBox editable by user?
For example: user can add a new item at list (without any buttons).
Can i do that? Maybe give some simple example.
P.S. It is about WPF.
In winforms this is simple with a ComboBox. The Text is added if it is new when the user presses Enter:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
if (!comboBox1.Items.Contains(comboBox1.Text))
comboBox1.Items.Add(comboBox1.Text);
}
There was no WPF tag at first, but the same should be possible in WPF as well..
(Make it editable, set the dropdown to visible and catch the enter key..can't provide code atm)
Update: After a rather quick check it seems WPF can't do it out of the box. I'm (somewhat) surprised that a useful control (an editable listbox) that has bee with Windows since the 90s (at least) is no longer there. But maybe I'm wrong..
There is no suitable method to do this trick without buttons. Try DataGridView instead http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx
I need to make password box as non-editable in wpf.
I used
IsEnabled = false
But it is affecting my style some blur effect came because of that...
Is there any other way to achieve this ?
I know this is two years old, but I had the same need and solved it this way:
The combination of these two properties, both set to false, will prevent entry/editing in the control, w/o affecting your desired style:
Focusable, IsHitTestVisible
You can handle the PreviewTextInput event, preventing the user from entering text. Like so:
Xaml:
<PasswordBox PreviewTextInput="HandleInput"/>
Codebehind:
private void HandleInput(object sender, TextCompositionEventArgs e) {
e.Handled = true;
}
One solution is to make a custom functionality to mimic the IsReadOnly.
There are couple things to take care of - e.g. clipboard pasting
also.
You'll get similar behavior by defining some attached property (e.g. IsPasswordReadOnly or just the same) - which would work out all that's required.
Here is a good starting example - which could, should I think work for Password box as well - but I haven't tried it and you gotta test yourself.
Readonly textbox for WPF with visible cursor (.NET 3.5)
You'd have to replace references to TextBox with PasswordBox, rename it to IsReadOnly - and I think the rest might work the same.
And you use it like...
<PasswordBox my:AttachReadOnly.IsReadOnly="True" />
Pretty simple..
Set an event handler for PreviewTextInput in your XML like so:
PreviewTextInput="PasswordBoxOnPreviewTextInput"
And within that method:
private void PasswordBoxOnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (m_DisablePasswordBox)
e.Handled = true;
}
It will now prevent you from typing anything in :)
Is there a way to make a label automatically update itself so that I don't have to use a button to send out the command. What I have setup is a subtotal textbox, discount textbox, tax label, shipping textbox, and total label. So, when people fill in the subtotal, discount, and shipping, I want the tax label to be calculated, but only if previously a certain state was selected in another part of the form. So then, with all those filled in, I want the total label to be filled in. All of these I know I can do with a button, but I was wondering if there is a way to automate it using C# in Visual Studio.
Thanks.
I use the TextChanged Event to update such values between pairs of textboxes. Here are some extracts of my code:
private void onLongitudeTextChanged(object sender, EventArgs e) {
updateDistanceAndBearing();
}
updateDistanceAndBearing does some error checking - this can be a good idea if the user can put invalid values in and then updates the Text property of the other TextBoxes
I have text boxes but update the label.Text property instead.
It gets more messy (at least I found it so) if you have numeric updowns to get values
You can call a method to update the label in the change events for the controls.
For more detail, please supply more detail.
this is off the top of my head but should get you pretty close...
private void taxChanged(object sender, EventArgs e)
{
updateTax();
}
private void updateTax()
{
// the rest of your logic, checking state, etc.
//
this.Tax.Text = aValueCalculatedInYourLogicAbove;
updateTotal()
}
private void updateTotal()
{
// sum up whatever fields need to be summed
//
this.Tax.Text = aTotalValueCalculatedAbove;
}
I am making a program in c sharp/xaml. I have a save button, and figured the easiest way to make it effective was when pressed if I could send the "control s" signal. What is the command (and any includes VS wouldn't add as standard) to do so?
A completely different question to cut down on thread count, how would I make a textblock (or textbox if easier) automatically newline when you reach the end rather than continuing to send text offscreen.
There are better patterns that I would suggest looking into before coupling your UI to keypresses (such as Commands) but if you really want to do this, you can use the SendKeys class from windows forms. This will allow you to send key presses to the application as if the user pressed those keys.
As for the second question, if you're using WPF just create a textbox element and set AcceptsReturn="True" and TextWrapping="Wrap". Here's an example:
<TextBox
Name="tbMultiLine"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
>
This TextBox will allow the user to enter multiple lines of text. When the RETURN key is pressed,
or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>
You can use the SaveFileDialog() .
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
If you're using XAML I'll assume you're using WPF. So you should be able to bind the Command property of the Button to ApplicationCommands.Save.