Click on a button within LongListSelector Windows phone 8 - c#

I have a LonglistSelector which displays a list a items, each item has a button:
My LongListSelector has a SelectionChanged event
private void MyLLS_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = (MyItemType)MyLLS.SelectedItem;
// Job 1 goes here
}
and each button in a item within LLS has a click event:
private void btDownload_Click(object sender, RoutedEventArgs e)
{
var button = (MyItemType)(sender as Button).DataContext;
// Job 2 goes here
}
Problem is, when i click a button, it'll process the job 2, and after that it'll process Job 1. So how can I just do the job2 of click-button event?

You should use a Boolean flag to determine it, just like:
bool isBtnClicked = false;
private void MyLLS_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// check if button is clicked, if so, return and reset the isBtnClicked flag.
if (isBtnClicked)
{
isBtnClicked = false;
return;
}
var item = (MyItemType)MyLLS.SelectedItem;
// Job 1 goes here
}
private void btDownload_Click(object sender, RoutedEventArgs e)
{
var button = (MyItemType)(sender as Button).DataContext;
// set it true when button clicked
isBtnClicked = true;
// Job 2 goes here
}

Related

Multiple ListBox on same Window doesn't select when focus moving ListBox

I have two ListBox in my window: LstStoreItems and LstPlayerItems. It looks like this:
The idea here is that when you select an item from the Store, the Sell button is disabled, and it UnselectAll on the Player Inventory, and vise Versa. Here's the code:
private void LstPlayerItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
LstStoreItems.UnselectAll();
BtnBuy.IsEnabled = false;
BtnSell.IsEnabled = true;
}
private void LstStoreItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
LstPlayerItems.UnselectAll();
BtnBuy.IsEnabled = true;
BtnSell.IsEnabled = false;
}
Now, though, if I select an item in the Player Inventory, and then go to select an item in the Store Inventory, it executes the code, but it doesn't actually select the item I'd clicked. How can I get it to select the item when I'm changing the focused ListBox?
I believe what is going on is the other ListBox's SelectionChanged event is being triggered when you do your UnSelectAll which is causing your new selected item to be unselected. Try checking to make sure that you have a selected item in ListBox before unselecting the items in the other ListBox.
Something like this:
private void LstPlayerItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (((ListBox)sender).SelectedIndex != -1)
LstStoreItems.UnselectAll();
BtnBuy.IsEnabled = false;
BtnSell.IsEnabled = true;
}
private void LstStoreItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (((ListBox)sender).SelectedIndex != -1)
LstPlayerItems.UnselectAll();
BtnBuy.IsEnabled = true;
BtnSell.IsEnabled = false;
}

Make event for any panel c#

So I have a matrix of panels (maybe will change for Picture Boxes in the future), and what i want is that every time i press one of the panels after pressing the button on the toolbox it will change it's background to a certain picture.
Right now what i have is:
private void EtapaInicial_Click(object sender, EventArgs e)
{
EtapaInicialWasClicked = true;
}
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
panel1.BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
What I would like to change was the panel1 to make it work for every panel (otherwise it will only change panel1 independently of the panel i've clicked), is that possible?
Try the following
private void EtapaInicial_Click(object sender, EventArgs e)
=> EtapaInicialWasClicked = true;
private void panel_Click(object sender, EventArgs e)
{
if (EtapaInicialWasClicked)
{
(sender as Panel).BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Yes it is. You have to loop through each panel
and assign the same event handler but you have to make some changes in the event handler itself
foreach(var p in allPanels)
{
p.Click += panel_Click;
}
Then change your event handler like this
private void panel_Click(object sender, EventArgs e)
{
var p = (Panel)sender;
if (EtapaInicialWasClicked)
{
p .BackgroundImage = Symbols.EtapaInicialbm;
EtapaInicialWasClicked = false;
}
}
Remember the sender argument contains reference to the actual control that initiated the event but you have to cast it first in order to use it.
However if you want to store more data for the event you've just handled you can use the panel.Tag property. This can be used to store EtapaInicialWasClicked for example

How can I switch between two methods in one button with every click?

I am new on C# so sorry if it look easy for some of you :)
I have this button click:
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_1); // Switch to IR1
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IR_Id.IR_2); // Switch to IR2
}
How can I switch between the two methods when clicking the button?
one click will be for method #1
second click will be for method #2
third click will be for method #1
etc..
You can do something like that:
bool executeMethodOne;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
executeMethodOne = !executeMethodOne;
var IrId = executeMethodOne ? IR_Id.IR_1 : IR_Id.IR_2;
_blManager.SendTrackerCmd(TrackerCmdType.PrimaryAVT_ActiveSensor, (float)IrId);
}
Have a bool called executeMethodOne which you will invert everytime you click on the button. Depending on if it is true or false you can execute the first or the second method
First click is old:
private void OnButtonClickOdd(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickOdd
button.Click -= OnButtonClickOdd;
// Subcribe to OnButtonClickEven
button.Click += OnButtonClickEven;
// Do your job here
}
private void OnButtonClickEven(object sender, RoutedEventArgs e)
{
// Unsubscribe OnButtonClickEven
button.Click -= OnButtonClickEven;
// Subcribe to OnButtonClickOdd
button.Click += OnButtonClickOdd;
// Do your job here
}
Other way: just use bool flag to know it odd or even click:
private bool odd = true;
private void buttonSwitchCamera_Click(object sender, RoutedEventArgs e)
{
if(old)
{
// Odd click job
}
else
{
// Even click job
}
old = !old;
}

