C
C#6mo ago
TheUnquiet

Name does not exist in the current context

I need to access the Text inside a textBox but its name does not exist. the code is found in the text files
192 Replies
leowest
leowest6mo ago
$details
MODiX
MODiX6mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
TheUnquiet
TheUnquietOP6mo ago
leowest
leowest6mo ago
x:Name but even better would be if u use bindings
TheUnquiet
TheUnquietOP6mo ago
tried that and Name none of them worked
leowest
leowest6mo ago
what is your code behind or viewmodel if u have one
TheUnquiet
TheUnquietOP6mo ago
private void SluitButtonClick(object sender, RoutedEventArgs e)
{
bool aanleg, afhaal;
if ((bool)AanlegRadioButton.IsChecked) aanleg = true; else aanleg = false;
if ((bool)AfhaalRadioButton.IsChecked) afhaal = true; else afhaal = false;

Offerte offerte = new(DateTime.Now, int.Parse(KlantIdTextBlock.Text), afhaal, aanleg, GeselecteerdeProducten.Count);
tuincentrumManager.UploadOfferte(offerte);

foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}

Close();
}
private void SluitButtonClick(object sender, RoutedEventArgs e)
{
bool aanleg, afhaal;
if ((bool)AanlegRadioButton.IsChecked) aanleg = true; else aanleg = false;
if ((bool)AfhaalRadioButton.IsChecked) afhaal = true; else afhaal = false;

Offerte offerte = new(DateTime.Now, int.Parse(KlantIdTextBlock.Text), afhaal, aanleg, GeselecteerdeProducten.Count);
tuincentrumManager.UploadOfferte(offerte);

foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}

