Xamarin dismiss popup window - c#

I have an issue , when SavedAyasListView_ItemLongClick event fires, a popupwindow appears with some buttons, and my intention is whenever i click btnDeleteAya button , perform some actions and dismiss the popup.
When the event fires, the popup appears and when i click the btnDeleteAya the action gets performed and the popup disappears, but when i do the same thing again the popup window doesnt get closed.
I want it to get closed each time i click the btnDeleteAya button not by just clicking outside because that part works as expected.
Here's the code:
public string itemclicked;
PopupWindow mpopup;
private void SavedAyasListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
{
View popUpView = LayoutInflater.Inflate(Resource.Layout.SavedAyasPopupMenu,
null); // inflating popup layout
mpopup = new PopupWindow(popUpView, 800, 500); // Creation of popup
mpopup.SetBackgroundDrawable(new BitmapDrawable());
mpopup.OutsideTouchable = true;
mpopup.Focusable = true;
mpopup.ShowAtLocation(popUpView, GravityFlags.Center, 10, 50);
itemclicked = savedAyasListView.GetItemAtPosition(e.Position).ToString();
Button btnDeleteAya = popUpView.FindViewById<Button>(Resource.Id.btnDeleteAya);
btnDeleteAya.Click += BtnDeleteAya_Click;
btnDeleteAya.Click += delegate
{
mpopup.Dismiss();
};
Button fbShareSavedAya = popUpView.FindViewById<Button>(Resource.Id.fbShareSavedAya);
fbShareSavedAya.Click += FbShareSavedAya_Click;
}

Related

non-responsive close button in winform

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

Using Button to show context menu [duplicate]

I have used toolstripdropdown in my Windows form to show list of buttons on click of another button.
var td = new ToolStripDropDown
{
AutoSize = true,
DropShadowEnabled = false,
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
var host = new ToolStripControlHost(panel)
{
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
td.Items.Add(host);
The panel contains list of buttons to be displayed. To show the panel to user, on button(Show) click following line is called.
td.Show(pointonScreen);
By default, AutoClose is set to true. So whenever user clicks anywhere in the form, the toolstripdropdown is getting closed. This is ok.
My requirements:
Click Show button
Display the toolstripdropdown by calling td.show() and close the popup if td.Visible
Again click the Show button
toolstripdrown should be closed
Click anywhere in the form, toolstripdropdown should be closed if it is visible
What is happening now is, on step 3, before the button click event is raised, toolstripdropdown is getting closed. So again the dropdown is being displayed.
Is there any other way to achieve my requirements?
You should handle Closing event of the dropdown and set a flag if the dropdown is closing by click on the button which opened it. Then when you click on button, check the flag and if there wasn't a flag, show dropdown and set the flag, otherwise close the dropdown and clear the flag:
ToolStripDropDown td;
private void Form1_Load(object sender, EventArgs e)
{
td = new ToolStripDropDown { /*...*/};
var host = new ToolStripControlHost(this.panel1){ /*...*/};
td.Items.Add(host);
td.Closing += td_Closing;
}
void td_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
if (this.button1.Bounds.Contains(this.PointToClient(MousePosition)))
{
td.Tag = true;
return;
}
td.Tag = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (td.Tag == null)
{
td.Show(Cursor.Position);
td.Tag = true;
}
else
{
td.Close();
td.Tag = null;
}
}

Close dialog, when pressed OK button on it

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();

Accept Button doesn't work

I have a problem with the accept-button inside a Windows Form.
The Form contains two buttons (OK and Cancel). Inside the Form I set the properties of the cancel and accept - Button to the specific buttons.
In addition to that I also created a simple Click - Event for both buttons.
But when I run the application and press enter the breakpoint inside my Click-Method doesn't get hit und nothing happens. On the other hand, the cancel button just works fine. Even if I switch the accept- and cancelbutton the acceptbutton doesn't work and the application seems to ignore the enter-input.
I have looked up the designer several times but couldn't find anything which could lead to this behaviour.
The Click Event itself also works fine when clicking the button, it's just about the enter-input.
So my question is: Does someone have a clue where this strange behaviour comes from?
Designer:
//
// SearchForm
//
this.AcceptButton = this.BtnSearch;
this.CancelButton = this.BtnCancel;
//
//BtnSearch
//
this.BtnSearch.DialogResult = System.Windows.Forms.DialogResult.OK;
this.BtnSearch.Location = new System.Drawing.Point(12, 60);
this.BtnSearch.Name = "BtnSearch";
this.BtnSearch.Size = new System.Drawing.Size(75, 23);
this.BtnSearch.TabIndex = 1;
this.BtnSearch.Text = "Search";
this.BtnSearch.Click += new System.EventHandler(this.BtnSearch_Click);
//
// BtnCancel
//
this.BtnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.BtnCancel.Location = new System.Drawing.Point(108, 60);
this.BtnCancel.Name = "BtnCancel";
this.BtnCancel.Size = new System.Drawing.Size(75, 23);
this.BtnCancel.TabIndex = 5;
this.BtnCancel.Text = "Cancel";
this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
Form:
private void BtnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void BtnSearch_Click(object sender, EventArgs e)
{
//DoStuff
}
Check what control has the focus when you press Enter. If that's a button then the key stroke is going to click that button, not the AcceptButton.
That makes AcceptButton a pretty lame property for dialogs that have more than an OK and Cancel key. Favor doing it like this instead:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Enter) {
btnSearch.PerformClick();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Are you showing the form as modal dialog? I think accept and cancel buttons only works for modal dialog. The example given in MSDN article shows modal dialog.

How do I create a event handler for a code that draws out the GUI for Windows Form Application

I am currently using the code in main.cs to draw out the GUI instead of using the [design] form. I understand that if you are using the [design] file to draw your GUI, all you have to do to create a event handler is just to double click the object.
But since I am using the code the draw out the labels/buttons, how do I do a event handler for that particular button/object?
p.Controls.Add(new Button
{
Text = "Clear",
Name = "btnClear",
Location = new Point(416, 17)
});
e.g. how do I add an event handler for the above code?c
You need to first create an instance of your button and assign it to a variable. Then you can add an event handler by calling += on the Click event of the button.
// This is a method body
{
Button btnClear = new Button
{
Text = "Clear",
Name = "btnClear",
Location = new Point(416, 17)
};
p.Controls.Add(btnClear);
btnClear.Click += new EventHandler(btnClear_Click);
}
void btnClear_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
In Visual Studio, after typing btnClear.Click += you can just press Tab twice and it will add the rest of the code.

Categories