C# set focus on textbox - c#

I'm trying to set the "txtMiles" textbox to focus after:
The form opens
When the "clear" button is clicked
I have tried using txtMiles.Focus(); but it doesn't seem to work for me.
CODE BEING USED ON THIS FORM
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.

You should make sure your TabIndex is set and then instead of Focus(), try use Select(). See this MSDN link.
txtMiles.Select();
Also make sure there isn't a TabStop = true attribute set in the view file.

It's old, but someone could need this.
Control.Focus() is bugged. If it's not working, try workaround:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.

You already have your txtMiles focused after the clear-button click. As for the Startup, set txtMiles.Focus() in your load-method.
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}

using this solution worked perfectly...
txtMiles.Select();

Related

bunifu material textbox mode password is not working

I want to create a login page based C# desktop application. I use the bunifu toolbox to create a login page design. But when I want to create a password field using the bunifumaterialtextbox, the textbox does not show any changes / it only displays alphabet. It looks like the ispassword contained in the textbox properties is not working. So what should I do so that this texbox can display the correct password (not displaying alphabeth) when the program is run ?. I apologize for any errors in this question.
is just a easy solution i have found
private void passbox_OnValueChanged(object sender, EventArgs e)
{
passbox.isPassword = true;
}
I'm new using the Bunifu framework tool and I had the same problem then you. The solution I found was to invoke the _TextBox method which I suppose that gives you all the normal TextBox controls.
My code was something like this:
txtPassword._TextBox.PasswordChar = '*';
I loaded this code inside the Form_Load code block. It worked for me, hope that be useful for you too. Good luck!
in side the properties tab of the bunifu text box there is a property ispassword set it to true.
to fully make the bunifu textbox to passwordbox you have to do the following:
1.in side the properties tab of the bunifu text box there is a property ispassword set it to False.
2.create an event of Enter and use the following code:
3.create an event of Leave and use the following code:
private void txtpassword_Enter(object sender, EventArgs e)
{
if (txtpassword.Text == "Password")
{
txtpassword.Text = "";
txtpassword.isPassword = true;
}
}
private void txtpassword_Leave(object sender, EventArgs e)
{
if (txtpassword.Text == "")
{
txtpassword.Text = "Password";
txtpassword.isPassword = false;
}
}
like this
You can use the UseSystemPasswordChar property in your text box.
Example:
TextBox1.UseSystemPasswordChar = true;
this problem's solution is using enter,leave and textchange event such as below code if the name of your text box is txtPassword:
private void txtPassword_TextChange(object sender, System.EventArgs e)
{
if (txtPassword.Text.Trim() != "")
{
txtPassword.PasswordChar = '*';
}
else
{
txtPassword.PasswordChar = '\0';
}
}
private void txtPassword_Leave(object sender, System.EventArgs e)
{
if (txtPassword.Text.Trim() == "")
{
txtPassword.PasswordChar = '\0';
txtPassword.TextPlaceholder = "insert your placeholder..";
}
}
private void txtPassword_Enter(object sender, System.EventArgs e)
{
if (txtUserName.Text.Trim() != "")
{
txtPassword.PasswordChar = '*';
txtPassword.PlaceholderText = "";
}
}

How to Save a Boolean Value in C#

