Handling multiple clicks on uwp button - c#

I would like to have a different method called depending on the number of time I click on a button, from 1 to 5, on an UWP application.
Is it possible ? How ?

I've used something like this in my windows forms application, maybe it will help. This switch operation was inside button click event.
int clickCounter = 0;
switch (clickCounter)
{
case 1:
{
}
break;
case 2:
{
}
break;
}

Well, one or two clicks is built-in:
var b = new Button();
b.Tapped += (s, e) => { /* TODO */ };
b.DoubleTapped += (s, e) => { /* TODO */ };
But if you want three clicks, say, you need john.kernel's solution (above). Of course, you would use only Tapped to track, then some kind of timing threshold, too. Otherwise they could tap twice in 5 minutes and John's code would fire.
I don't mind interjecting that a triple-click is lame. Not discoverable. But, hey, I don't know your app or your user base, do I? So, you do what you think is right. You are the developer.
Best of luck!

Related

Xamarin: Is there a Way to Differentiate Between a Swipe and a BottomNavigationView Click?

I am wondering if it's possible to differentiate between a swipe and a click on the BottomNavigationView in Xamarin.Android.
I've implemented:
void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
if (_viewPager.CurrentItem == 0)
{
_fm1.Pop2Root();
}
_viewPager.SetCurrentItem(e.Item.Order, true);
}
but there is no differentiation between a swipe and a click. I want to keep the current page loaded if the user swipes, but pop to the root if the user has clicked on the currently selected BottomNavigationView tab.
And here's what my Pop2Root method looks like (not that it really matters):
public void Pop2Root()
{
_wv.LoadUrl("https://www.bitchute.com/");
}
I just want a separate event for click versus swipe.
I'm not looking for anyone to do my work. I will post the full solution (as always) once I've figured it out. What I'm looking for is a yes or no answer whether or not it's possible; then I'll take care of the rest. I've implemented a click listener on the TabHost before, but that's a completely different UI element:
https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.7641/Activities/ClickListeners.cs
If you would like more context on the complete project, here's the MainActivity.cs then you can back into the rest:
https://github.com/hexag0d/BitChute_Mobile_Android_BottomNav/blob/master/MainActivity.cs
Thanks, in advance
The answer to this question is yes. The ViewPager_PageSelected method is invoked when user swipes. The NavigationView_NavigationItemSelected is invoked on a tab press. Interestingly, if ViewPager_PageSelected method is put before NavigationView_NavigationItemSelected method, ViewPager_PageSelected won't be invoked when the user presses the a tab until after this method is called:
_viewPager.SetCurrentItem(e.Item.Order, true);
After that happens, the ViewPager_PageSelected method is invoked and NavigationView_NavigationItemSelected gets invoked again. So I decided to do the order like this and set a custom int. This way, both methods are only called once per user interaction, and there is differentiation.
(Note events BottomNavigationView.NavigationItemSelectedEventArgs & ViewPager.PageSelectedEventArgs)
//put all of this inside your MainActivity.cs
int _tabSelected;
void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
if (_tabSelected == e.Item.Order)
{
switch (_viewPager.CurrentItem)
{
case 0:
_fm1.Pop2Root();
break;
case 1:
_fm2.Pop2Root();
break;
case 2:
_fm3.Pop2Root();
break;
case 3:
_fm4.Pop2Root();
break;
case 4:
_fm5.Pop2Root();
break;
}
}
else
{
_viewPager.SetCurrentItem(e.Item.Order, true);
}
}
private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e)
{
_menu = _navigationView.Menu.GetItem(e.Position);
_navigationView.SelectedItemId = _menu.ItemId;
_tabSelected = _viewPager.CurrentItem;
}

Execute specific blocks of code based on which key on the keyboard is pressed

I am very new to the programming world and recently dove into c#. I don't want to waste your time so I'll get right to it. I wanted to create a program just to test my knowledge, and thought I could attempt to execute specific blocks of code based on which key on the keyboard is pressed by the user. I tried doing this by creating an event handler that contained if statements, but then realized I didn't know how to have the event handler active in the program.
For example, and as you can see in the below snippet, after the WriteLine in Line 5 lets say I wanted to raise the EventKeyPress event so that it waits for user input and reads the key they have pressed and reacts accordingly, how would I do that?
Again, I'm almost a complete beginner and have searched around for explanations about event handlers for hours and still can't wrap my head around what I am supposed to do or if I am even using the event handler correctly. Thanks in advance!
static void Main();
{
if (search == "Ball")
{
Console.WriteLine("Press enter to exit or backspace to return to the search bar")
// RIGHT HERE
}
else
{
Console.WriteLine("Sorry, I don't recognize {0}", search);
}
void EventKeyPress(object sender, KeyPressEventArgs e)
{
for (int i = 0; i < 1;)
{
if (e.KeyChar == (char)Keys.Enter)
{
// exit app
}
else if (e.KeyChar == (char)Keys.Back)
{
// go back to search
}
else
{
i = 0; // error
}
}
}
}
So, you're asking for something that involves Threading which is not a beginner thing to accomplish at all. The best way to do this for a beginner is to ask for a prompt, then accept as an input. For example.
Console.WriteLine("Hello, what's your name?");
string nameStr = Console.ReadLine();
Console.WriteLine($"Hello, {nameStr}");
You can then use your variable and apply it to an if/while or whatever kind of conditional.
if (nameStr == "Matt"){
//Do This Code.
}
Once you have that code, add a sequential method that will ask the user to return to the main menu or whatever you want it to do.
Main.ReturnMenu(); //Or whatever you want to use.

