Use Event Handler's code inside my own code - c#

private void OnCarSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Car tmpCar = (Car)CarLST.SelectedItem;
VinNumberTB.Text = tmpCar.VinNumber;
CarMakeTB.Text = tmpCar.CarMake;
CarTypeCB.SelectedIndex = (int)tmpCar.Type;
PurchasePriceTB.Text = tmpCar.PurchasePrice.ToString();
ModelYearCB.SelectedItem = tmpCar.ModelYear;
MileageTB.Text = tmpCar.Mileage.ToString();
CarIMG.Source = new BitmapImage(new Uri(String.Format("ms-appx:///Assets/CarImages/{0}.png", tmpCar.Type.ToString())));
}
This corresponds to a list box's selectionChanged event handler. Is there a way I can just use this in my code as if it were a method. e.g.
SelectionChanged(foo, bar);
Basically, is there a way of using this block of code again without creating another method to hold this stuff.

Since you don't use any of the event arguments, you can extract the code into a separate method:
private void OnCarSelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateDisplay();
}
private void UpdateDisplay()
{
Car tmpCar = (Car)CarLST.SelectedItem;
VinNumberTB.Text = tmpCar.VinNumber;
CarMakeTB.Text = tmpCar.CarMake;
CarTypeCB.SelectedIndex = (int)tmpCar.Type;
PurchasePriceTB.Text = tmpCar.PurchasePrice.ToString();
ModelYearCB.SelectedItem = tmpCar.ModelYear;
MileageTB.Text = tmpCar.Mileage.ToString();
CarIMG.Source = new BitmapImage(new Uri(String.Format("ms-appx:///Assets/CarImages/{0}.png", tmpCar.Type.ToString())));
}
Then to call it separately all you need to do is:
this.UpdateDisplay();

Related

add keyup event for dynamically generated buttons in c#

I'm trying to define a keyUp event for a List of buttons that I generate dynamically but I can't find the appropriate method to do it.
This is the code that I use to generate the buttons.
public void generateurBtns()
{
test.cmd.CommandText = "select id from Citoyens";
test.cmd.Connection = test.cnx;
test.dr = test.cmd.ExecuteReader();
while (test.dr.Read())
{
Button Citoyen = new Button();
citoyensBtns.Add(Citoyen);
this.Controls.Add(Citoyen);
Citoyen.Name = test.dr[0].ToString();
Citoyen.Click += new EventHandler(button_keyup);
}
test.dr.Close();
}
I defined the button_keyup method as following.
void button_keyup(object sender, EventArgs e)
{
//code executed if button released
}
Any suggestions on how to make this work?
Hav you tried KeyUp event instead of Click ?
public void generateurBtns()
{
test.cmd.CommandText = "select id from Citoyens";
test.cmd.Connection = test.cnx;
test.dr = test.cmd.ExecuteReader();
while (test.dr.Read())
{
Button Citoyen = new Button();
citoyensBtns.Add(Citoyen);
this.Controls.Add(Citoyen);
Citoyen.Name = test.dr[0].ToString();
Citoyen.KeyUp += new KeyEventHandler(button_keyup);
}
test.dr.Close();
}
update : you will also need to implement the right delegate:
void button_keyup(object? sender, KeyEventArgs e)
{
//code executed if button released
}

c# get control from another void/form (eventHandler)

