A parent window is designed having keyup event attached.
The MainWindow goes like:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e != null && e.Key == Key.Return)
{
MessageWindow msgWindow = new MessageWindow("KEY UP");
msgWindow.Show();
}
}
}
The MessageWindow having a button OK to close the window, goes like:
public partial class MessageWindow : Window
{
public MessageWindow()
{
InitializeComponent();
}
public MessageWindow(string message) : this()
{
txtMess.Text = message;
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
On receiving the input key as [Return] on parent, a new child window is initialized and is displayed.
Press TAB to focus OK button and press Enter.
The child MessageWindow pops up again.
Reason: Parent window receives KeyUp event when RETURN key is pressed on child window to close it.
Please provide a way to stop the handling of KeyUp event by parent, other than using a FLAG.
Try to use PreviewKeyDown event instead of KeyUp one
Related
I want a normal cursor when I mouse over items in a WPF tree, unless the control key is down, in which case I'd like it to be a hand cursor.
How would you suggest I go about doing this?
You could handle the PreviewKeyDown and PreviewKeyUp events for the window and set the Cursor property, e.g.:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
PreviewKeyDown += (s, e) =>
{
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
this.Cursor = Cursors.Hand;
};
PreviewKeyUp += (s, e) =>
{
this.Cursor = null;
};
}
I have a windows usercontrol which in turn has a elementhost (to display wpf content inside the usercontrol).
public partial class HostControl : UserControl
{
internal ElementHost elementHost { get; set; }
internal System.Windows.Controls.Control Content { get; set; }
public HostControl()
{
InitializeComponent();
}
private void HostControl_Load(object sender, EventArgs e)
{
this.elementHost = new ElementHost();
this.elementHost.Dock = DockStyle.Fill;
this.elementHost.Child = this.Content;
this.Controls.Add(this.elementHost);
}
public void CloseWindow(object sender, EventArgs e)
{
this.FindForm().Close();
}
}
The HostControl.Load event is bound to HostControl_Load event handler (above).
The closewindow eventhandler from the hostcontrol (above) is bound to a button in the wpf view
HostControl control = new HostControl();
AView aView = new AView(); //WPF View
control.Content = aView;
if(aView.DataContext != null && aView.DataContext is AViewModel )
{
var viewModel = aView.DataContext as AViewModel;
viewModel.CloseRequest += control.CloseWindow;
}
WPF view is a usercontrol and the xaml code looks like
<UserControl.DataContext>
<local:AViewModel/>
</UserControl.DataContext>
A button inside this wpf view is bound to the OK command handler below
public class AViewModel
{
public event EventHandler CloseRequest; //bound to hostcontrol's CloseWindow handler
private async void OK(object parameter)
{
await Generate();
CloseRequest(this, EventArgs.Empty)
}
private async void Generate()
{
//Some processing here
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
The problem I am facing is as follows.
1) As soon as the await Generate() completes, the CloseRequest triggers the CloseWindow event handler in HostControl .
2) If I do not close the wpf view, the Generate() method completes and calls the CLoseWindow handler- FindForm() has a valid parent and the view closes.
3) If I close the wpf view and re-open the view while the Generate() method is under execution, later once CloseWindow handler gets executed FindForm() is Null and a null reference exception occurs ( basically the hostcontrol's Parent is null after re-opening the form)
4) I notice that using the async and await constructs is causing an issue (?)
My question is why would the Parent of the Windows Form UserControl be set to null on re-opening the window ?
Is there an event for a UserControl that only fires the first time it is added to a form? I basically want to show a memmo on how to use the control to the user once they add the control into the form, but never show it afterwards (until they add another instance of this control).
An easy way to do that could be:
Use the event ParentChanged from Control : Control.ParentChanged Event
UserControl is an heritance of Control
In your UserControl, you could use a private field to define if the item has been already added to a parent.
This code could work for you:
public class CustomUserControl : UserControl
{
private bool _firstTimeAdded = false;
private void Init()
{
this.ParentChanged += CustomUserControl_ParentChanged;
}
private void CustomUserControl_ParentChanged(object sender, EventArgs e)
{
if (Parent is Form && !_firstTimeAdded)
{
_firstTimeAdded = true;
// Show your memmo
}
}
}
How do i detect when enter is pressed in a textbox in visual studio 2013 forms application?? I've tried using textbox1_OnKeyPress, KeyUp, KeyDown, KeyPress, everything I could find in google searches, and none of it worked... is it a check I have to make in textChanged??
Add a textbox in your form and handle the KeyDown event of that textbox.
public class Form1
{
public Form1()
{
InitializeComponent();
textBox1.KeyDown += new KeyEventHandler(this.KeyDownEvent);
}
private void KeyDownEvent(object sender, KeyEventArgs e)
{
if (e.KeyCOde == Keys.Return)
{
//Place your code here
}
}
}
I got a problem with usercontrol like this:
I have a formA contain 1 usercontrol with name UC_wrap
and UC_wrap contain a usercontrol with name UC_child
In UC_child contain : a button add, a button edit, a button delete and 1 textbox
but I don't know how do I create event for each button on formA?
please, Somebody help me !!!!
You can rig UC_Wrap with an event that forwards any events it receives from the button to whoever is subscribed to it.
partial class UC_Wrap : Control
{
public event EventHandler AddButtonClick
{
add { addButton.Click += value; }
remove { addButton.Click -= value; }
}
// etc
}
Then the UC_Control can forward those events
partial class UC_Control : Control
{
public event EventHandler AddButtonClickedInWrap
{
add { ucWrap.AddButtonClick += value; }
remove { ucWrap.AddButtonClick -= value; }
}
// etc
}
Then finally at the FormA level you can subscribe to the event and handle it.
partial class FormA : Form
{
protected override void OnLoad()
{
ucControl.AddButtonClickedInWrap += ActuallyDoSomething;
}
private void ActuallyDoSomething(object sender, EventArgs e)
{
// do something
}
}
That's probably the best way to do it. The only simpler way I can think of is to make each sub control public, but that has the major downside of exposing far more than is needed.
Try this
//UC_child - child user control code
public event EventHandler addClick;
public event EventHandler editClick;
public event EventHandler deleteClick;
//call above event in each button click ie.
private void btnAdd_Click(object sender, EventArgs e)
{
if (addClick != null)
addClick(sender, e);
}
//Do same for other edit and delete button
//UC_wrap- UC_wrap usercontrol code
//Hand UC_Child event in UC_wrap
//Create event again in UC_wrap
public event EventHandler addClick;
public event EventHandler editClick;
public event EventHandler deleteClick;
private void UC_Child_Load(object sender, EventArgs e)
{
UC_Child1.addClick += new EventHandler(add_Click);
//Do same for other edit and delete button
}
private void add_Click(object sender, EventArgs e)
{
if (addClick != null)
addClick(sender, e);
}
//formA-This is your form code
private void formA_Load(object sender, EventArgs e)
{
UC_wrap1.addClick += new EventHandler(add_Click);
//Do same for other edit and delete button
}
private void add_Click(object sender, EventArgs e)
{
//Place your code here.
}