call a button of different form in c# Forms? - winforms

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.

Related

C# how to make a child form topmost only inside the application?

I have 5 child forms in my c# application. one of the child forms named childFormopens another form subForm. What i want is, when subForm is open, user cannot click or do anything inside the application without closing subForm. But this should be contained in the application only. i.e When user wants to switch to another application without closing subForm he/she wont see subForm anymore, again if user switches to the c# application the subForm form must be on top and rest of the control must be disabled.
to create and show the subForm from within the childForm i wrote a button_click event
Imagine your form contains an editbox, a Button and a close Button (x).
This may help if we have the same SDK.
Button.click += delegate{
Subform();
}
Public void Subform(){
Button closebutton = Resource.Id...;
Button Enter = Resource.id...;
Edittext Edit = Resource.Id...;
Enter.enabled = false;
Edit.editable = false;
Closebutton.click += delegate{
Mainform();
}
}

Lightswitch modal window

I've got standard CreateNewEntity screen. Entity can contain list of entities of some other type. By default there is an add button that opens modal window when user wants to add another entity into collection. However, default modal window was lacking some of the needed functionality so I've done a bit of research. Turns out that default modal screens cannot be modified. So, I found a nice custom modal window helper class. The problem is that I can't seem to be able to access modal window fields in order to enforce needed logic. There are two dropdown lists that are associated. Change in one will result in limiting the other dropdown list options. I'm stuck at this particular part:
var proxy = this.FindControl("DodavanjeParcele");
proxy.ControlAvailable += (s, e) =>
{
var ctrl = e.Control as System.Windows.Controls.Control;
//how to obtain access to ctrl fields?
};
"DodavanjeParcele" is custom modal window. Before this, modal window is instantiated and initialized. It pops up after button click and functions as expected. The only thing missing are above-mentioned rules. I need to set change event handlers for modal window fields in order to define rules. As seen above I tried to cast IProxy as a standard Windows control. This is where I got stuck. I can't seem to find a way to access control fields and set event handlers. Any thoughts?
If I understand you correctly, I'm not sure why you need to search through controls or cast anything.
Control1 is an entity which creates an AutoComplete Box (dropdown list). That selection is copied into a local property in the Control1_Changed method. That property is used as a parameter in a filter query to create Control2.
C#:
private void Control1_Changed()
{
this.MyLocalProperty = this.Control1.SelectedItem;
}
VB.NET:
Private Sub Control1_Changed()
Me.MyLocalProperty = Me.Control1.SelectedItem
End Sub
Just make sure you have Auto Execute Query checked in Control2's Properties and the second control should update and filter when Control1 changes the query parameter.
The code in my screen shots all takes place inside of Yann's Modal Helper so there is nothing special you need to do.

Handling Key Pressed Events + WinForms + Validation + Cancel

I am implementing a WinForms form in a C# project.
My form is a child of a MDI form.
My form contains a user control.
My user control contains some buttons including a validation button and a cancel one.
I want to implement the following logic :
When my form is active and the user presses the enter key then I want the validation button clicked event to be fired automatically.
When my form is active and the user presses the escape key then I want the cancel button clicked event to be fired automatically.
If my validation and my cancel buttons were not included in a user control then I would probably set the AcceptButton and CancelButton properties of my form.
Here is the code I have written in the Load event handler of my user control according to a tip given by Arthur in a comment to my first post :
// Get the container form.
form = this.FindForm();
// Simulate a click on the validation button
// when the ENTER key is pressed from the container form.
form.AcceptButton = this.cmdValider;
// Simulate a click on the cancel button
// when the ESC key is pressed from the container form.
form.CancelButton = this.cmdAnnulerEffacer;
Set the KeyPreview Property of your from true from properties;
Add keyDownEvent to your Form
In keyDownEvent of your Form, include following lines of code
The code
if(e.KeyValue==13)// When Enter Key is Pressed
{
// Last line is performing click. Other lines are making sure
// that user is not writing in a Text box
Control ct = userControl1 as Control;
ContainerControl cc = ct as ContainerControl;
if (!(cc.ActiveControl is TextBox))
validationButton.PerformClick(); // Code line to performClick
}
if(e.KeyValue==27) // When Escape Key is Pressed
{
// Last line is performing click. Other lines are making sure
// that user is not writing in a Text box
Control ct = userControl1 as Control;
ContainerControl cc = ct as ContainerControl;
if (!(cc.ActiveControl is TextBox))
cancelButton.PerformClick(); // Code line to performClick
}
validationButton or cancelButton are the names of your buttons which I am just supposing. You may have different ones. Use Your names instead of these two if you have different.

WPF trigger code behind button to display second form

I am a WPF novice.
I have created a form containing a combo box with which to choose a multi-field key value(populated from an XML data file).
I have also created a second WPF form which is available to display all field values from the record associated with the multi-field key value chosen from the first form.
I need to be able to click a button which will cause the second form to be displayed, with all fields filled in which are associated with the chosen key field values.
How do I go about writing such an event trigger using C#?
couple of steps (this is not really MVVM, BTW) ...
first, add a click handler to your button
second, in the click handler code, instantiate your new form
third, set the data context, etc for the new form
forth, show the new form by calling .Show()
in your xaml add a click handler to the button in question....
<Button Click="myClickHandler"/>
in visual studio, you can right click the text in the click="" and choose to navigate to the handler and visual studio will generate the code for it for you.
in your click handler, in code behind, do something like this....
public void myClickHandler(object sender,EventArgs)
{
MySecondForm form = new MySecondForm();
form.DataContext = theDataContextIWantToSet;
form.Show();
}

Popup window and context menu

I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top

Resources