I am making a game similar to "Make it Rain" or "Cookie Clicker." I have settings where you can purchase items to make each click of the cookie produce more cookies. I have been trying to make the game save the variable. For example, each click produces a cookie. If you have 100 cookies, you can buy a cookie cutter, so each click produces 5 cookies, and so on. I have booleans called cookiecutters, which are false. If the user buys the item, cookiecutters = true. I would like to save this variable to true so when the user closes and reopens the form, each click produces 5 cookies. This is what I have come up with, yet it doesn't work.
private void Form1_Load(object sender, EventArgs e)
{
if (cookiecutter == true)
{
cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
cookiecutter = Properties.Settings.Default.cookiecutter;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (cookiecutter== true)
{
Properties.Settings.Default.cookiecutter = cookiecutter = true;
}
Properties.Settings.Default.Save();
}
Thank you all for the help in advance!
Your code looks weird. Try to make these changes and make sure your initial value in the settings for cookiecutter is false.
private void Form1_Load(object sender, EventArgs e)
{
cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.cookiecutter = cookiecutter;
Properties.Settings.Default.Save();
}
Almost.. you can get the value trough
Properties.Settings.Default.["cookiecutter"]
instead of
Properties.Settings.Default.cookiecutter

Interaction between forms in WinForms application, c#

guys!
I've got 2 forms in application - working form (frmMain) and form of settings (frmSettings).
There are two buttons on frmSettings - Save and Cancel. In frmMain I use the following approach to show the frmSettings:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
// ...
}
The problem is I don't know, how to detect, which button was pressed on the frmMain - Save or Cancel. The further logic of the program depends on this fact. I need something like this:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(/* frmSettings.SaveButton.WasClicked == true */)
{
InitializeServices();
}
// ...
}
Please, give me an advice, how to implement such kind of interaction between forms. Better without using global variables for saving buttons state.
Thanks beforehand.
ShowDialog returns a DialogResult object that allow you to know that. You have to:
On Save Button's click event, set this.DialogResult to DialogResult.OK
On Cancel Button's click event, set this.DialogResult to DialogResult.Cancel
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
if(frmSettings.ShowDialog() == DialogResult.OK)
{
InitializeServices();
}
//.......
}
Edited to manage the DialogResult as #tsiorn's answer: setting form's DialgoResult insted of setting that property on each button.
You chould use DialogResult to handle this. On your form settings window, you can set the result as so:
protected void btnSave_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.OK
this.close;
}
protected void btnCancel_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.close;
}
Then ...
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(frmSettings.DialogResult == DialogResult.OK)
{
// save
InitializeServices();
}
// ...
}
Start with an enumeration of the possible values:
public enum ExitMethod
{
Other, //this should be first, as a default value
Save,
Cancel,
Error
}
Then make a property on SettingsForm of that type:
public ExitMethod ExitMethod { get; private set; }
In SettingsForm's save/exit methods set that property to the appropriate enum value, and in the main form you can read that property value.
in the frmSettings window you handle the Click events on the buttons. Then set the dialog result:
void frmSettings_Save_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
void frmSettings_Cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
in the main form you do something like this to capture and evaluate the result:
DialogResult answer = frmSettings.ShowDialog();
if (answer == DialogResult.OK)
{
...
}
Additional information and usage can be found here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx

user input and acceptance inside a function

My function accepts user input and then do somework when user clicks ok.
private void cannyToolStripMenuItem_Click(object sender, EventArgs e)
{
canny();
}
private void canny()
{
// get user input
// if user clicks ok
if (ok button is clicked)
{
messagebox.show(" you clicked ok")
//
//do dome work
//
}
}
But I can't see any messagebox. What I am missing.
private void ok_Click(object sender, EventArgs e)
{
// should I add here some thing
}
what i am missing.
regards,
I think what you are trying to achieve is to get the result from a dialog box. If that is the case you want to do the following:
private void ShowDialogAndDoSomethingBasedOnTheResult()
{
DialogResult result = MessageBox.Show(
"Dialog text",
"Caption to go in title bar",
MessageBoxButtons.OK);
if (result == DialogResult.OK)
{
//Do work
}
}
See http://msdn.microsoft.com/en-gb/library/0x49kd7z.aspx for more examples.
Well, yes, you do:
private void ok_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Ok;
}
Which closes the dialog, it will only stay running as long as its DialogResult property is None. It isn't strictly necessary, you can also use the designer. Change the button's DialogResult property, now you don't need to write the code. That is however not often appropriate, you usually want to check if the user provided all the information you require. Ymmv.

C# program question

private void delete_Click(object sender, EventArgs e)
{
convertedText.Text = "";
}
private void copy_Click(object sender, EventArgs e)
{
if (convertedText.Text != "")
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
else... what to put here?
}
The program has two buttons (copy and delete) and one textbox. If I click the 'Copy' button, it copies the text from convertedText.Text without any problem. The 'delete' button also clears the textbox fine.
But if there's nothing in the textbox, the 'copy' button still attempts to copy it which is causing an unexpected behaviour.
So, what code do I add to the 'else' statement...? What I want is that if the textbox has nothing in it, then the clipboard operation won't be used. How to do that?
Thanks in advance!
Don't add an else clause, just have the if by itself, e.g.
private void copy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(convertedText.Text))
{
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
}
Also, is there any reason why you're copying the text box text to the clipboard and then using the clipboard text to update the text box text? Unless I'm missing something, this should have no effect on the text box, so the code can be simpler:
private void copy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(convertedText.Text))
Clipboard.SetText(convertedText.Text);
}
Your error stems from the fact that you are missing some parentheses there:
if (convertedText.Text != "")
{
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
Only the first line after an if statement is considered to be part of what gets executed dependent on the evaluation of the if when you are omitting parentheses.
You could also return if the textbox does not have a value...
private void copy_Click(object sender, EventArgs e)
{
if (convertedText.Text.Equals(""))
return;
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
maybe you are missing brackets { and }
if (convertedText.Text != ""){
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
else
Try putting
try
{
string foo = "bar" + 42;
}
catch
{
throw;
}

Categories