I have a entry/Textbox. And i have 10 number buttons as a calculator.(0-1-2-3-4....) When my page loaded entry having a number. Its showing me start value.( For example when my page loaded entry text being 10 already without any click to button) So i want to when i click a number button It will delete first entry value that loaded with page and it will start write my button clicks. How can I do it ?
I tried with:
private void btn1_Clicked(object sender, EventArgs e)
entry.Text="";
entry.Text="1";
But of course it didn't work because if i want to write 123 I cant write its always deleting. I just want delete first value which is coming with page loaded.
As #Jason suggested use the Placeholder property to display the initial value and in the click events append the value to the entry text:
Note: you can have a single event delegate that handle the click event for all buttons:
Constructor()
{
//set initial value here or use binding
//entry.Placeholder =
}
private void btn_Clicked(object sender, EventArgs e)
{
//you can remove the initial value if you want
//entry.Placeholder = string.Empty;
string buttonText = ((Button)sender).Text;
entry.Text = $"{entry.Text}{buttonText}";
}
An alternative way is to add a boolean flag out of the method to avoid this.
public class MyCalculator ()
{
private bool _isNewNumber;
protected override void OnAppearing()
{
_isNewNumber = true; //whenever open the page, needs to refresh
}
private void btn1_Clicked(object sender, EventArgs e)
{
if (_isNewNumber)
{
entry.Text=""; //clear the number for the first time
}
entry.Text="1";
_isNewNumber = false;
}
}
Related
I am trying to fill two text box in a form using button controls but it seems to work for 1 only. when I run the program, I can't select between two text box. what condition should I use?
private void Button0_Click(object sender, EventArgs e)
{
if (???????????????????)
{
metroTextBoxQuantity.Text = metroTextBoxQuantity.Text + "0";
}
else
{
metroTextBoxItemcode.Text = metroTextBoxItemcode.Text + "0";
}
}
One possible solution is to create a Control variable that stores a reference to the last clicked textbox. This would be handled by the Click event for both textboxes. Then, in your number button clicks, just append the number to the selected textbox variable. Something like this:
Control SelectedTextbox { get; set; } = null;
//Use this event handler for both the Quantity and Itemcode textbox
public TextBox_Click(object sender, EventArgs e)
{
SelectedTextbox = (Control)sender;
}
private void Button0_Click(object sender, EventArgs e)
{
if(SelectedTextbox == null)
throw new Exception("SelectedTextbox has not been set");
SelectedTextbox.Text += "0";
}
Note, if MetroTextBox doesn't inherit from Control, you'll need to change SelectedTextbox to a different type. It most likely does though.
You also may want to consider using a single event handler for all the button clicks instead of one for each. You can either parse what number button was clicked using the sender's name, or you can store the number of the button in the button's Tag and read it from there. But that's best left for another question.
I am trying to make a button visible = false if a quantity in a text box is less than or equal to 0. The problem I am having is that you have to click the button in order to activate the function.
Here is my code so far
int quantity = int.Parse(textBox33.Text);
if (quantity <= 0)
button13.Visible = false;
if (quantity > 0)
button13.Visible = true;
do I have to disable the visibility of the text box beforehand?
Simply go to the form editor and double click on the textbox. In the code presented to you after double clicking add your code or double click on the form itself if you want the code to be executed whenever the form is loaded.
At first you should encapsulate the code to update the button in a specific method:
private void UpdateButton13()
{
button13.Visible = quantity > 0; // no need for if/else
}
Then you can call this from every event after which the button should be updated. From your comments it seems you want to update that button
at Form load and
when the text in textBox33 has been changed.
So for example:
public class YourForm : Form
{
public YourForm()
{
InitializeComponents();
// register event handlers:
Load += YourForm_Load;
textBox33.TextChanged += textBox33_TextChanged;
}
private void YourForm_Load(object sender, EventArgs e)
{
UpdateButton13();
}
private void textBox33_TextChanged(object sender, EventArgs e)
{
UpdateButton13();
}
private void UpdateButton13()
{
button13.Visible = quantity > 0; // no need for if/else
}
}
Of course you can also create and register the event handlers in the designer window, without having to write the code in the constructor yourself.
The code above may now seem a little redundant (same code in two methods and a one-line method). But I assume that you want to do further things on loading the form and on changing text, and maybe you want to call UpdateButton13 from other parts of your code, too. So encapsulating here is good style (imho) to avoid problems for further development.
go to textbox events and insert the code to textChanged event.
but for better than that you can do digit validation event
private void textBox33_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[1-9]"))
{
e.Handled = true;
}
}
in that case order can be only positive number
I have been trying to figure out on disabling the text box when text is entered in the text box. I am able to do this but I have also got another problem which is, lets say you have a text box with some word i.e "Welcome". If I edit that and add more letter on to that i.e "WelcomeSSS" adding SSS then text is enabled. But when I delete "SSS" from that text box, button is still enabled and not DISABLED as the text is the same as it was before editing.
How do I make sure that the text is disabled in this situation?
And also I want to add dialog box when a user click on different button to go to different page without saving the edited content. How do i do this?
Here is my code so far:
private void textbox1_IsChanged(object sender, KeyEventArgs e)
{
//SaveButton.IsEnabled = !string.IsNullOrEmpty(TextBox1.Text);
if (TextBox1.Text.Trim().Length > 0)
{
SaveButton.IsEnabled = true;
}
if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes))
{
}
}
This is using KeyUp event handler in wpf.
If I understood your question correctly...
private void textbox1_IsChanged(object sender, KeyEventArgs e)
{
if (textbox1.Text == "Welcome"){
SaveButton.IsEnabled = false;
}
else{
SaveButton.IsEnabled = true;
}
}
You need a data structure for storing the saved values. E.g. a List of strings. In the following snippet, these values are stored in the SavedTextBoxTexts list.
At first, the SaveButton gets disabled (you can do this in the XAML as well). When SaveButton has been clicked, the textBox1.text value will be stored in the list and the button gets disabled.
When textBox1.text is edited and SaveButton exists (already), the different conditions get checked.
If textBox1.text is already stored in SavedTextBoxTexts or textBox1.text is empty or contains only whitespace characters, SaveButton gets disabled. Otherwise the SaveButton will be enabled.
public partial class MainWindow : Window
{
private List<string> SavedTextBoxTexts = new List<string>();
public MainWindow()
{
InitializeComponent();
// Disable button right from the beginning.
SaveButton.IsEnabled = false;
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
// The function may be called, while the window has not been created completely.
// So we have to check, if the button can already be referenced.
if (SaveButton != null)
{
// Check if textBox1 is empty or
// textBox1.text is already in the list of saved strings.
if (String.IsNullOrEmpty(textBox1.Text) ||
textBox1.Text.Trim().Length == 0 ||
SavedTextBoxTexts.IndexOf(textBox1.Text.Trim()) >= 0)
{
// Disable Button
SaveButton.IsEnabled = false;
}
else
{
// If textBox1.text has not been saved already
// or is an empty string or a string of whitespaces,
// enable the SaveButton (again).
SaveButton.IsEnabled = true;
}
}
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
// Store the text in textBox1 into the SavedTextBoxTexts list.
SavedTextBoxTexts.Add(textBox1.Text.Trim());
// Disable the SaveButton.
SaveButton.IsEnabled = false;
}
// This is executed, when the other button has been clicked.
// The text in textBox1 will not be saved.
private void AnotherButton_Click(object sender, RoutedEventArgs e)
{
if (WpfHelpers.Confirmation(resources.QuitWithoutSaving, resources.Changes))
{
// Move to other page ...
}
}
}
My program:
Textfield will display a default value and when button is clicked, value decrements by 1. I am able to save the form and reload at a later date and textfield value will display last saved value. When I click button to decrement again, value should decrement by 1 from saved value and not default value.
My problem:
When I save the program after decrementing value and reload form later, the textfield value returns back to default value when I click the button to decrement. On initial form load, the correct value is displayed, its when I click the button that the value resets back to default.
Here's the core areas of my program:
class Play
{
public int Total = 5110;
}
private void Form1_Load(object sender, EventArgs e)
{
fText.Text = Properties.Settings.Default.TextBox1;
}
private void btnButton1_Click(object sender, EventArgs e)
{
myPlay.Total--;
ftext.Text = myPlay.Total.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
Properties.Settings.Default.TextBox1 = fText.Text;
Properties.Settings.Default.Save();
lblSaveNote.Text = "Saved " + DateTime.Now;
}
I've managed to fix it by replacing btnButton1_Click code with:
Properties.Settings.Default.TextBox1--;
fText.Text = Properties.Settings.Default.TextBox1.ToString();
I have two text boxes in a win forms that the user will be typing information into. I would like to clear the text box when the user start typing. I am using the TextChanged event handler, so every time I type it will erase, which makes me not able to type anything into the text box. Here is the code I am using:
private void TXTBX_HourlyRatae_TextChanged(object sender, EventArgs e)
{
TXTBX.HourlyRate.Clear();
TXTBX.HoursWorked.Clear();
}
I understand that everytime I type into the text box I will be executing this event handler, but I don't know how to go about making it execute only the first time I type into the text box.
private bool firsttime = true;
private void TXTBX_HourlyRatae_TextChanged(object sender, EventArgs e)
{
if (firsttime)
{
TXTBX.HourlyRate.Clear();
TXTBX.HoursWorked.Clear();
firsttime = false;
}
}
if you want to do it everytime you enter the textbox handle the loss focus event
private void TXTBX_HourlyRatae_LostFocus(object sender, System.EventArgs e)
{
} firsttime = true;
Move your Clear() calls into the corresponding Enter or GotFocus events.
Create a new private class member:
private bool _userHasEnteredText = false;
Only erase the text when this bool is false, then set this bool to true once you've cleared the text the first time.