In form1 I created simple form called formhaslo.
I created in formhaslo control called listBoxhaslo
Now, I want to create MouseDoubleClick Event to listBoxhaslo.
I have problem with getting listBoxhaslo from formhaslo to form1.
Can you take a look at this code, please (check out comments):
public partial class Form1 : Form
{
Form formhaslo = new Form();
...
...
...
public void buttonLoadPassForBAKFile_Click(object sender, EventArgs e)
{
int i = 0;
string path = #"Backups";
formhaslo.StartPosition = FormStartPosition.CenterScreen;
ListBox listBoxhaslo = new ListBox();
listBoxhaslo.Location = new System.Drawing.Point(0, 30);
listBoxhaslo.Left = (formhaslo.ClientSize.Width - listBoxhaslo.Width) / 2;
using (FileStream fsSbHaslo = new FileStream(path + #"\BAKPass._pass", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader srhaslo = new StreamReader(fsSbHaslo, Encoding.Default))
{
string line;
while ((line = srhaslo.ReadLine()) != null)
{
listBoxhaslo.Items.Add(line);
i++;
}
srhaslo.Close();
}
formhaslo.Controls.Add(listBoxhaslo);
formhaslo.Controls.Add(label1);
listBoxhaslo.MouseDoubleClick += new MouseEventHandler(listBoxhaslo_MouseDoubleClick); // <---here is EventHandler
formhaslo.Show();
}
}
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
if ((listBoxhaslo.SelectedItem) != null) // <--listBoxhaslo does not exist in current context
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
I know that this error must be there because im doing it wrong but I don't know how to do it.
The listBoxhaslo doesn't exists because it was declare within the scope of first function which is not visible to the second function of your event listBoxhaslo_MouseDoubleClick. For you to make it work you need to declare listBoxhaslo variable outside of your function. You could declare it after formhaslo near top. Or another way to accomplish this is to cast sender to ListBox within your event.
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
var listBoxhaslo = (sender as ListBox);
if (listBoxhaslo.SelectedItem != null)
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
I haven't tried the code but I think it will do.

WPF-Frame navigate to same page only works once

In this page, you can add items. now you press "Save" to add another one. Heres the code:
private void Btn_Save_Click(object sender, RoutedEventArgs e)
{
// Adding the item to DB and List
MainData.MainDataItem MDI_Temp = new MainData.MainDataItem();
MDI_Temp.Int_AF = Convert.ToInt32(Tb_AF.Text);
MDI_Temp.Int_HO = Convert.ToInt32(Tb_HO.Text);
MDI_Temp.Int_ST = Convert.ToInt32(Tb_ST.Text);
MDI_Temp.Int_STD = Convert.ToInt32(Tb_STD.Text);
MDI_Temp.Int_DIA = Convert.ToInt32(Tb_DIA.Text);
MDI_Temp.Int_ECK = Convert.ToInt32(Tb_ECK.Text);
MDI_Temp.Int_MID = ((HelperClasses.Main_VM)this.DataContext).MDO_TmpStore.Int_ID;
MDI_Temp.Str_Bauteil = Str_Bauteil;
MDI_Temp.Str_Defekt = Str_Defekt;
MDI_Temp.Str_Massnahme = Str_Massnahme;
MDI_Temp.Str_Feld = Tb_Feld.Text;
MDI_Temp.Str_Zeile = Tb_Zeile.Text;
MDI_Temp.Int_Pos = Convert.ToInt32(Tb_Pos.Text);
HelperClasses.SQL_Class.DBAddItem(MDI_Temp);
// Navigate
HelperClass.Navigate("pages/New_Item.xaml");
}
And this is the void in the helperclass:
public static void Navigate(string Str_Uri)
{
((MainWindow)Application.Current.Windows[0]).Fm_MainContainer.Source = new Uri(Str_Uri, UriKind.Relative);
}
The first time you click on Btn_Save the page reloads, the second time it onyl add the item
Another option is to create an overload of navigate in helper class
public static void Navigate(object target)
{
((MainWindow)Application.Current.Windows[0]).Fm_MainContainer.Content = target;
}
ans use this way
// Navigate
New_Item item = new New_Item();
HelperClass.Navigate(item);
this will ensure to have a new initialization every time

Object share between more WPF windows

I'm fairly new to WPF and I need your help with one object passing between more WPF windows.
Firstly I have my MainWindow with Button_Click event like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
Attribute ChooseYourAttr = new Attribute();
Application.Current.MainWindow.Close();
ChooseYourAttr.Show();
Character Player = new Character(firstTextbox.Text);
}
And Then I have my second window called Attribute with something like this:
private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
String welcomeAttribute = "Ahh. I see! So ";
attributeTopLabel.Content = welcomeAttribute;
}
And I would like to have something like this: (Player.getName());
private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
String welcomeAttribute = "Ahh. I see! So " + Player.getName();
attributeTopLabel.Content = welcomeAttribute;
}
Thanks for your answers!
Just pass the value through in the constructor:
private Character player = new Character();
public Attribute(Character player)
{
this.player = player;
}
...
Character player = new Character(firstTextbox.Text);
Attribute ChooseYourAttr = new Attribute(player);
...
private void attributeTopLabel_Initialized(object sender, EventArgs e)
{
String welcomeAttribute = "Ahh. I see! So " + player.GetName();
attributeTopLabel.Content = welcomeAttribute;
}

Using a control created on Form_Load in another event

I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
ComboBox kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
kombo.Items.Add("Panel"); //just an example
}
Is there a way to make this work?
Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.
What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.
// kombo is now scoped for use throughout this class
ComboBox kombo = null;
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
// Assign to our kombo instance
kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
// Using the kombo we created in form load, which is still referenced
// in the class
kombo.Items.Add("Panel"); //just an example
}
You will have to use the FindControl() method to find the object first.
private void przycisk_Click(object sender, EventArgs e)
{
ComboBox kombo = (ComboBox)FindControl("kombo");
kombo.Items.Add("Panel");
}

Categories

Resources