Inherited form problem - winforms

i have a windows form that inherites from another windows form in the same project,
the problem is when i click a button in the inherited form the eventhandler fires twice
performing the event in the parent form and then performs the event in the inherited form
.
any suggestions?
http://www.metacafe.com/watch/852308/inheritied_forms_c/
in this video u will see what am doing exactly and the problem am facing is in the end of this video .. u will see exactly what i mean –

This is the way it's supposed to work. You have two event handlers, both are executed. I would suggest the following:
In the parent form, add a method
Protected Overridable Sub OnOKButtonClick()
MsgBox("Parent Form OK Button Clicked")
End Sub
which you call from the button's click event. In the inherited form, remove the button click event handler, and override the OnButtonClick method
Protected Overrides Sub OnOKButtonClick()
MsgBox("Inherited Form OK Button Clicked")
End Sub
This should accomplish what you are after.

Related

Handling click events for dynamically created buttons in array

I have an array of buttons that are created through code. The reason why I've done this is because the amount of buttons could change, and in this case, I'm trying to create buttons that are displayed as days in a month on a calendar, and the amount can't be at a set amount because if the amount of buttons were being created for February, the amount would either be 28 or 29.
I've done this how I want to, however the problem comes at having to handle a click event for each button. Since I'm looking for the general idea how to handle a click event in the below example, I want to messagebox what the content is for the button.
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length
btns(ButtonCount) = New Button With {.Content = ButtonCount}
'Handler goes here.
Next
The way that I reference these buttons individually is through btns(ButtonCount), I do not name them.
So is there a way to add a click event to these buttons created in the example?
Use the AddHandler statement to assign an event handler to your button. In your event handler check which button was pressed.
First of all, take a look at the documentation as suggested in the comments.
(AddHandler Doc.)
Second, you could follow this example to achieve what you want to do:
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length - 1
btns(ButtonCount) = New Button With {.Content = ButtonCount}
AddHandler btns(ButtonCount).Click, AddressOf OnBtnClick
Next
Private Sub OnBtnClick(sender As Object, e As RoutedEventArgs)
'Your Event Handling
End Sub

Windows Form IsClosing flag?

Is there a way to know if a Windows Form is closing?
I would have to know from another class with a reference to the windows form. Something like:
if WinForm.IsClosing then ....
How about using the event Form.FormClosing Event
The FormClosing event occurs as the form is being closed. When a form
is closed, it is disposed, releasing all resources associated with the
form. If you cancel this event, the form remains opened. To cancel the
closure of a form, set the Cancel property of the FormClosingEventArgs
passed to your event handler to true.

call a button of different form in c# Forms?

I am new to C#, I am creating a application in which there is a need of using two forms
one is Mainform and other is DialogForm.
The DialogForm has two buttons btnYes and btnNo.
whenever the user clicks the close button, FormClosing Event invokes in which I am calling the DialogForm as shown below:
DialogForm ex = new DialogForm();
ex.ShowDialog(this);
Now I want to give e.cancel=false for btnYes and e.cancel=true for btnNo. (this explained by my sir, only basics)
I know how to give functions to a Button which is in same Form but I dont know how to if the Form is different.
I have gone through some links but as I am new to c#, I can't understand it.
If you atleast provide me some links that would be appreciable.
Thanks in advance.
Forms have a property DialogResult. You can set it in the button event handlers.
DialogResult = DialogResult.Yes;
// or
DialogResult = DialogResult.No;
Then you can call the form like this
if (ex.ShowDialog(this) == DialogResult.Yes) {
// Do something
} else {
// Do something else
}
You can also set the CancelButton property of the form in the properties window. Microsoft says:
Gets or sets the button control that is clicked when the user presses the ESC key.
The form has also an AcceptButton property. Microsoft says:
Gets or sets the button on the form that is clicked when the user presses the ENTER key.

DevExpress XtraGrid checkbox check not registered unless focus changes

We have a databound XtraGrid on our Windows form. One of the columns is a check box. The problem is as follows: when users check the checkbox and click OK button, the checkbox, while visibly checked, is not considered checked by the grid. When I do this (while looping through rows):
isAllowed = Convert.ToBoolean(viewMain.GetRowCellValue(nRowCtr, "IsAllowed"))
I get back False. BUT, if the user checks the box, and then clicks somewhere else on the form or on another row in this grid, thus taking away focus from the checked checkbox, the same code above will return True.
Any insight on how to fix this behavior would be greatly appreciated.
Workaround found:
With default settings, when users click on a cell to edit it, the cell goes into edit mode, loads the editor control (in this case I have a CheckEdit repository control) and changes control's value (in this case checked state). If I click on another row, or another control, the cell then gets out of edit mode, committing the change to data item. But if I click on a button, then my change is lost. The workaround is to use the CheckEdit's CheckedChanged event to close editor:
Private Sub edCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edCheck.CheckedChanged
gridYears.FocusedView.CloseEditor()
End Sub
There's actually a cleaner way of doing this (it works for all RepositoryItems), detailed on the DevExpress site. The idea is to call the GridView.PostEditor method from a repository item's EditValueChanged event handler to immediately save the edited value to the grid's cell and the underlying column.
This code in the view's CellValueChanging event handler solved the problem:
private void OnCellValueChanging(object sender, CellValueChangedEventArgs e)
{
_gridView.SetFocusedRowCellValue(_gridView.FocusedColumn, e.Value);
}

How to close a dialogue box from some custom link inside the dialogue box

I have custom control which I am rendering inside dialogue box.
this custom control has a link lable lnkLable. I want to close the opened window when I click on lnkLable.
right now I am finding the parent of my conrol which will be dialogue control in the end and then calling the dispose method of that, which I don't feel very good technique to do this.
Use the Close() method on the form to close it.
private void button1_Click(object sender, EventArgs e)
{
Control btn = sender as Control;
Form frm = btn.Parent as Form;
frm.Close();
}
If it's a modal dialog you can also close it by calling the Hide() method because modal dialogs are automatically destroyed the modal pump exits, and the pump will exit when the dialog is hidden.
Rather try using Control.FindForm Method
You have to remember that the control might not be directly on the form, but inside another container, like a panel, in which case the parent of your control will not be the form.
Once yuo have the instance of the form, rather use Form.Close Method

Resources