Constant Check if ListView is Selected

I have done this before and I have a way of doing it, but I want to make sure it is the best way. I have a ListView in Details view. I also have a button. I only want that button to be enabled if there is an item selected (multiselect is disabled). Items will be added and removed to this listview but the button should be enabled anytime there is a selected item and disabled otherwise.
My event handler:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
button1.Enabled = true;
else
button1.Enabled = false;
}
That is what I have, just wondering if that will always work or are there freak incidents where it fails? Like if I delete or add things or anything else?
It'd be better if you show what you have - but in short, you start with the button disabled, and in the list view selectedindexChanged event enable the button if the list view has a selectedItems.Count of 1. Disable it if no item is selected. Here's a link which may help:
ListView selectedindexchanged
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 9; i++)
{
listView1.Items.Add("kashif");
}
button1.Enabled = false;
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
private void button2_Click(object sender, EventArgs e)
{
foreach (ListViewItem v in listView1.SelectedItems)
{
v.Remove();
}
}
}
Before Button2 Click
After Button2 click

CheckBox button and ContextMenuStrip for dropdown menu

I'm tring to implement a button which have a dropdown menu when checked and this menu is gone when unchecked. My problem is I cannot uncheck the checkbox when it or its menu lost focus.
The checkbox's appearance mode is button.
My code:
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
I've tried to uncheck the checkBox on contextMenuStrip's VisibleChanged / Closed event but this caused menu not to hide (or hide and show immediately).
The example below does not, of course, include the code you would need for swapping BackGroundImage of the CheckBox to indicate CheckState. The events to "wire-up" should be obvious. Hope this is helpful.
// tested in VS 2010 Pro, .NET 4.0 FrameWork Client Profile
// uses:
// CheckBox named 'checkBox1
// ContextMenuStrip named 'contextMenuStrip1
// TextBox named 'cMenuSelectionInfo for run-time checking of results
// used to position the ContextMenuStrip
private Point cPoint;
// context click ? dubious assumption that 'right' = context click
private bool cmOpenedRight;
// the clicked ToolStripMenuItem
private ToolStripMenuItem tsMIClicked;
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
cmOpenedRight = e.Button == MouseButtons.Right;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// positioning the CheckBox like this
// is something in a 'real-world' example
// you'd want to do in the Form.Load event !
// unless, of course, you'd made the CheckBox movable
if(checkBox1.Checked)
{
contextMenuStrip1.Show();
cPoint = PointToScreen(new Point(checkBox1.Left, checkBox1.Top + checkBox1.Height));
contextMenuStrip1.Location = cPoint;
}
else
{
contextMenuStrip1.Hide();
}
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// assume you do not have to check for null here ?
tsMIClicked = e.ClickedItem as ToolStripMenuItem;
tbCbMenuSelectionInfo.Text = tsMIClicked + " : " + ! (tsMIClicked.Checked);
}
private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = checkBox1.Checked;
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (cmOpenedRight)
{
tbCbMenuSelectionInfo.Text += " : closed because : " + e.CloseReason.ToString();
}
}
I think your approach of unchecking the check box on the context menu's closed event is a good one, what you need is a bit of "event cancelling logic"(c), like this:
private void OnContextClosing(object sender, EventArgs e)
{
_cancel = true;
cbSettings.Checked = false;
_cancel = false;
}
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if(_cancel)
return;
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
This will keep your CheckChanged event from re-checking your checkbox.

Categories