Close();
}
leowest
leowest6mo ago
and which one it doesnt find
TheUnquiet
TheUnquietOP6mo ago
its this line: tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
leowest
leowest6mo ago
AantalTextBox ?
TheUnquiet
TheUnquietOP6mo ago
yes I gave it x:Name="AantalTextBox" but the error still presists
leowest
leowest6mo ago
it doesnt exist in your xaml
TheUnquiet
TheUnquietOP6mo ago
is there a way to Bind it to Bestelling? I added it <TextBox Width="50" Margin="5" x:Name="AantalTextBox"/> this is the line I added
leowest
leowest6mo ago
what is Bestelling sorry I dont se it on xaml that looks fine
TheUnquiet
TheUnquietOP6mo ago
this is the model using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BL.Models { public class Bestelling { public int Offerte_Id; public int Product_Id; public int Aantal_Product; public Bestelling(int offerte_Id, int product_Id, int aantal_Product) { Offerte_Id = offerte_Id; Product_Id = product_Id; Aantal_Product = aantal_Product; } } }
leowest
leowest6mo ago
well u cannot bind fields to XAML u need properties are u using mvvm or only code behind? just trying to understand code behind is when u use xaml.cs for everything
TheUnquiet
TheUnquietOP6mo ago
nope
leowest
leowest6mo ago
ok so xaml.cs
TheUnquiet
TheUnquietOP6mo ago
yes
leowest
leowest6mo ago
this should work if u build does it work? as it does it says succeed in the build if it does vs might be drunk
TheUnquiet
TheUnquietOP6mo ago
I tried Rebuilding
leowest
leowest6mo ago
also your xaml does not have a lot of the things u are showing me SluitButtonClick is not in any button
TheUnquiet
TheUnquietOP6mo ago
Yeah it was SluitButtonClick I changed to OpslaanEnSluitButtonClick
leowest
leowest6mo ago
ah ok that one I see I mean then your textbox should work
TheUnquiet
TheUnquietOP6mo ago
after building the error is still there
leowest
leowest6mo ago
but in the output window what does it say
TheUnquiet
TheUnquietOP6mo ago
ill show you
TheUnquiet
TheUnquietOP6mo ago
No description
leowest
leowest6mo ago
in the output window if it says that
leowest
leowest6mo ago
No description
TheUnquiet
TheUnquietOP6mo ago
No description
leowest
leowest6mo ago
ok check what file it complains about it should say on the right side
TheUnquiet
TheUnquietOP6mo ago
Tuincentrum_UI_x02civxy_wpftmp.csproj im not sure what file this is
TheUnquiet
TheUnquietOP6mo ago
I found these
No description
leowest
leowest6mo ago
No description
leowest
leowest6mo ago
it would be like that u see mine says MainWindow.xaml.cs
TheUnquiet
TheUnquietOP6mo ago
yes the code behind I sent is for that fike file*
leowest
leowest6mo ago
ok so can u send me the code again updated with exactly what u have now xaml and xaml.cs $paste
MODiX
MODiX6mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
leowest
leowest6mo ago
if possible use this site its better then discord to read
TheUnquiet
TheUnquietOP6mo ago
I will
TheUnquiet
TheUnquietOP6mo ago
BlazeBin - gdrlxvzpcihi
A tool for sharing your source code with the world!
leowest
leowest6mo ago
ok moment
TheUnquiet
TheUnquietOP6mo ago
thank you
leowest
leowest6mo ago
its missing the xaml.cs file ah I see because the textbox is inside a datatemplate that wont work
TheUnquiet
TheUnquietOP6mo ago
I had a feeling can you help me get to the TextBox?
leowest
leowest6mo ago
what u should do is using an ObservableCollection and binding
TheUnquiet
TheUnquietOP6mo ago
yeah I got the collection
TheUnquiet
TheUnquietOP6mo ago
here is the cs file i'm very sorry for not sending it before https://paste.mod.gg/hfxmutjbldzo/0
BlazeBin - hfxmutjbldzo
A tool for sharing your source code with the world!
leowest
leowest6mo ago
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
}
}
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
}
}
and then in your xaml.cs
public ObservableCollection<Bestelling> Products {get;set;}
public ObservableCollection<Bestelling> Products {get;set;}
and then your listbox
TheUnquiet
TheUnquietOP6mo ago
okay
leowest
leowest6mo ago
ItemsSource="{Binding Products}"
ItemsSource="{Binding Products}"
what is ProductNaarString
TheUnquiet
TheUnquietOP6mo ago
I don't understand using BL.Exceptions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BL.Models { public class Product { public int? Id; public string ProductNaarString => ToString(); private string naam_nl; public string Naam_nl { get { return naam_nl; } set { if (string.IsNullOrWhiteSpace(value)) throw new TuincentrumException("Naam nl is leeg"); naam_nl = value; } } private string naam_w; public string Naam_W {
get { return naam_w; } set { if (string.IsNullOrWhiteSpace(value)) throw new TuincentrumException("Naam w is leeg"); naam_nl = value; } } public double Prijs; public string Beschrijving; public Product(int id, string naam_nl, string naam_W, double prijs, string beschrijving) { Id = id; Naam_nl = naam_nl; Naam_W = naam_W; Prijs = prijs; Beschrijving = beschrijving; } public Product(string naam_nl, string naam_W, double prijs, string beschrijving) { Naam_nl = naam_nl; Naam_W = naam_W; Prijs = prijs; Beschrijving = beschrijving; } public override string ToString() { return $"{Naam_nl} {Naam_W} {Beschrijving} €{Math.Round(Prijs, 2)}"; } } }
leowest
leowest6mo ago
<ListBox ... ItemsSource="{Binding Products}" ...>
TheUnquiet
TheUnquietOP6mo ago
could you take a look at the xaml.cs file? I got a link https://paste.mod.gg/hfxmutjbldzo/0
BlazeBin - hfxmutjbldzo
A tool for sharing your source code with the world!
leowest
leowest6mo ago
I will now but what is ProductNaarString
TheUnquiet
TheUnquietOP6mo ago
its a property I made to hold the ToString method I had a previous problem with the binding I set on the textBlock
leowest
leowest6mo ago
ah ok Ah wow I see there is a lot of issues because you're mixing binding with code behind and no datacontext
TheUnquiet
TheUnquietOP6mo ago
oh, could you help make the code a little better?
leowest
leowest6mo ago
ok let me think for a moment what is easiest to explain
TheUnquiet
TheUnquietOP6mo ago
thank you very much
leowest
leowest6mo ago
u dont edit items in the listbox do u?
TheUnquiet
TheUnquietOP6mo ago
you can edit the TextBox
leowest
leowest6mo ago
ok what would be the aantal_product in Product?
TheUnquiet
TheUnquietOP6mo ago
you mean in Bestelling?
leowest
leowest6mo ago
you're not using bestelling as far as I can see in Product
TheUnquiet
TheUnquietOP6mo ago
so I've got products in a collection then im making a Bestelling object for each of those Product objects Bestelling has a property Aantal_product which should have the value of whatever the user has entered in the textbox
leowest
leowest6mo ago
??translate en bestelling ah mm bot doesnt work in threads what bestelling means
TheUnquiet
TheUnquietOP6mo ago
'Order_product' this is the object in between Order and Product
leowest
leowest6mo ago
ok so list A is all products list B is the shopping cart?
TheUnquiet
TheUnquietOP6mo ago
yes
leowest
leowest6mo ago
ok I will worry about that latter first I will show u how to do this then we work our way for bestelling which might need to change things ok, so what u want to do is: To your xaml change these 2 like these:
<ListBox ItemsSource="{Binding AlleProducten}" Grid.Row="2" Grid.Column="0" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended"/>
<ListBox ItemsSource="{Binding GeselecteerdeProducten}" Grid.Row="2" Grid.Column="2" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding AlleProducten}" Grid.Row="2" Grid.Column="0" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended"/>
<ListBox ItemsSource="{Binding GeselecteerdeProducten}" Grid.Row="2" Grid.Column="2" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Then in your xaml.cs u want to do this, change these 2 lines from
private ObservableCollection<Product> AlleProducten;
private ObservableCollection<Product> GeselecteerdeProducten;
private ObservableCollection<Product> AlleProducten;
private ObservableCollection<Product> GeselecteerdeProducten;
to
public ObservableCollection<Product> AlleProducten {get;set;}
public ObservableCollection<Product> GeselecteerdeProducten {get;set;}
public ObservableCollection<Product> AlleProducten {get;set;}
public ObservableCollection<Product> GeselecteerdeProducten {get;set;}
Then change this from:
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());
AlleProductenListBox.ItemsSource = AlleProducten;

