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
$details
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)
x:Name
but even better would be if u use bindings
tried that and Name none of them worked
what is your code behind
or viewmodel if u have one
and which one it doesnt find
its this line: tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
AantalTextBox ?
yes
I gave it x:Name="AantalTextBox"
but the error still presists
it doesnt exist in your xaml
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
what is Bestelling sorry I dont se it on xaml
that looks fine
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;
}
}
}
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
nope
ok so xaml.cs
yes
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
I tried Rebuilding
also your xaml does not have a lot of the things u are showing me
SluitButtonClick is not in any button
Yeah it was SluitButtonClick I changed to OpslaanEnSluitButtonClick
ah ok
that one I see
I mean then your textbox should work
after building the error is still there
but in the output window
what does it say
ill show you
in the output window
if it says that
ok
check what file it complains about
it should say on the right side
Tuincentrum_UI_x02civxy_wpftmp.csproj
im not sure what file this is
I found these
it would be like that
u see mine says MainWindow.xaml.cs
yes
the code behind I sent is for that fike
file*
ok so can u send me the code again updated with exactly what u have now
xaml and xaml.cs
$paste
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!
if possible use this site its better then discord to read
I will
BlazeBin - gdrlxvzpcihi
A tool for sharing your source code with the world!
ok moment
thank you
its missing the xaml.cs file
ah I see because the textbox is inside a datatemplate
that wont work
I had a feeling
can you help me get to the TextBox?
what u should do is using an ObservableCollection and binding
yeah I got the collection
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!
and then
in your xaml.cs
and then your listbox
okay
what is ProductNaarString
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)}"; } } }
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)}"; } } }
<ListBox ... ItemsSource="{Binding Products}" ...>
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!
I will now but what is ProductNaarString
its a property I made to hold the ToString method
I had a previous problem with the binding I set on the textBlock
ah ok
Ah wow I see there is a lot of issues because you're mixing binding with code behind and no datacontext
oh, could you help make the code a little better?
ok let me think for a moment what is easiest to explain
thank you very much
u dont edit items in the listbox do u?
you can edit the TextBox
ok
what would be the aantal_product in Product?
you mean in Bestelling?
you're not using bestelling as far as I can see
in Product
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
??translate en bestelling
ah mm bot doesnt work in threads what bestelling means
'Order_product' this is the object in between Order and Product
ok
so list A is all products
list B is the shopping cart?
yes
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:
Then in your xaml.cs u want to do this, change these 2 lines from
to
Then change this from:
to
By doing this we are not actually making use of the DataContext and bindings in xaml
one moment please
I'm getting this error in my .xaml file
try reloading the designer never seen that error before
yup
still there
ill reload vscode
ah vscode
:OOF:
I would not be able to help u with vscode I dont use it for c#
reloading the app worked
no i meant Visual studio
im using vs
sorry forget sometimes
visual studio community
ok
yes
yeah I have 0 idea about vscode issues 😛
i have enterprise too, if that one is better
either should work
anyway does it build now?
vscode isnt good for compiling
ah right I forgot change this too
now I'm having issues with my other functions
The listbox doesnt exist anymore so I cant put any products in
u would change AantalTextBox.Text to either Naam_nl or u would need ot make
A get;set; to use that
I need the textbox so the user can put the amount of the product
oh so its mean to be a quantity ok now it makes mose sense
sorry I forgot to explain
thank you for your patience
then its best if u have a property in Product that says quantity int
then u can bind it
Bestelling has that property, should I use Bestelling instead of Product?
yes it would be better
so u just set it to 1
by default
and user can modify it on the 2nd listbox
okay
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
to
and it should reflect the changes in your ObservableCollection so it will update Quantity
got it
I also added this back : AlleProductenListBox.ItemsSource = AlleProducten;
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
The Binding takes care of it?
yes
cool
let me know if it works of if u have issues still
yeah I have a question
so to access the quantity what's the syntax?
so u would have say
And if u loop
GeselecteerdeProducten is the 2nd listbox right?
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
U can remove if u want, u would just have to keep track of the items to add back
this is the method that adds the products from the first list to the other
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
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?
the textbox is fine
then I can just count the amount of times the product appears in the list
u can even make it a numeric up down box
visually having the quantity on the cart is better
I understand
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
this makes things way to complicated
just add+ 1 instead of adding a new item
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?
that is one way, u dont add to the box u add to the item
item.Quantity += 1;
right
one more thing
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
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?
yeah
something like this to add product back to product
u check product id from bestelling against the product id on product
right, how do I show the Product in the ListBox (the second listBox)?
when u add item to GeselecteerdeProducten
it auto updates the list
ill run it
is it not showing?
no
u change this right
yeah
cause the type right now is Bestelling
can u show me your current code on xaml and xmal.cs
yes
this one is throwing errors
yes I need to see the code to see the changes u did
https://paste.mod.gg/sycdjoacwtbv/0 the code behind
BlazeBin - sycdjoacwtbv
A tool for sharing your source code with the world!
u did not change things like how I told u :/
oh didnt see that
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
yup
I have a question what's p?
Product
this line is throwing an error: foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p);
p does not exist in the current context
ah my bad
that one I forgot to change to c
that line
cart.Add(c);
so all the products are already in the second listbox
it should be that a user can add one and then remove one
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
this is the method that adds items
no that is to remove
oh
there is no code to add items to GeselecteerdeProducten so how is it filled
the listbox is full without me adding any products
i have no clue
what is your xaml u didnt send me
BlazeBin - hqquteseflpq
A tool for sharing your source code with the world!
well u have no code filling the 2nd list
hey its good now
💀
I just ran the app again
so to recap to add a product I should?
do the inverse of how u remove items
add item to removedProducts
add bestelling to cart list
and remove item from products list
i'm so confused
what's the purpose of the removedProducts List?
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
yes
u keep a list that intermediate both
which is the removedProducts
okay, but that list is Product too?
yes
im still a little confused
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
right, I'm confused about how i'll show the products in the secod listbox
by adding the bestelling to GeselecteerdeProducten?
similar to how u do in your button
OpslaanEnSluitenButtonClick
yeah that makes sene
snese
sense
but, I need to show product names
and it will
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
even tho ProductNaarString is from the Product class?
public Product ProductInfo {get;set;}
so u can use ProductInfo.ProductNaarString
whichever u think its best
it can be as simple as
is there really no other way than everything we're doing?
and u change ProductNaarString to DisplayName
u could add quantity to product and keep everything as it was before if u want
then there is no use in having the Bestelling class
but doesn't sound like a Product responsibility to hold the quantity the user is buying
yeah
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
should I make a nother class?
i.e.:
I can check the product by its id using a method that asks the db
is that better in your opinion?
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
cant it help with showing the name?
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
nope
ADO
Ado? ok
then yes that is fine
alright
in this case here all u would do is
I got a question, what's the difference between Product.ProductNaarString and ProductNaarString?
because the 2nd list is Cart
so we are accessing the Product reference in the Cart item
so to add I should?
👆
alright, I'll figure the rest out on my own
thank you for all the help
and
this is just a basic example
ofc your data comes from the database etc
anyway I got to go for now