I have a WPF application where i open a popup mennu (popup control) using the escape key. In that popup menu I open a file dialog when pushing a button, and the when pushing the button closes the popup. When i next time pushes the esc button it doesnt pop up, not until i have focused another program, eg. reset focus. Does anyone know what could cause this?
Edit
//called when pushing esc
private void ShowSettingsMenu()
{
SettingsMenu.IsOpen = true;
}
//clicking my button, subsequent presses on my esc, doesnt pop it up (the code is run)
private void ImportLicenseButton_Click(object sender, RoutedEventArgs e)
{
SettingsMenu.IsOpen = false; //<- hiding it again
OpenFileDialog filedialog = new OpenFileDialog();
filedialog.Filter = "Xml Files|*.xml";
if ((bool)filedialog.ShowDialog())
{
string fileName = "license.xml";
string destinationFolder = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
if (!string.IsNullOrEmpty(filedialog.FileName))
{
File.Copy(filedialog.FileName, System.IO.Path.Combine(destinationFolder, fileName), true);
}
else
{
MessageBox.Show("Please select a file name");
}
}
this.Cursor = Cursors.None;
}
Fixed by redisplaying the popup.
Related
I have a list of "projects" with some informations in textBoxs for each project. The user can select a project then modify the informations and click on save button after that.
If I changes selected project without save the modifications, a Yes/No MessageBox appear:
DialogResult dialogResult = MessageBox.Show(
"Do you want to save changes ?",
"Title",
MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//Click Yes
}
else
{
//Click No
}
I would to refresh all the project list (with my own Refresh() methode) after clicking on Yes/No button, but staying on the MessageBox until the refresh is done.
Is it possible?
The built in MessageBox class does not allow such complicated behaviour.
One way to do this is to create your own message box. Create a subclass of Form, add some labels and buttons. Expose some events like YesClicked and NoClicked.
In your main form, create an instance of your custom message box, subscribe to the events, and call ShowDialog on it.
After the refresh is done, you can call Close or Dispose on your custom message box to close it.
Try creating a custom message box. I commented the code, let me know if you need clarification
public static class MessageBoxResult
{
public static int dialogResult; // <== i use this value to determine what button was pressed
}
// your custom message box form code
public partial class CustomMsgBox : Form
{
public CustomMsgBox()
{
InitializeComponent();
}
public void show(string pos0, string pos1, string pos2, string message) //<=== initializing the message box with the values from your main code
{
button1.Text = pos0;
button2.Text = pos1;
button3.Text = pos2;
label1.Text = message;
}
// message box events to set the static field incase a button on the custom form was changed
private void button1_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 0;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 1;
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
MessageBoxResult.dialogResult = 2;
this.Close();
}
}
//usage
{
MessageBoxResult.dialogResult = -1; // <== setting the static field to -1 to mean nothing was pressed
CustomMsgBox cMsgBox = new CustomMsgBox();
cMsgBox.show("your message");
cMsgBox.ShowDialog();
}
You can change what happends, based on what button is clicked, with the DialogResult
DialogResult dialogResult = MessageBox.Show(#"Some body", #"Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
// do stuff
}
No, a message box cannot do this. It's not meant to do this. It's meant to just display a message... in a box :)
What you can always do is create your own window that looks like a message box and behaves like a message box, but actually does things when you click the buttons.
I'm trying to make an Option dialog in Visual Studio. I hid the default ControlBox and made a close button. The dialog work, but the close button isn't. Here's the code:
public static class dialog
{
static Form gotoBox = new Form();
public static void showDialog()
{
Button closeButton = new Button() { Text = "Close" };
gotoBox.Controls.Add(closeButton);
gotoBox.ControlBox = false;
gotoBox.ShowDialog();
closeButton.Click += new System.EventHandler(gotoBox_close);
}
static void gotoBox_close(object sender, EventArgs e)
{
gotoBox.Close();
}
}
When I click the button, nothing happen. So what did I do wrong?
gotoBox.ShowDialog(); //This line shows the dialog
//The rest doesn't execute until ShowDialog returns
closeButton.Click += new System.EventHandler(gotoBox_close);
You need to move the event registration before the show dialog or else it will not have any effect until dialog is closed
If the user clicks on a button, a dialog shows up, asking to input a string, and there's an 'OK' button on the same dialog, when the user presses that, the dialog should close. That's at least the plan, the problem is: after adding the eventhandler to the OK button, my application freezes, when the user would open the dialog.
addNewFamButton = FindViewById<Button>(Resource.Id.newFamilyButton);
addNewFamButton.Click += (sender, e) => {
Dialog dialog = new Dialog(this);
dialog.SetContentView(Resource.Layout.addNewFamily);
dialog.SetTitle("Add new family to the list");
dialog.Show();
// Problem starts here:
Button saveNewFamily = FindViewById<Button>(Resource.Id.dialogButtonOK);
saveNewFamily.Click += (object o, EventArgs ea) => { dialog.Dispose(); };
};
I tried it with dialog.Cancel() too, but I got the same results. If I remove the last two lines, the dialog works, but obviously won't close.
FIXED: Thanks to user370305 for the simple solution:
Button saveNewFamily = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK);
Your OK button is part of Dialog view, so you have to find that view using your dialog object reference, something like, (I am not familiar with xamarin but this one gives you hint)
Change your line,
// Problem starts here:
Button saveNewFamily = FindViewById<Button>(Resource.Id.dialogButtonOK);
with
Button saveNewFamily = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK);
try this
// create an EditText for the dialog
final EditText enteredText = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title of the dialog");
builder.setView(enteredText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
// perform any operation you want
enteredText.getText().toString());// get the text
// other operations
dialog.cancel(); // close the dialog
}
});
builder.create().show();
In my application there appeared a following problem: certain dialogue forms can be interacted with before they are drawn.
The code goes like:
void FunctionCalledFromButtonPressHandler()
{
var f = new MyDialog();
if (f.ShowDialog() == DialogResult.OK)
{
//do things
}
}
The buttons on the starting form and dialog form (one that loses it) are over each other on the touchscreen. If the plac is clicked over twice, there is a pause corresponding to the load of dialog form, and then, without it being shown, wotk in starting form is continued/
How should i go about preventing such sort of behaviour (that is, apart from removing the delay on the loading of dialog)?
Set 'Button.Enabled = false;' on your starting form inside the launching method.
void FunctionCalledFromButtonPressHandler()
{
YOURBUTTON.Enabled = false;
var f = new MyDialog();
if (f.ShowDialog() == DialogResult.OK)
{
//do things
}
YOURBUTTON.Enabled = true;
}
Then in your dialog form set the other Button.Enabled = false; in the form constructor. Then you can set the button to enabled in the Shown event like this:
void MyDialog_Shown(object sender, EventArgs e)
{
OTHERBUTTON.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
cl = new ChangeLink();
cl.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = cl.ShowDialog(this);
if (dr == DialogResult.Ok)
{
if (cl.getText() == "")
{
MessageBox.Show("The TextBox Cannot Be Empty");
cl.ShowDialog(this);
return;
}
else
{
label4.Text = cl.getText();
cl.Close();
}
}
else if (dr == DialogResult.Cancel)
{
cl.Close();
}
cl is a new Form i mgetting the text from.
Now im check that if cl.getText() is empty "" its throwing a message to the user and when the user click ok on the messagebox i want it to return and show the new Form dialog again.
When i click once on the OK button of the new Form and it show the messaeBox message and then show me again the dialogresult box of the new Form but then when i click on OK again and the textBox is still empty it doesnt show the messageBox again just close the new Form and set label4 text to be empty.
I want that each time the user click OK and the textBox is empty it will keep show the new Form textBox dialog untill the user click Cancel or put something in the textBox and then click OK.
It wiil be a lot cleaner if you do the checking in your second Form. You will need to add code to your Form2's OK Button's Click Event. Make sure you remove the default DialogResult from the OK Button Property's.
private void button2_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(textBox1.Text))
DialogResult = DialogResult.OK;
}