WPF Window Closing does not work after Cancel [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have an issue that I can't seem to resolve via Google (likely because I'm not searching the right criteria). I have a Closing Event that checks if a button is enabled and pops a messagebox with a result (Yes/No). If the user says NO, I get the desired results in the app; however, the X in the top right corner stops working. How to I "reinstate" the close button ("X" in the top right hand corner) so it works again if pressed (and also evaluates the logic again).
I tried this: Stackoverflow Question
i don't think I want to play with Visibility of the window. The window doesnt go anywhere. They have dirty data and they need to fix it or have it auto saved.
What I have in the application is:
private void DXWindow_Closing(object sender, CancelEventArgs e)
{
if (BtnPatSave.IsEnabled == false) return;
if (MessageBox.Show(Properties.Resources.PatAcctMsgBox3, Properties.Resources.PatAcctMsgBox1,
MessageBoxButton.YesNo,
MessageBoxImage.Warning) == MessageBoxResult.No)
{
e.Cancel = true;
}
else
{
var patId = TbPatId.Text;
var usl = new UnSetLockValue();
usl.UnSetVal(patId);
Log.Info("Patient Account is now unlocked by user: " + Environment.UserName);
}
}

Thats because you are using the MessageBox class.
It disables the "X" button to allow the user only to provide the values you specify.
If you don't want this behaviour i think you have to create your own "MessageBox".

Related

c# Button Press Detection [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I'm making a "web browser" in C#, and I'm coming across problems. I don't think my button to go to the navigated URL is working, and I don't know how to test it. When you click the button, it runs mainBrowser.Navigate(urlTextBox.Text);, but that's not working for some reason. This is my code:
private System.Windows.Forms.GroupBox browserGroup; //container of sorts
private System.Windows.Forms.TextBox urlTextBox; //self-explanatory
private System.Windows.Forms.Button navigateToURL; //button to go
private System.Windows.Forms.WebBrowser mainBrowser; //browser
private void navigateToURL_Click(object sender, EventArgs e) //on the button's click
{
mainBrowser.Navigate(urlTextBox.Text);
}
What am I doing wrong?
PS: I literally just started c# today.
I suppose you are using Visual Studio IDE ...
So, here are some directions for you to start debugging and solve it yourself:
Place a breakpoint inside the 'navigateToURL_Click' event handler method. Run on debug mode, add a URL to the URL text box and check if you can catch the 'navigateToURL' button click there.
If that doesn't work, check the button 'Click' event in the button properties dialog. It should point to your 'navigateToURL_Click' method, as seen in this next image:
On the other end, if that breakpoint works, you can debug and watch the 'urlTextBox.Text' value to see if it is correct.
That's the idea of debugging (an inseparable part of any coding), from here you can think of more checks & test ideas like this ...
Good luck with C#... Have fun coding ...

How can I do a quiz with radiobutton? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a small problem, I'm trying to create a math quiz in WPF Apllication, where numbers will randomly pop up, which will be calculated according to the chosen math operation.
Everything works as planned, but now I've gotten to prepare Radiobutton and I have no idea how to come up with it. I would like to do this so that the Radiobuttons would take turns which answer would be correct (so that it was still not the first correctly). I know the answer will have to be written as Radiobutton's content. But I don't know how to set the condition for checking the correctness of the result.
if (firstPoss.Content == result.Text)
{
}
if (secondPoss.Content == result.Text)
{
}
if (thirdPoss.Content == result.Text)
{
}
this IF statement always passes
I will be happy for any advice or nudges in the right direction.
I'm speculating a bit since I do not find your use-case to be clear (perhaps add a picture or visual description?). but i think this should be what you want;
There are a lot of good resources out there, so why don't you start with investigating how the radiobutton works in general?
e.g.
https://wpf-tutorial.com/basic-controls/the-radiobutton-control/
Now we see that the IsChecked property signifies that a certain control is checked - be it by the user or programmatically.
Thus we can check that property:
if(firstPoss.Content == Result.Text && firstPoss.IsChecked)
{
// The result is the same as the 'firstPoss' radiobutton, and the button is checked.
// Debug.WriteLine("User checked the right answer!");
}
// ... etc.
EDIT:
Would something like this work in checking whether the right answer radiobutton is checked?
var answerRadioButtons = new RadioButton[] { firstPoss, secondPoss, thirdPoss };
var correctAnswerButtonFound = false;
foreach(var answerButton in answerRadioButtons)
{
if(answerButton.IsChecked)
{
// The user checked this particular button
if(answerButton.Text == Result.Text)
{
// The user checked this button, and the result text is the same as the radio button text - this means we have the right button
correctAnswerButtonFound = true;
}
}
}
if(correctAnswerButtonFound)
{
// The user pressed the correct answer button
}
else
{
// The user did not press the correct answer button
}

