After I touch a control, such as Button, How can I get this button's name?
If you mean "click" instead of "touch", put something like this in your View XAML file:
<Button x:Name="MyButton" MouseDown="MyButton_MouseDown">
Then add following lines to the View CODE file:
void MyButton_MouseDown(object sender, MouseButtonEventArgs e) {
base.OnMouseUp(e);
var control = sender as FrameworkElement;
Console.WriteLine(control.Name);
}
When you click on a control you are raising an event which called : Control_Click
Then you can do whatever you want in that even. As you asked about button I'm gonna show you how you can get button's name . You can have the similar code for other controls :
private void button1_Click(object sender, EventArgs e)
{
string control_name = button1.Name;
MessageBox.Show(control_name);
}
Related
so what i want to do is, on Event ButtonClick disable parent Control of this button.
In my Form i have several Panel's in which those button actually are.
I am using following code:
private void button1_Click(object sender, EventArgs e)
{
Control control = button1.Parent;
control.Enabled = false;
}
This xode is working fine, but I wanted to use this.Parent instead of button1.Parent, so that each button would be able to disable its own parent Panel(in this case will disabled Panel of button1).
When i am using this.Parent I get a System.NullReferenceException.
Knows some one why i am getting this error ?
this is your current class you want to use the sender and cast it to Control than get it's parent something like this
private void button1_Click(object sender, EventArgs e)
{
var uc = sender as Control;
uc.Parent.Enabled = false;
}
this represents the Window, which has no Parent.
You can do the following:
private void button_Click(object sender, EventArgs e){
Control control = ((Button) sender).Parent;
control.Enabled = false;
}
Button[] buttons = {button, button2....};
foreach (Button button in buttons)
button.Click += button_Click;
Alternatively, you can create a class inheriting from Button. You can then add a Click event handler that disables it's parent control. You can then use this button instead of the default one.
If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.
I have a GridView, and in this Gridview there are several Grids which contain other elements. If I click onto one element of my GridView (one Grid) I want to open details about this element, but Grid and GridView dont seem to support setting a click method. What else can I do to call a method when an element is clicked?
but Grid and GridView dont seem to support setting a click method
Use Tapped event.
you could add an invisible rectangle or other element to register an OnMouseOver event or click ect.
Each of your Grid should subscribe to 3 events and have transparent background:
<Grid Background="Transparent" PointerPressed="Grid_OnPointerPressed" PointerReleased="Grid_OnPointerReleased" PointerExited="Grid_OnPointerExited">
in code you may simulate click event like this:
private bool _isPressed;
private void Grid_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
_isPressed = true;
}
private void Grid_OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (_isPressed)
{
//your logic on click event
}
_isPressed = false;
}
private void Grid_OnPointerExited(object sender, PointerRoutedEventArgs e)
{
_isPressed = false;
}
or the easiest way is to wrap your Grid into Button
In my MainWindow.xaml i have a Frame. The content of this Frame will change when i click a Button. So if I click a button to show private customers, the Frame Content shows the site of the private customers:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Privatecustomer.privatecustomer_show();
}
After clicking this Button the Frame will change the Content.
The new Content shows Privatecustomer.privatecustomer_show.xaml now.
In this xaml i have another Button. If i click this Button, the Content of the Frame in MainWindow should change to "Privatecustomer.privatecustomer_add.xaml".
But How I can tell from privatecustomer_show.xaml to MainWindow.xaml, that the Button addPrivatecustomer in privatecustomer_show is clicked and that the Main.Content have to change to
Main.Content = new Privatecustomer.privatecustomer_add();
?
I hope I can get some help here
How about adding an event in privatecustomer_show.xaml that the MainWindow.xaml can subscribe to.
Like this
public event EventHandler AddPrivateCustomer;
protected virtual void OnAddPrivateCustomerEventArgs e)
{
if (AddPrivateCustomer!= null)
AddPrivateCustomer(this, e);
}
Update: Please note the updated code, I made a copy'n'paste mistake in my first version.
Change your:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
To:
private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
var privateCustomerContent=new Privatecustomer.privatecustomer_show();
privateCustomerContent.AddPrivateCustomer+=onClick_addPrivateCustomer;
Main.Content = privateCustomerContent;
}
private onClick_addPrivateCustomer(object o,EventArgs e)
{
// Change Main.Content
}
The idea is that your privatecustomer_show control sends events for when it want to change something outside of what it has access to.
You MainWindow which has full control of all its child windows will then subscribe to each event.
Set Page2.Tag property to the instance of your MainWindow (use Binding in Xaml, or simply set it in code). After the specified Button in Page2 is clickd, simply use the Tag property (which is now your MainWindow).
Another option is to use
App.Current.MainWindow
When the Button is clicked. (A warning. I don't know the structure your project and this might not be the instance you are looking for).
I have an element with a Click method.
I would like to activate that method (or: fake a click on this element) within another function.
Is this possible?
No, there currently isnt a way to do this. See links:
https://stackoverflow.com/questions/4137528/ui-automation-for-wp7
windows phone 7: How to simulate control click programmatically
EDIT
This doesnt do exactly what you wanted to do, but you can do something like this and get the same outcome. I do this type of pattern alot in my code to do get the same outcome.
XAML
<Button Name="testClick" Click="testClick_Click">
<TextBlock Text="Click Me"></TextBlock>
</Button>
C#
private void testClick_Click(object sender, RoutedEventArgs e)
{
TestFunction(sender as Button);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Call same function as button
TestFunction(testClick);
}
private void TestFunction(Button bt)
{
//do stuff
}
Somewhere in your class initiate a RoutedEventArgs:
private RoutedEventArgs evnt;
Then later call this:
element_Click(element, evnt);
That should do it.