GeselecteerdeProducten = new ObservableCollection<Product>();
GeselecteerdeProductenListBox.ItemsSource = GeselecteerdeProducten;
Klant = k;
}
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());
AlleProductenListBox.ItemsSource = AlleProducten;

GeselecteerdeProducten = new ObservableCollection<Product>();
GeselecteerdeProductenListBox.ItemsSource = GeselecteerdeProducten;
Klant = k;
}
to
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
DataContext = this;

tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());

GeselecteerdeProducten = new ObservableCollection<Product>();
Klant = k;
}
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
DataContext = this;

tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());

GeselecteerdeProducten = new ObservableCollection<Product>();
Klant = k;
}
By doing this we are not actually making use of the DataContext and bindings in xaml
TheUnquiet
TheUnquietOP6mo ago
one moment please
TheUnquiet
TheUnquietOP6mo ago
I'm getting this error in my .xaml file
No description
leowest
leowest6mo ago
try reloading the designer never seen that error before
TheUnquiet
TheUnquietOP6mo ago
yup still there ill reload vscode
leowest
leowest6mo ago
ah vscode :OOF: I would not be able to help u with vscode I dont use it for c#
TheUnquiet
TheUnquietOP6mo ago
reloading the app worked no i meant Visual studio im using vs sorry forget sometimes
leowest
leowest6mo ago
visual studio community ok
TheUnquiet
TheUnquietOP6mo ago
yes
leowest
leowest6mo ago
yeah I have 0 idea about vscode issues 😛
TheUnquiet
TheUnquietOP6mo ago
i have enterprise too, if that one is better
leowest
leowest6mo ago
either should work anyway does it build now?
TheUnquiet
TheUnquietOP6mo ago
vscode isnt good for compiling
leowest
leowest6mo ago
ah right I forgot change this too
TheUnquiet
TheUnquietOP6mo ago
now I'm having issues with my other functions
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in GeselecteerdeProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Remove(p);
AlleProducten.Add(p);
}
}
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in GeselecteerdeProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Remove(p);
AlleProducten.Add(p);
}
}
The listbox doesnt exist anymore so I cant put any products in
leowest
leowest6mo ago
foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}
foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}
u would change AantalTextBox.Text to either Naam_nl or u would need ot make
public string ProductNaarString => ToString();
public string ProductNaarString => ToString();
A get;set; to use that
TheUnquiet
TheUnquietOP6mo ago
I need the textbox so the user can put the amount of the product
leowest
leowest6mo ago
oh so its mean to be a quantity ok now it makes mose sense
TheUnquiet
TheUnquietOP6mo ago
sorry I forgot to explain thank you for your patience
leowest
leowest6mo ago
then its best if u have a property in Product that says quantity int then u can bind it
TheUnquiet
TheUnquietOP6mo ago
Bestelling has that property, should I use Bestelling instead of Product?
leowest
leowest6mo ago
yes it would be better so u just set it to 1 by default and user can modify it on the 2nd listbox
TheUnquiet
TheUnquietOP6mo ago
okay
leowest
leowest6mo ago
and add the x:Name="AlleProductenListBox" back to your lists so u can use them from the button that should be all I guess u can change the
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Width="50" Margin="5" x:Name="AantalTextBox"/>
</StackPanel>
</StackPanel>
</DataTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Width="50" Margin="5" x:Name="AantalTextBox"/>
</StackPanel>
</StackPanel>
</DataTemplate>
to
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5" />
</StackPanel>
</StackPanel>
</DataTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5" />
</StackPanel>
</StackPanel>
</DataTemplate>
and it should reflect the changes in your ObservableCollection so it will update Quantity
TheUnquiet
TheUnquietOP6mo ago
got it I also added this back : AlleProductenListBox.ItemsSource = AlleProducten;
leowest
leowest6mo ago
and then u would need to add the name to your bestelling no need if u have it the way I gave u earlier setting it from xaml is better for visibility
TheUnquiet
TheUnquietOP6mo ago
The Binding takes care of it?
leowest
leowest6mo ago
yes
TheUnquiet
TheUnquietOP6mo ago
cool
leowest
leowest6mo ago
let me know if it works of if u have issues still
TheUnquiet
TheUnquietOP6mo ago
yeah I have a question so to access the quantity what's the syntax?
leowest
leowest6mo ago
so u would have say
public ObservableCollection<Bestelling> GeselecteerdeProducten {get;set;}
public ObservableCollection<Bestelling> GeselecteerdeProducten {get;set;}
And if u loop
foreach (var item in GeselecteerdeProducten)
{
//item.Quantity
}
foreach (var item in GeselecteerdeProducten)
{
//item.Quantity
}
GeselecteerdeProducten is the 2nd listbox right?
TheUnquiet
TheUnquietOP6mo ago
yes to transfer the products from the first listbox to the second I cant just remove and add them right? Since the types are Product and Bestelling instead of Product and Product
leowest
leowest6mo ago
U can remove if u want, u would just have to keep track of the items to add back
TheUnquiet
TheUnquietOP6mo ago
this is the method that adds the products from the first list to the other
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in AlleProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Add(p);
AlleProducten.Remove(p);
}
}
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in AlleProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Add(p);
AlleProducten.Remove(p);
}
}
leowest
leowest6mo ago
You could keep a list of like: private List<Product> removedProducts = new List<Product>(); if u want to remove and add items back and just use the item Id to compare from bestelling to product
TheUnquiet
TheUnquietOP6mo ago
should I just rewrite the working of everything? or is it better to get rid of the textbox and have users just add as many using the button instead?
leowest
leowest6mo ago
the textbox is fine
TheUnquiet
TheUnquietOP6mo ago
then I can just count the amount of times the product appears in the list
leowest
leowest6mo ago
u can even make it a numeric up down box visually having the quantity on the cart is better
TheUnquiet
TheUnquietOP6mo ago
I understand
leowest
leowest6mo ago
I just dont see any need to remove the product from the list of products if the user add the same product just check the id if it exists in the list of cart
TheUnquiet
TheUnquietOP6mo ago
this makes things way to complicated
leowest
leowest6mo ago
just add+ 1 instead of adding a new item
TheUnquiet
TheUnquietOP6mo ago
I thought it would be best so that users cant add the same one again so I'll check if the selected item is already in the second list and if it is i'll add + to the textbox?
leowest
leowest6mo ago
that is one way, u dont add to the box u add to the item item.Quantity += 1;
TheUnquiet
TheUnquietOP6mo ago
right one more thing
leowest
leowest6mo ago
but you can remove items if u prefer u would just have an extra list keeping the items removed so u can add it back later it would not be complicated its just 1 extra check
TheUnquiet
TheUnquietOP6mo ago
whenever I transfer one product to the other listbox the product shows up in the listbox how can i achieve this now that I'm using Bestelling as a type instead of Product remove from AlleProducten?
leowest
leowest6mo ago
yeah
private List<Product> removedProducts = new List<Product>();
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
private List<Product> removedProducts = new List<Product>();
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
something like this to add product back to product u check product id from bestelling against the product id on product
TheUnquiet
TheUnquietOP6mo ago
right, how do I show the Product in the ListBox (the second listBox)?
leowest
leowest6mo ago
when u add item to GeselecteerdeProducten it auto updates the list
TheUnquiet
TheUnquietOP6mo ago
ill run it
leowest
leowest6mo ago
is it not showing?
TheUnquiet
TheUnquietOP6mo ago
no
leowest
leowest6mo ago
u change this right
TheUnquiet
TheUnquietOP6mo ago
yeah cause the type right now is Bestelling
leowest
leowest6mo ago
can u show me your current code on xaml and xmal.cs
TheUnquiet
TheUnquietOP6mo ago
yes this one is throwing errors
TheUnquiet
TheUnquietOP6mo ago
No description
leowest
leowest6mo ago
yes I need to see the code to see the changes u did
TheUnquiet
TheUnquietOP6mo ago
BlazeBin - sycdjoacwtbv
A tool for sharing your source code with the world!
leowest
leowest6mo ago
u did not change things like how I told u :/
leowest
leowest6mo ago
No description
TheUnquiet
TheUnquietOP6mo ago
oh didnt see that
leowest
leowest6mo ago
that is why it was not showing also line 50 no need FirstOrDefault(x => x.Id == c.Id); this was an example u have to change Id to the actual Id names in bestelling and product
TheUnquiet
TheUnquietOP6mo ago
yup I have a question what's p?
leowest
leowest6mo ago
Product
TheUnquiet
TheUnquietOP6mo ago
this line is throwing an error: foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p); p does not exist in the current context
leowest
leowest6mo ago
ah my bad that one I forgot to change to c that line cart.Add(c);
TheUnquiet
TheUnquietOP6mo ago
No description
TheUnquiet
TheUnquietOP6mo ago
so all the products are already in the second listbox it should be that a user can add one and then remove one
leowest
leowest6mo ago
it should not because you're initializing it to empty how are u adding items to it, its not in the code u sent me
TheUnquiet
TheUnquietOP6mo ago
this is the method that adds items
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (Bestelling c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product_Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (Bestelling c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product_Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
leowest
leowest6mo ago
no that is to remove
TheUnquiet
TheUnquietOP6mo ago
oh
leowest
leowest6mo ago
there is no code to add items to GeselecteerdeProducten so how is it filled
TheUnquiet
TheUnquietOP6mo ago
the listbox is full without me adding any products i have no clue
leowest
leowest6mo ago
what is your xaml u didnt send me
TheUnquiet
TheUnquietOP6mo ago
BlazeBin - hqquteseflpq
A tool for sharing your source code with the world!
leowest
leowest6mo ago
well u have no code filling the 2nd list
TheUnquiet
TheUnquietOP6mo ago
hey its good now 💀 I just ran the app again so to recap to add a product I should?
leowest
leowest6mo ago
do the inverse of how u remove items add item to removedProducts add bestelling to cart list and remove item from products list
TheUnquiet
TheUnquietOP6mo ago
i'm so confused what's the purpose of the removedProducts List?
leowest
leowest6mo ago
it keeps the items u have removed from products so u can add it back when u remove them from cart because cart is a different type Bestelling != Product
TheUnquiet
TheUnquietOP6mo ago
yes
leowest
leowest6mo ago
u keep a list that intermediate both which is the removedProducts
TheUnquiet
TheUnquietOP6mo ago
okay, but that list is Product too?
leowest
leowest6mo ago
yes
leowest
leowest6mo ago
No description
TheUnquiet
TheUnquietOP6mo ago
im still a little confused
leowest
leowest6mo ago
about what its the same thing u were doing before except u have a separated list to store the removed products that is not visible to the xaml u add item to bestelling u add item to removedProducts u remove item from Products
TheUnquiet
TheUnquietOP6mo ago
right, I'm confused about how i'll show the products in the secod listbox
leowest
leowest6mo ago
by adding the bestelling to GeselecteerdeProducten? similar to how u do in your button OpslaanEnSluitenButtonClick
TheUnquiet
TheUnquietOP6mo ago
yeah that makes sene snese sense but, I need to show product names
leowest
leowest6mo ago
and it will
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Aantal_Product}" Width="50" Margin="5"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Aantal_Product}" Width="50" Margin="5"/>
</StackPanel>
if Aantal_Product is quantity and ProductNaarString is the product name in Bestelling it will work just fine if its not u need to add a name property to bestelling or a refence to Product so u can do say
TheUnquiet
TheUnquietOP6mo ago
even tho ProductNaarString is from the Product class?
leowest
leowest6mo ago
public Product ProductInfo {get;set;} so u can use ProductInfo.ProductNaarString whichever u think its best it can be as simple as
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}
public string DisplayName {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product, string displayName)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
DisplayName = displayName
}
}
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}
public string DisplayName {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product, string displayName)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
DisplayName = displayName
}
}
TheUnquiet
TheUnquietOP6mo ago
is there really no other way than everything we're doing?
leowest
leowest6mo ago
and u change ProductNaarString to DisplayName u could add quantity to product and keep everything as it was before if u want
TheUnquiet
TheUnquietOP6mo ago
then there is no use in having the Bestelling class
leowest
leowest6mo ago
but doesn't sound like a Product responsibility to hold the quantity the user is buying
TheUnquiet
TheUnquietOP6mo ago
yeah
leowest
leowest6mo ago
the problem here is how you are handling things normally you have Product and u have Cart Cart will usually have some references of Product
TheUnquiet
TheUnquietOP6mo ago
should I make a nother class?
leowest
leowest6mo ago
i.e.:
public class Cart
{
public int Id {get;set;}
public int ProductId {get;set;}
public Product Product {get;set;}
public int Quantity {get;set;}
}
public class Cart
{
public int Id {get;set;}
public int ProductId {get;set;}
public Product Product {get;set;}
public int Quantity {get;set;}
}
TheUnquiet
TheUnquietOP6mo ago
I can check the product by its id using a method that asks the db is that better in your opinion?
leowest
leowest6mo ago
I dont think that is relevant here since u already have a list with all products but having a reference of Product in your cart
TheUnquiet
TheUnquietOP6mo ago
cant it help with showing the name?
leowest
leowest6mo ago
allows the flexibity of u being able to use its name etc so u could do say Product.Name in the binding as an example now it depends if you're using those models for something else i.e.: are u using EFCore? if u are then it becomes a bit more complex
TheUnquiet
TheUnquietOP6mo ago
nope ADO
leowest
leowest6mo ago
Ado? ok then yes that is fine
TheUnquiet
TheUnquietOP6mo ago
alright
leowest
leowest6mo ago
in this case here all u would do is
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding Product.ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding Product.ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5"/>
</StackPanel>
TheUnquiet
TheUnquietOP6mo ago
I got a question, what's the difference between Product.ProductNaarString and ProductNaarString?
leowest
leowest6mo ago
because the 2nd list is Cart so we are accessing the Product reference in the Cart item
TheUnquiet
TheUnquietOP6mo ago
so to add I should?
leowest
leowest6mo ago
👆
TheUnquiet
TheUnquietOP6mo ago
alright, I'll figure the rest out on my own thank you for all the help
leowest
leowest6mo ago
leowest
leowest6mo ago
public record Product(int Id, string Name, decimal Price);
public record Cart(int Id, Product Product, int Quantity);
public record Product(int Id, string Name, decimal Price);
public record Cart(int Id, Product Product, int Quantity);
and
public ObservableCollection<Product> Products { get; set; }
public ObservableCollection<Cart> Cart { get; set; }
private List<Product> removedProducts = new List<Product>();

