I'm trying to figure out how I can reset my windows form application once the option to do so has been selected from a choice of to radio buttons. eg:.
So once "Yes" is selected the program resets. Here is what I have been trying.
private void EndOptions() {
if (anotheryes.Checked) {
RestartForm();
}
if (anotherno.Checked) {
//todo
}
}
The restartform method is just:
private void RestartForm() {
Application.Restart();
Application.ExitThread();
}
Right now, this only works if I press the "calculate ratio" button again. On click, the application will restart. I'm wanting it so that it resets just by clicking the Yes radiobutton.
The logic behind the button is as follows:
private void DisplayLogic() {
double height = 0, waist = 0;
if (WaistNumericCheck(out waist) && HeightNumericCheck(out height)) { //check if input is numeric, if true, move on to next check
if (CheckDimensionHeight(height) == true && CheckDimensionsWaist(waist) == true) { //check if dimensions are higher than the lower limits, if BOTH true, move on the next
ShowResult(height, waist); //shows results
DisableInputs(); //disables inputs while results are being shows
AnotherGroupBoxVisible();//makes the restart selection groupbox visible
EndOptions(); //check to see which option is selected
}
}
}
Any tips/help would be greatly appreciated thanks!
I have to say its an odd thing to have the entire app restart, just so the user could do another calculation.. The API you are using i.e. Application.Restart is documented for use with ClickOnce Applications
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.restart(v=vs.110).aspx
I don't know if your app is click once or not.. but anyway, hoping you'd learn something new regardless, here is literal answer to your question..
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged(v=vs.110).aspx
This event occurs when the value of the Checked property of a radio button changes.
Anyway, I do strongly recommend, that as suggested in comment above, you should reconsider how you want your app to provide a better user experience here..
If its just for learning, the event should help.. and feel free to explore other events on the control which might be of interest.
Handle the CheckedChanged event of the RadioButton, and then you can immediately restart as soon as it is clicked:
private void AnotherYes_CheckedChanged(object sender, EventArgs e)
{
if (AnotherYes.Checked)
{
Application.Restart();
}
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged(v=vs.110).aspx
Related
I'm using web forms
Right now my code runs when I leave the text box and the text has changed. I am running into issues. If I change the text but hit a button instead of enter, it resets via code. I need to be able to change the text and click a button which wont yet do anything, or change the text and hit enter which will trigger code.
thanks for the help
This is text changed event for the text box with notations of what im needing to do. really what I think I need is an event for clicking enter, not changing text
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
if (txtboxPlate.Text == "plate number")
{
//will check database for "plate number" and do stuff on enter.
}
else
{
resetforms();// on enter
}
else
{
the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
}
You need to implement a method for the KeyDown event of your textbox.
private void txtboxPlate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
//Code
}
}
Keydown is a client-side event, as far as I know you're more than likely going to have to use JavaScript/Jquery.
Refer to the following link:
Textbox Keydown Event
I would of used a comment but... rep issues :/
edit:
To anyone that hasn't realised yet, the question changed to webforms not winforms
Alternative:
Use a button.
You could place a button next to the textfield, it's simple and not a lot of work goes into it. You can set the TabIndex of the textbox to 1 and the TabIndex of the button to 2 so when you hit TAB it will focus the textbox and if pressed again it will focus the button. You could also look into adding the button to the textbox for design purposes.
edit: You also need to think about post back, when you hit a button the page get's post back meaning values are lost, to persist the data within the field you would have to use view state, session variables or a hidden field. For simplicity I'd set the value of the text box to a hidden field and then simply re-apply the value on page_load. If the value needs to persist across multiple postbacks/accessed from other pages use session variables.
So as a code example:
Remove the Text_changed event entirely.
protected void btnDoStuff(object sender, EventArgs e)
{
if(txtPlateNumber.Text == "Plate Number")
{
//Do stuff
}
else
{
//Do other stuff
}
}
-
If you notice the value disappearing after the post back do something like:
protected void btnDoStuff(object sender, EventArgs e)
{
if(txtPlateNumber.Text == "Plate Number")
{
//Do stuff
myHiddenField.Value == txtPlateNumber.Text;
}
else
{
//Do other stuff
}
}
then reset the value on page_load.
If this is winform and you still want to use textchanged you can try catching your code first for example:
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
//Catching code
if (txtboxPlate.Text != "")
{
if (txtboxPlate.Text == "plate number")
{
//will check database for "plate number" and do stuff on enter.
}
else
{
resetforms();// on enter
}
}
else
{
the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
cause what textchanged is doing is unless txtboxPlate.Text equels "plate number" it will always do the else statement. Correct me if i'm wrong though but i had the same problem before which almost made me go insane.
Or try above 1 upvote code:
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
//Event only happens if you press enter
if (e.KeyCode == Keys.Enter)
{
if (txtboxPlate.Text == "plate number")
{
//will check database for "plate number" and do stuff on enter.
}
else
{
resetforms();// on enter
}
}
else
{
the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
}
background
I have a c# application written that interacts with an SQL database. a recent feature request was to allow for different types of authentication (3 types). I originally decided to use 3 radio buttons (one for each auth option) for this and 2 textboxes (username/password). this worked fine and all the background code works fine but they have now requested that when they use SSPI auth (requires no extra input from user) that i gray out the textboxes so information cannot be entered and when one of the other two options is chosen, to allow the boxes to be editable again. To make this cleaner i now have a single combobox with 3 items (auth) and 2 textboxes (un/pw).
question
how do i have the application listen for changes to the combobox before the user clicks run? I have always used a button as a catalyst event and have not had to do this before. I have seen a few examples where i can use the condition (if selected index is equal to x) do blah, but it seems to require my start button to be pressed anyways and not a workable solution. i have also found this example C# -comboBox Selected IndexChange that i do not quite understand and i believe requires a 2 boxes to be made but i dont know why.
sudo code
if ((combobox item is not selected) or (combobox selection == indexitem1))
{
//then keep textboxes read only
}
else
{
//change textbox to editable
}
request
i need this listener to be able to tell when the combobox selection is any of the 3 choices and moves between any of them and will correctly change the textboxes to reflect the current selection regardless of previous state to current state.
Any help is greatly appreciated. Links, code, comments, questions. everything helps me see something i havent yet or helps me search for better answers. Thanks!
solution
i just figured this out
private void AuthSelect_SelectedIndexChanged(object sender, EventArgs e)
{
//listen if combobox selection is changed
if ((AuthSelect.SelectedIndex == 0) || (AuthSelect.SelectedIndex == -1))
{
userName.ReadOnly = true;
password.ReadOnly = true;
}
else
{
userName.ReadOnly = false;
password.ReadOnly = false;
}
}
private void AuthSelect_SelectedIndexChanged(object sender, EventArgs e)
{
//listen if combobox selection is changed
if ((AuthSelect.SelectedIndex == 0) || (AuthSelect.SelectedIndex == -1))
{
userName.ReadOnly = true;
password.ReadOnly = true;
}
else
{
userName.ReadOnly = false;
password.ReadOnly = false;
}
}
I want to show the list of items in a combo box when the user selects the text. I have a touch screen application and it's very difficult to hit the dropdown arrow so I figure I'd show the menu when the text is selected which is often what gets touched. I'm using VS 2008. and suggestions for a touch friendly numeric up down solution in VS2008?
You could use the ComboBox.Click event handler and the ComboBox.DroppedDown property and do something like this:
private void ComboBox1_Click(System.Object sender, System.EventArgs e)
{
ComboBox1.DroppedDown = true;
}
You could also use the same event handler for a numericUpDown and use the mouseposition as well as the position and height of the NumericUpDown to get whether or not the click was above or below the halfway-line of the control by doing something like this (not sure if my math here is perfect, but it worked when I tested it):
if ((MousePosition.Y - this.PointToScreen(NumericUpDown1.Location).Y < NumericUpDown1.Height / 2))
{
NumericUpDown1.Value += 1;
}
else
{
NumericUpDown1.Value -= 1;
}
HTH
I was working on a similar situation. We wanted to make the text area behave the same as the button on the right. (IE the user clicks and gets the drop down box)
davidsbro is similar to what I ended up doing, but we wanted it to close if they clicked again, so the value became dropDown.DroppedDown = !dropDown.DroppedDown;.
The issue with this is that if the user clicks the right button of the drop down box, the dialog box opens, then calls the onClick event.
I solved this situation by tracking the original state via the onmouseover event. If the value has changed, we have to assume that the button on the select box handled the click already.
private bool cbDropDownState = false;
private void dropDown_MouseEnter(object sender, EventArgs e)
{
cbDropDownState = dropDown.DroppedDown;
}
private void dropDown_Click(object sender, EventArgs e)
{
if (dropDown.DroppedDown == cbDropDownState )
dropDown.DroppedDown = !dropDown.DroppedDown;
}
I am writing an IM program, and I have the method to make a form flash and stop flashing... question is, how do I implement it?
When a message arrives, I can set the window flashing, but I need to make sure it doesn't have focus. Checking the focued method always seems to return false and so it flashes even when the form is open.
Also, which event to I need to handle to stop it flashing? When the user clicks the form to make it maximise, or switches focus to the form, I need a way of stopping it.
What's the best way?
You can handle the Activated and Deactivate events of your Form, and use them to change a Form-level boolean that will tell your code whether your form has the focus or not, like this:
private bool _IsActivated = false;
private void Form1_Activated(object sender, EventArgs e)
{
_IsActivated = true;
// turn off flashing, if necessary
}
private void Form1_Deactivate(object sender, EventArgs e)
{
_IsActivated = false;
}
When a message arrives, you check _IsActivated to determine if your Form is already the active window, and turn on flashing if it isn't. In the Activated event, you would turn off the flashing if it's on.
The Focused property of your form will always return false if it has any controls on it. This property refers to whether the control in question (the form, in this case) has the focus within your application's form, not whether the application itself has the focus within Windows.
Checking if the form is minimized or not:
if (this.WindowState == FormWindowState.Minimized)
{
MakeFormFlash();
}
else
{
MakeFormStopFlash();
}
Event to trigger when the form is activated by user or code:
this.Activated += new EventHandler(Form_Activated);
Well Focused should be the property to check, so you need to try and work out why that is always returning false.
As for what event to listen to, probably the GotFocus event, though that may not work until you can work out what is wrong with the Focused property.
There are a number of ways you can handle this. Probably the easiest would be to have a flag that you set whenever the form is flashing so this can be reset on re-activation of the form e.g.
Code for base IM window form
private bool IsFlashing;
....
// Code for IM windows
public void OnActivate(EventArgs e)
{
if (IsFlashing)
{
// stop flash
IsFlashing = false;
}
}
public void Flash()
{
// make flash
IsFlashing = true;
}
Then wherever you do your code to handle the new message you would just need to check that the particular conversation window (if you handle multiple ones) that the message is directed at is the current active one:
public void OnNewMessage(AMessage msg)
{
Form convoWindow = FindConvoWindow(msg.Sender);
if (Form.ActiveForm == convoWindow)
{
// update the conversation text
}
else
{
convoWindow.Flash();
}
}
I have a working WinForm that handles the search functionality in my customer database. It contains the following controls:
A textBox where you type your search string (A)
A "search" button (B)
A DataGridView (C) that returns the result of the search
An "OK" button (D) and a "Cancel" button (E)
What I am trying to accieve next is this:
When A is active the AcceptButton (enter) should be linked to B
When B or enter is pressed C should become active
When C is active the AcceptButton should be linked to D
I realise this is a somewhat big question so don't hesitate to answer just one of the bullet marks if you know the answer.
EDIT: I have solved the implementation of requirement 1 and 3 above, but I am still looking for an answer to the second one. To clarify, when the search is initiated (meaning i have pressed enter on the keyboard or the search button with the mouse) I want focus to shift to the first line in the DataGridView.
When you get the text changed event for textBox set the AcceptButton to be "Search":
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = SearchButton;
}
There will probably want to be more code in here, checking the length of the string etc.
Then once you've done the search and filled in the DataGridView you can then set the AcceptButton to be "OK".
private void dataGridView1_DataMemberChanged(object sender, EventArgs e)
{
this.AcceptButton = OKButton;
}
Though you probably wouldn't want to use this event.
"Cancel" is always the CancelButton.
EDIT: OK for part 2 you want the following code:
private void SearchButton_Click(object sender, EventArgs e)
{
dataGridView1.Focus();
}
This will make it active as soon as the search button is pressed, but you probably want to do it when the DataGridView has been populated - just in case the search returned no results.
Setup handlers to track the Control.Enter event for the particular controls. When the enter is reached try:
this.AcceptButton = ControlThatShouldBeAcceptButton;
You should implement an event listener to DataGridViewBindingCompleteEventHandler. In that event you could add the following code:
{
if(grid.Rows.Count > 0)
{
grid.ClearSelection();
grid.Rows[0].Selected = true;
}
grid.Focus();
}
private void WhateverControl_Enter(...)
{
this.AcceptButton = myButton;
}