ButtonClick to get an Object on SelectionChanged event - c#

I have a SelectionChanged event and works perfectly, but I want to figure out how to "catch" this selected item at the click of a button they need to pass it as parameter to another page and edit this Item. Here's the current code and button SelectionChanged I still implemented because this is what I need.
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
//pega o Carro que foi selecionado
Carro sCar = (Carro)listBox.SelectedItem;
btnEditCar.IsEnabled = true;
btnDeleteCar.IsEnabled = true;
}
else
{
btnEditCar.IsEnabled = false;
btnDeleteCar.IsEnabled = false;
}
}
I need to edit the selectedItem on this button:
private void btnEditCar_Click(object sender, EventArgs e)
{
//Here I need access to the selectedItem on SelectionChanged event.
}
If you could also tell me how to pass the object as parameter would be perfect.

You can do this with binding also
1.Bind ListBoxItem(Carro Object) to the tag of "btnEditCar" in xaml.
Xaml should be like this
<Button Name="btnEditCar" OnClick="btnEditCar_Click" Tag="{Binding}"/>
and now in
private void btnEditCar_Click(object sender, EventArgs e)
{
Carro sCar=(Carro)((sender as FrameworkElement).Tag)
}
This is the good practice,creating a class variable only for temporary purpose is hack

To give a better idea on my comments. Creating a class level variable is like this:
Notice that sCar is declared outside the method, but within the class.
Carro sCar = new Carro();
private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
sCar = (Carro)listBox.SelectedItem;
...
private void btnEditCar_Click(object sender, EventArgs e)
{
sCar.ProperyYouWantToChange = "Stuff I want to change"
}

Related

I got a multiple buttons event handler here But I need to target only one button if it was clicked how do I do that?

private void btnCodeAkas_Click(object sender, EventArgs e)
{
tmrTimerAkas.Start();
tmrTimerTwoAkas.Start();
}
But I need to target only one button if it was clicked how do I do that?
well you can cast your sender to Control object so you can get the name of the button then you write your conditional logic.
private void button1_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control.Name == "button1")
{
Console.WriteLine($"Clicked {control.Name} button");
}
else if (((Control)sender).Name == "button2")
{
Console.WriteLine($"Clicked {control.Name} button");
}
}

WPF ComboBox GotFocus

I have a DataGrid with a ComboBox column and I need to add items to that combobox only when it get focus. I handle the event GotFocus, but still have a problem. In fact once I select an item from the dropdown the event will fire again. Any workaround on how to fix this issue.
private void CmbxGotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !this.returnedFocus)
{
//dowork
}
}
private void CmbxLostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
ComboBox cb = sender as ComboBox;
if (cb != null)
{
this.returnedFocus = cb.IsDropDownOpen;
}
}
}

Indirect reference to a button

I'm having this code:
private void b9_Click(object sender, EventArgs e)
{
b9.Enabled = false;
color = 8;
}
The problem is that i'm having a lot of buttons for disabling. Is there a chance i can use something like:
this.Enabled=false;
Probably that is what you want
private void OnClick(object sender, EventArgs e)
{
if( sender is Button )
{
Button button = (Button)sender;
button.Enabled = false;
}
}
Use this routine for every button you need to disable on click.
It is known as single event handler for multiple controls. Just put following event handler for your buttons as many as you like.
public void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.IsEnable = false;
// If you want to access text in the button
... = button.Content as object;
}
private void OnClick(object sender, EventArgs e)
{
Button btn = sender as Button; // if sender is not a Button, btn will be null
if (btn != null)
{
btn.Enabled = false;
}
}
If you want to apply the same behaviour to any clickable control, you can use Control class instead of Button. Button inherits from Control and the property Enabled is defined in Control class.
private void OnClick(object sender, EventArgs e)
{
Control ctrl = sender as Control; // if sender is not a Control, ctrl will be null
if (ctrl != null)
{
ctrl .Enabled = false;
}
}
Also, if you want to go one step further, you can create a method that disables the clicked control. Something like this:
private void DisableControl(object sender)
{
Control ctrl = sender as Control;
if (ctrl != null)
{
ctrl.Enabled = false;
}
}
Then, you can call this method from the Click even handler like this:
private void OnClick(object sender, EventArgs e)
{
DisableControl(sender);
}

C# combobox if statement + continue button incorporation

This code doesn't work. I don't know what to fix.
public sealed partial class Home : Page
{
public Home()
{
this.InitializeComponent();
ComboBox1.Items.Add("Hindiiiii");
}
string selection = null;
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=1)
{
selection = ComboBox1.SelectedItem.ToString();
}
}
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
{
if (selection == "Hindiiiii")
this.Frame.Navigate(typeof(MainPage));
else if (selection == "English")
this.Frame.Navigate(typeof(Home));
}
}
When a user selects Hindiiiii on the main screen and clicks continues he is not redirected to the next page (MainPage).
Let's say your main page looks like this:
You can store the selection in a variable:
string selection = null;
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=-1)
{
selection = ComboBox1.SelectedItem.ToString();
}
}
Then in your click event you can pass parameters between your pages:
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
this.Frame.Navigate(typeof(SomePage), selection); //send the contents of the variable to another page
}
And let's say you had another page with a TextBox and a TextBlock:
In your other pages' OnNavigatedTo event, you can retrieve the parameters so you don't have to create a page for every selected language:
string selection = null;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
selection = e.Parameter.ToString();
languageTextBlock.Text = selection; //the textblox is now the selected language
//decide what the contents are based on the selection
if (selection == "English")
translation.Text = "Something in English";
else if (selection == "Hindi")
translation.Text = "Something in Hindi";
else if (selection == "German")
translation.Text = "Something in German";
//etc
}
When you go to the next page, this allows you to create your page based on the selected item. This image demonstrates this:
Alternatively, you can solve your problem by creating a page for every possible language:
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
{
if(selection == "English")
this.Frame.Navigate(typeof(EnglishPage));
else if(selection == "Hindi")
this.Frame.Navigate(typeof(HindiPage));
//and so on
}
}
I prefer to do it this way because it's a lot simpler.
Edit: I see Items in the property box but I'm not aware of how to use it to add combo box items. This is the way that I usually see it done:
Of course you'll need to replace MainPage with your page (if it's not already named MainPage).
Another edit:
If you added the items via the properties panel, you have to access the Content. Use this instead, if you want:
string selection = null;
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ComboBox1.SelectedIndex != -1)
{
//selection = ComboBox1.SelectedItem.ToString();
selection = (ComboBox1.SelectedItem as ComboBoxItem).Content.ToString();
}
}

Clear listbox when new tab selected

I am trying to clear the contents of a listbox when a new tab is seleced. Here is what I got, but nothing happens.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["entryTab"])
{
readBox.Items.Clear();
reminderBox.Items.Clear();
}
}
Try something like this in your form load
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_SelectedIndexChanged);
// Try this set null to DataSource
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages["entryTab"])
{
readBox.DataSource = null;
reminderBox.DataSource = null;
}
}

Categories

Resources