public Window1()
{
Products = new ObservableCollection<Product>
{
new Product(1, "Boots", 120),
new Product(1, "Pants", 90),
new Product(1, "Shirt", 81),
new Product(1, "Gloves", 22),
new Product(1, "Hat", 12),
};

Cart = new ObservableCollection<Cart>();
InitializeComponent();
DataContext = this;
}

private void AddAll(object sender, RoutedEventArgs e)
{
var products = new List<Product>();
foreach (Product p in ProductBox.SelectedItems) products.Add(p);
foreach (Product p in products)
{
Cart.Add(new Cart(1, p, 0));
removedProducts.Add(p);
Products.Remove(p);
}
}

private void RemoveAll(object sender, RoutedEventArgs e)
{
var cart = new List<Cart>();
foreach (Cart c in CartBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product.Id);
Cart.Remove(c);
Products.Add(p);
removedProducts.Remove(p);
}
}
}
public ObservableCollection<Product> Products { get; set; }
public ObservableCollection<Cart> Cart { get; set; }
private List<Product> removedProducts = new List<Product>();

public Window1()
{
Products = new ObservableCollection<Product>
{
new Product(1, "Boots", 120),
new Product(1, "Pants", 90),
new Product(1, "Shirt", 81),
new Product(1, "Gloves", 22),
new Product(1, "Hat", 12),
};

Cart = new ObservableCollection<Cart>();
InitializeComponent();
DataContext = this;
}

private void AddAll(object sender, RoutedEventArgs e)
{
var products = new List<Product>();
foreach (Product p in ProductBox.SelectedItems) products.Add(p);
foreach (Product p in products)
{
Cart.Add(new Cart(1, p, 0));
removedProducts.Add(p);
Products.Remove(p);
}
}

private void RemoveAll(object sender, RoutedEventArgs e)
{
var cart = new List<Cart>();
foreach (Cart c in CartBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product.Id);
Cart.Remove(c);
Products.Add(p);
removedProducts.Remove(p);
}
}
}
this is just a basic example ofc your data comes from the database etc anyway I got to go for now
Want results from more Discord servers?
Add your server