Form.Show() not showing form [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've a c# project in which a form hides itself after some background task finishes, to be opened on command, but under certain conditions, the call to this.Show() or setting this.Visible to true doesn't make the form reappear! I even had the state of the form's Visible value output to be sure, and the form seems to think it's on screen, but it's nowhere to be seen.
The condition that seems to cause it to break is if I give another window control before the form hides itself. If I let it stay in front while it's working, hide itself, then tell it to come back it always does, even if I change focus after the fact, but I change focus away beforehand, it doesn't reappear, even though form.Show appears to be called.
Does anyone have any insight as to why this might be happening? It's such a weird case, especially since the state of form.Visible changes.
public partial class testForm : Form {
private void testForm_Sometrigger(object sender, EventArgs e) {
//some delay. In the actual program, this is thanks to a background worker working.
Thread.Sleep(5000);
//manually change focus to another process/window before this
this.Hide();
//I've been adding a wait here, since in practice the form won't be called again right away.
Thread.Sleep(3000);
//show form again.
this.Show();
// this will be true even if the form isn't actually visible
Console.WriteLine("is visible? "+this.Visible.ToString());
}
}
Code block added upon request. There isn't much to this bit, just showing and hiding and time passing, really.
Turns out the window not appearing probably has something to do with form inactivity and the UI thread not coming back to it, so adding a Form.Activate() after the show call fixed it.
Original question updated with solution.

Opening new form is reseting the design? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I have a project with two forms: the MainForm and AnotherForm.
In the MainForm I have a button which should open the AnotherForm. I'm using the following code to do that:
private void btnTimeReminder_Click(object sender, EventArgs e) {
AnotherForm anotherForm = new AnotherForm();
anotherForm.Show();
this.Hide();
}
The problem is, when I click the button to open the "AnotherForm", it just opens a blank form in the WindowsDefaultPosition. All the design of the AnotherForm is deleted (The buttons, images, even the size of the AnotherForm).
I have no ideia why is this happening... Am I using a bad code to open the form?
Ensure that
InitializeComponent();
is being called in the constructor of your new form.
If you need to change the initial position of your new form, you can use
anotherForm.StartPosition = FormStartPosition.CenterParent;
or whichever start position is necessary.

Maintaining focus on textbox while I click on different buttons [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am currently trying to develop a custom keyboard for WinCE application.Currently I have a form with a text box and a button. The issue is how can I maintain the focus on the keyboard when I click on the mouse to SendInput (to make sure that the textbox capture that input). One way is to set the "Focusable" property but I can't seem to set that on a Windows Form. I hope someone could help me on this. Thanks!
If you are not afraid of moving to the native side you may consider to implement a Software Input Panel (SIP). It will behave in the way you describe and can be used by any application running on the device.
This documentation is for Compact 2013, but it's also valid for previous releases (you can find release-specific versions on MSDN but they were pretty good in hiding them):
http://msdn.microsoft.com/en-us/library/ee500563.aspx
You should set the TextBox.Focus() on the button press event handler. I assume the button has a KeyPress or KeyDown function.
A more flexible alternative would be to store the last focused control.
private Control lastFocusedControl;
And when the text box is focused on it sets the value using the GotFocus event.
private void TextBox_GotFocus(object sender, EventArgs e)
{
lastFocusedControl = (Control)sender;
}
And then in the event handler you can simply do.
lastFocusedControl.Focus();
Although it is for VB.Net it has some good ideas: http://msdn.microsoft.com/en-us/magazine/hh708756.aspx
See WS_EX_NOACTIVATE and
If (m.Msg = WM_MOUSEACTIVATE) Then
m.Result = MA_NOACTIVATE
Else
MyBase.WndProc(m)
End If
Now the challenge is to adopt this to your idea (a separate process and form? or a panel with buttons?).
OTOS MS provides a custom keyboard SDK API set to write custom software keyboards for Windows Mobile (c/C++).

Categories

Resources