I have a canvas ,inside it an image control which has events ManipulationDelta Event , Click Event etc etc.
I have an ADD button which add an image control to the canvas
Image image = new Image();
string url = "ms-appx:///Assets/992829_484663934955807_859212711_n.jpg";
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri(url, UriKind.Absolute);
image.Source = bm;
image.Height = 100;
image.Width = 100;
MyCanvas.Children.Add(image);
Now I want to create same events for this controls and many other also dynamically so that they have their own ManipulationDelta and Click events . How to achieve that ? Should I use Anonymous methods,Lambda Expressions etc. Just a hint which will guide me??
subscribe event like this
image.ManipulationDelta += image_ManipulationDelta;
image.Tapped += image_Tapped;
void image_Tapped(object sender, TappedRoutedEventArgs e)
{
//do something
}
void image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
//do somthing
}
in Visual Studio .. just type image.ManipulationDelta += and then press Tab 2 times. It will create event handler method automatically
also you can create event handler by using Anonymous method
image.ManipulationDelta += (sender, e) =>
{
//do something
};
Related
I am attempting to learn c# and wpf. I have a segment of code that works in that it shows the controls correctly. I also attempt to create a mouse button handler and when I debug it, the handler is never called. The buttons are nested within a couple of StackPanels, that can't seem to be the problem?
StackPanel tabPanel = new StackPanel();
for (int i = 0; i < 20; i++)
{
StackPanel micPanel = new StackPanel();
micPanel.Orientation = Orientation.Horizontal;
// Add the Calibration Button
Button micButton = new Button();
micButton.Height = 25;
micButton.Name = string.Format("Mic{0}", i);
micButton.Content = string.Format("Mic{0}", ((20 * index) + i));
micButton.MouseLeftButtonUp += new MouseButtonEventHandler(mic_MouseLeftButtonUp);
micPanel.Children.Add(micButton);
// Add the calibrated Value
Label micLabel = new Label();
micLabel.Height = 25;
micLabel.Content = string.Format("Value: {0}", ((20 * index) + i));
micPanel.Children.Add(micLabel);
tabPanel.Children.Add(micPanel);
}
tab.Content = tabPanel;
The handler looks like this:
private void mic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Button but = sender as Button;
}
I set a breakpoint and it never calls the handler?
This is typical: use the preview event handler to make sure it is raised first in the tree view.
micButton.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(mic_MouseLeftButtonUp);
Why don't you handle the Click event of the Button?
micButton.Click += new ClickEventHandler(mic_Click);
...
private void mic_Click(object sender, RoutedEventArgs e)
{
Button but = sender as Button;
}
Certain controls do "swallow" certain events but the Button control doesn't raise any MouseButtonUp event. It raises a Click event. The Click event is actually a combination of the LeftButtonDown and LeftButtonUp event:
WPF MouseLeftButtonUp Not Firing
You can read more about routed events and how they work in the documentation on MSDN: https://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx
I have a loop that could create 1 or maybe 10 or maybe 584575 (example, not actually true) FlowLayoutPanels. For all these panels I want a hover event handler, or maybe later another type of event handler but for now only hover.
How can I make this happen for multiple same type created controls?
FlowLayoutPanel finalResult_panel = new FlowLayoutPanel{
FlowDirection = FlowDirection.LeftToRight,
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle,
Name = "result_flowLayoutPanel" + i,
Size = new System.Drawing.Size(790, 72),
TabIndex = i,
};
You can attach the handler like this
finalResult_panel.MouseHover += panel_MouseHover;
private void panel_MouseHover(object sender, EventArgs e)
{
}
Alternatively you can create an anonymous delegate
finalResult_panel_MouseHover += (s,e) => {
//event code
};
These will attach the same handlers to every panel so if you need to differentiate, you can do that in the handler itself (using the sender property) or somehow differentiate before attaching the handler.
In C# WPF, I'm trying to create a new picturebox on the point where I clicked, whenever I click on the main window. I'm not too sure how to approach this since I could not find anything on the internet about this.
Are you sure you want to create new one each time? That could potentially be a bad idea. But still, if you want to, You can do it in codebehind like this:
public MainWindow()
{
InitializeComponent();
MouseUp += MainWindow_MouseUp; //add eventhandler vor click event
}
void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
{
var img = new Image(); //create new instance of image
img.Width = 100; //set some size properties
img.Height = 100;
img.Source = somesource;//set source
MainGrid.Children.Add(img); //add it as a child to some conteiner element, like grid.
}
But please reconsider reusing controls, if possible.
Background.
I have a very simple picture box on which I am streaming video using expression encoder sdk 4.
Problem:
I am unable to get a double click event on it.
deviceSource.PreviewWindow = new PreviewWindow(new HandleRef(PicBox, PicBox.Handle));
PicBox.MouseDoubleClick += new System.EventHandler(this.PicBox_DoubleClick);
private void PicBox_DoubleClick(object sender, EventArgs e)
{MessageBox.Show("");
}
What am I missing ?
You need to specify what event you wish the DoubleClick function to be called for
PicBox += new System.EventHandler(this.PicBox_DoubleClick);
Should be
PicBox.MouseDoubleClick += new System.EventHandler(this.PicBox_DoubleClick);
here is the basic model of the code I have:
private void textBlock1_Tap (object sender, System.Windows.Input.GestureEventArgs e)
{
TextBox TextBox1 = new TextBox();
TextBlock tblk = (TextBlock)sender;
ApplicationBar = new ApplicationBar();
TextBox1.LostFocus += TextBox1_LostFocus;
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/check.png", UriKind.Relative));
appBarButton.Text = "Accept";
ApplicationBar.Buttons.Add(appBarButton);
appBarButton.Click +=
}
void TextBox1_LostFocus(object sender, RoutedEventArgs e)
{
//do things here
}
I need to subscribe to the click Event, and when it is triggered, I need TextBox1_LostFocus to be called and TextBox1 to be sent as a parameter. Basically what I want to do is make appBarButton.Click do the exact same thing as TextBox1.LostFocus.
Problem is, LostFocus is a RoutedEventHandler and TextBox1_LostFocus takes a RoutedEventArgs as a parameter while appBarButton.Click is an EventHandler.
I'm not very experienced in coding at all so any help is much appreciated!
RoutedEventArgs inherits EventArgs.
You can add the same handler to both events.
Better yet, you can move the code to a function and call it from two different event handlers.