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.
Related
I have a button that I want to clear certain fields after it is pressed. I have a button event that is supposed to display a question before the clear functionality is implemented. After the user confirms they want to clear the button then the button will clear the text in the fields. However, as of now the dialog isn't displaying. Below is a sample of my Clear Button function. Please let me know if you see anything I am not seeing.
void btnPalletClear_Click(object sender, EventArgs e)
{
dialog = new Dialog(this, Android.Resource.Style.ThemeHoloLightDialogNoActionBarMinWidth);
View myView = View.Inflate(this, Resource.Layout.confirmation_dialog, null);
myView.FindViewById<TextView>(Resource.Id.txtConfirmTitle).Text = "Clear Pallet";
myView.FindViewById<TextView>(Resource.Id.txtConfirmMessage).Text = "Are you sure?";
myView.FindViewById<LinearLayout>(Resource.Id.llQuantity).Visibility = ViewStates.Gone;
myView.FindViewById<Button>(Resource.Id.cmdConfirmCancel).Click += delegate { dialog.Dismiss(); };
myView.FindViewById<Button>(Resource.Id.cmdConfirmOK).Click += delegate
{
dialog.SetContentView(myView);
dialog.Show();
txtPalletUNQ.Text = "";
adapter.lstPallet.Clear();
adapter.NotifyDataSetChanged();
txtPalletVTPID.Text = "";
};
}
I cut the dialog.SetContentView(myview), and dialog.Show() out and pasted it in outside of the cmdConfirmOk and it fixed it.
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
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;
}
I have next issue, I am working on small c#-wpf application,
and on load I am disabling button which is ussualy used to do some action on click, and it looks like this:
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
if (String.IsNullOrEmpty(password.Password))
{
btnActivate.IsEnabled=false;
}
}
and somewhere I am checking my password field, for example:
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
if (password.Password == "drag0n")
{
btnActivate.IsEnabled = true;
}
else
{
btnActivate.IsEnabled = false;
}
}
}
And my problem is next, when user enter "drag0n" and press enter, button should be just enabled, but its not only enabled, its calling automatic his event _Click, I don't know why does that happening, because in that case, if my button is just enabled event _Click is also called, and if user clicks on that button, event is called again, so actually my event onclick is called twice.
My question is how can I stop calling my Click event if I set IsEnabled=true. When I set IsEnabled=true I just want it to be enabled for pressing and I don't want execute event _Click.
I want to execute event _Click only when my button is pressed as it should work and not on IsEnabled=true.
Thanks guys,
Cheers
On click event occurs when you press Enter key, because this button is the default control in a form.
If you don't want click event on Enter key, you should either make this button not default or not process Enter key pressing in your button click (e.Handled = true -> when Enter is pressed).
Or try to change your code:
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
if (password.Password == "drag0n")
{
e.Handled = true; // add this line
btnActivate.IsEnabled = true;
}
else
{
btnActivate.IsEnabled = false;
}
}
}
I'm on to a small project where I try to make my own web browser.
I found out that a web browser is worthless without "New Tabs"-function, so I thought that I could use buttons as tabs and every time I press "ctrl + T" a new button appears.
The problems I encountered is:
-Array of buttons in a way that makes it possible for me to spawn a new button every time I press "ctrl + T"
-When the button is spawned it should be clickable and disabled when clicked until another tab (button) is click.
At the moment I focus on getting 1 tab to work, so here's an example:
private void TB_Address_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
{
Button tabButton = new Button();
tabButton = new System.Windows.Forms.Button();
tabButton.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
tabButton.Cursor = System.Windows.Forms.Cursors.Hand;
tabButton.ForeColor = System.Drawing.Color.Lime;
tabButton.Location = new System.Drawing.Point(154, 32);
tabButton.Name = "tabButton";
tabButton.Size = new System.Drawing.Size(152, 23);
tabButton.TabIndex = 13;
tabButton.Text = "Tab 2";
tabButton.UseVisualStyleBackColor = false;
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Controls.Add(tabButton);
}
}
I also have this click function:
private void tabButton_Click(object sender, EventArgs e)
{
tab_1.Enabled = true;
tabButton.Enabled = false;
}
"tab_1" is a button created in the design mode.
"tabButton.Enabled" is red marked because it cannot find tabButton.
I understand why it cannot be found. But I have no idea about how to solve the problem in a good way.
You are assigning the tabButton_Click to all buttons with this line:
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Just cast the sender to button and you will get the button who fired the event:
void tabButton_Click(object sender, EventArgs e)
{
Button buttonSender = (Button) sender;
buttonSender.Enabled=false;
}
You are not finding "tab_1" because it is not a valid name inside the tabButton_Click scope.
That's why you have to cast the sender object to WindowsForms Button, and then change its properties.
I'm going with a different method.
Creating all the buttons needed initially.
Sorry for wasting your time.