How to implement a graphical 3-Way "Switch"

I'm trying to build a 3-Way "Switch" in my WinForms project.
It only sends one command for all three "settings", but should alternate between 3 different background images each time the user clicks on the button. I've already implemented a 2-Way toggle switch into my project by using a CheckBox with it's appearance set to "Button", but I don't believe this method will work for a 3-Way switch.
Here is the code that I've tried, but it doesn't seem to do anything when the button is clicked:
private void ThreeWayButton_Click(object sender, EventArgs e)
{
if (ThreeWayButton.BackgroundImage.Equals(Properties.Resources.ThreeWay_1))
{
ThreeWayButton.BackgroundImage = Properties.Resources.ThreeWay_2;
}
else if (ThreeWayButton.BackgroundImage.Equals(Properties.Resources.ThreeWay_2))
{
ThreeWayButton.BackgroundImage = Properties.Resources.ThreeWay_3;
}
else if (ThreeWayButton.BackgroundImage.Equals(Properties.Resources.ThreeWay_3))
{
ThreeWayButton.BackgroundImage = Properties.Resources.ThreeWay_1;
}
}
Another method I tried is using a switch:
static int switch_state = 0;
//...
protected void ThreeWayButton_Click(object sender, EventArgs e)
{
switch_state++;
switch (switch_state)
{
case 1:
ThreeWayButton.BackgroundImage = Properties.Resources.ThreeWay_2;
break;
case 2:
ThreeWayButton.BackgroundImage = Properties.Resources.ThreeWay_3;
break;
case 3:
ThreeWayButton.BackgroundImage = Properties.Resources.ThreeWay_1;
break;
default:
break;
}
}
This method kind of works; it cycles through the three images, but once it gets to the last one, it doesn't cycle through the images again.
If the second method is the appropriate one to use, I'd like it to revert back to case 1 after the user clicks on the button when the switch_state is case 3
It should cycle between the three images each time the user clicks the button, no matter how many times the button has been clicked.
Your second approach is the good one, you just need to add:
if(switch_state > 3)
switch_state = 1;
Just after your switch_state++, else it will continue incrementing thus doing nothing.

How to execute code when Universal Windows Store App is closing?

I am developing an app for Windows 10 Store app, but I can't seem to find/learn on how to check if the user pressed the red close button (at the top right) or by pressing Alt + F4. Basically what I want is something like this:
private void app_Close(object sender, CloseEventArgs e)
{
//saves some data in the app :D
}
.: EDIT :.
If you have a Universal app that doesn't have a MainWindow object you'll probably want to tap into the "Suspending" event:
appObject.Suspending += (s, a) =>
{
SaveTheData(); // I really like my data and want it for later too
};
or
public App()
{
/*
stuff
*/
Suspending += (s, a) =>
{
SaveTheData(); // I really like my data and want it for later too
};
}
.: Original :.
Add a handler to your MainWindow "Closing" event to save your data. Once "Closing" has finished, "Close" should fire normally.
theMainWindowObject.Closing += (s,a) =>
{
SaveTheData(); // It's precious!
};
I have something similar to this in one of my smaller applications where in the constructor for MainWindow I put the above snippet substituting "theMainWindowObject" for "this" so that it references itself
So I have:
public MainWindow()
{
// Note: "this." isn't necessary here but it helps me with mental accounting
this.Closing += (s, a) =>
{
Save();
};
}
If you're just saving one or two properties and don't have any crazy logic going on you can just drop it right in the handler:
public MainWindow()
{
Closing += (s, a) =>
{
Properties.Settings.Default.SettingsPopupX = mySettingsPopupObject.GetX();
Properties.Settings.Default.SettingsPopupY = mySettingsPopupObject.GetY();
Properties.Settings.Default.Save();
};
}
If you want to manage your Application Lifecycle have a look at this MSDN article: https://msdn.microsoft.com/en-us/library/windows/apps/mt243287.aspx
In general you don't have a "close" event in Windows Apps. Your App will be suspended when Windows or your user wants to close it. Using the ApplicationExecutionState (see more) enum you can find out who (windows or your user) closed the app.
Hope it helps!

How to prevent tab item selection change event?

I am working on a wpf .My requirement is to change selection of tab according to user confirmation it means every time when user changes tab a message box opens and confirm with user whether he wants to change the tab or not.
But problem with me is when I press no first time it works fine .but after that on second time it asks two times for user confirmation
can anyone help me to solve this ?
private void tabcontrol_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
try
{
if (handleSelection && e.OriginalSource == tbUserProfileMainControl)
{
//Ask user for change
if (isUserAllowedToChanged)
{
int currentIndex = (tabcontrol.SelectedIndex);
GeneralDeclaration.currentSelectedTabIndex = currentIndex;
LoadUserControl(GeneralDeclaration.currentSelectedTabIndex);
}
else
{
//e.Handled = true;
handleSelection = false;
tbUserProfileMainControl.SelectedIndex = Math.Abs(tbUserProfileMainControl.SelectedIndex - 1);
}
}
handleSelection = true;
}
catch (Exception ex)
{
//
}
}
It sounds like you're adding handlers during the click event itself. This causes your subsequent click to perform the action one more time (3rd click 3 times, 4th click 4 times, etc).
Check how you bind the event to the handler and check where you are defining the handler itself. You're doing something twice that should only be done once.
This is my estimation based on your findings, without code, I'm just taking a wild stab in the dark.

Categories