C
C#ā€¢2y ago
johndoe

ā” Simple project with C# windows forms app

Hello. I want to create an application which compares different offers and then gives the best offer. I am brand new to C#, that is why I need to know which elements I would use and how I would make the application responsive and dynamic and not hard coded. I want to use best practice C#. I attached a picture of what I want it to look like. The column Criteria should initally be there. In the form on the left you can create a new offer which is then inserted in the table as a new column with the name as the column heading and the values of the inputs in the matching fields. The user should be able to add as many offers as they want to the table, not hardcoded amount. And when there is not enough space you should be able to scroll to the right to see the rest of the offers. It would be nice if all the cells are getting smaller once you make the window smaller and same with the add offer form (responsive). Can someone please guide me
44 Replies
Pobiega
Pobiegaā€¢2y ago
You'd normally layout something like this by adding more rows, not adding columns so "Price", "discount" etc would all be columns First of all, "best practice" is to not use winforms these days, but if thats where you want to start you can.
johndoe
johndoeOPā€¢2y ago
I selected Windows Forms app Some people in my class recommended it
Pobiega
Pobiegaā€¢2y ago
but this seems pretty straight forward, you'd use textboxes and labels on the left side, and if you want that grid-view style on the right, there is a control for that called gridview Thats fine, you can use winforms if you want. Just wanted to let you know since you asked for best practice
johndoe
johndoeOPā€¢2y ago
What would I use otherwise
Pobiega
Pobiegaā€¢2y ago
WPF most likely for a windows-only desktop GUI, WPF is the "default" but there are many alternatives. Avalonia has windows/linux/mac support, for example
johndoe
johndoeOPā€¢2y ago
WPF-App from the visual studio templates? And what is the difference between windows forms app and the windows pressentation one
Pobiega
Pobiegaā€¢2y ago
entirely different frameworks. WPF is declarative, kind of like html
johndoe
johndoeOPā€¢2y ago
I am very used to HTML, CSS and JavaScript. That is why Windows Forms was confusing to me
Pobiega
Pobiegaā€¢2y ago
while winforms is... not. you use a drag and drop system and its really hard to get things to behave properly when resized šŸ™‚ then perhaps consider WPF
johndoe
johndoeOPā€¢2y ago
You mean GridDataView, right?
Pobiega
Pobiegaā€¢2y ago
thats the one
johndoe
johndoeOPā€¢2y ago
Is it better to use than the TableLayoutPanel? I was very confused on which one I would rather use
Pobiega
Pobiegaā€¢2y ago
they are different. they look different, behave different pick the one that does what you want. I'm not a GUI expert by any means, but table layout panel doesnt look like what you want. assuming we rotate the grid to make sense šŸ˜› ie, scaling by row instead of column
johndoe
johndoeOPā€¢2y ago
Now I am also not sure if I choose "WPF-Application" or "WPF-App (.NET Framework)". Both an option here in the templates. For the latter it also says XAML
Pobiega
Pobiegaā€¢2y ago
never pick anything with ".NET Framework" in the name in 2023 thats the legacy version of .net
johndoe
johndoeOPā€¢2y ago
ah okay, so I will pick Wpf-app okay
Pobiega
Pobiegaā€¢2y ago
yup.
johndoe
johndoeOPā€¢2y ago
i can stil drag and drop now though but at the bottom i have this window which is kind of like html
Pobiega
Pobiegaā€¢2y ago
thats XAML, which is how we make things in WPF dont use the drag and drop šŸ™‚ $rulesofwpf
MODiX
MODiXā€¢2y ago
Rules of WPF:

āŒ Avoid the WPF Designer to eliminate a category of confusing bugs
āŒ Don't rely on Margin as the primary tool for layouts
āŒ Avoid writing UserControls or subclassing to extend a default control -- use Behaviors instead (Microsoft.Xaml.Behaviors.Wpf)

āœ… Write XAML by hand and autoformat with "Ctrl K,D" or XAML Styler
āœ… Rely upon XAML Hot Reload to design your app's UI at runtime
āœ… Use layout controls (Grid, DockPanel, etc) to support proper resizing
āœ… Use data binding to eliminate glue code and state synchronization issues
āœ… Use collection controls and DataTemplate to dynamically create lists of controls
āœ… Learn MVVM to create maintainable apps
āœ… Use the Dispatcher to update controls from non-UI threads
āœ… WPF's default controls can be easily modernized via $wpfuilibs
āœ… Include relevant XAML, code-behind, and ViewModel code for questions when possible
Rules of WPF:

āŒ Avoid the WPF Designer to eliminate a category of confusing bugs
āŒ Don't rely on Margin as the primary tool for layouts
āŒ Avoid writing UserControls or subclassing to extend a default control -- use Behaviors instead (Microsoft.Xaml.Behaviors.Wpf)

āœ… Write XAML by hand and autoformat with "Ctrl K,D" or XAML Styler
āœ… Rely upon XAML Hot Reload to design your app's UI at runtime
āœ… Use layout controls (Grid, DockPanel, etc) to support proper resizing
āœ… Use data binding to eliminate glue code and state synchronization issues
āœ… Use collection controls and DataTemplate to dynamically create lists of controls
āœ… Learn MVVM to create maintainable apps
āœ… Use the Dispatcher to update controls from non-UI threads
āœ… WPF's default controls can be easily modernized via $wpfuilibs
āœ… Include relevant XAML, code-behind, and ViewModel code for questions when possible
johndoe
johndoeOPā€¢2y ago
Okay, now to summarize: I will use a GroupBox for the sidebar to add an offer. Inside of that GroupBox I use Labels, Textbox, and NumericUpDown. For the right I use DataGridView. And for the text at the bottom I use TextBlock. Does that sound good?
Pobiega
Pobiegaā€¢2y ago
sounds like a good sstart it wont be too hard to change some of these, especially not with WPF
johndoe
johndoeOPā€¢2y ago
The only problem I had last time is that I used DockStyle: Left for the GroupBox and then DockStyle: Fill for the DataGridView. And then the GroupBox overlapped the DataGridView I never figured it out how to have them side by side
Pobiega
Pobiegaā€¢2y ago
so with WPF, thats easy to solve. you just make a grid with 2 columns, 1 row and drop those things into that grid (groupbox on the left, a new grid on the right (since you want the gridview AND the text))
johndoe
johndoeOPā€¢2y ago
I definitely like the xaml more than the annoying drag and drop Ah yes, two grid columns and then in the right column another grid. That is what you mean, right?
Pobiega
Pobiegaā€¢2y ago
yup
johndoe
johndoeOPā€¢2y ago
damn this is so much easier than forms thankk god It does not support NumericUpDown unfortunately. Is there an alternative?
Pobiega
Pobiegaā€¢2y ago
you could use a textbox, but if you really want specifically a numeric up down one, this toolkit contains one https://github.com/xceedsoftware/wpftoolkit
johndoe
johndoeOPā€¢2y ago
Alright, I think I will just use a TextBox. Another question, how would I make my DataGrid scrollable when all the columns do not fit on the screen?
Pobiega
Pobiegaā€¢2y ago
ĀÆ\_(惄)_/ĀÆ I still strongly recommend you let a row be the thing the user inserts thou instead of a column
johndoe
johndoeOPā€¢2y ago
yes, that is what i am doing right now there are very many columns
Pobiega
Pobiegaā€¢2y ago
so its way more than just name price discount, like in your example?
johndoe
johndoeOPā€¢2y ago
yeah. Wait, I will send a screen
johndoe
johndoeOPā€¢2y ago
@Pobiega full screen app:
johndoe
johndoeOPā€¢2y ago
That is why i initially wanted columns for less side scrolling
Pobiega
Pobiegaā€¢2y ago
Fair. Doesn't seem too bad thou.
johndoe
johndoeOPā€¢2y ago
Yeah, it is okay Why is it so much harder to work with columns
Pobiega
Pobiegaā€¢2y ago
Because thats how the control is intended to be used.
johndoe
johndoeOPā€¢2y ago
What I currently do to insert data also returns a bunch of errors
VergleichDataGrid.Items.Add(new {
Anbieter = anbieter.Text,
Menge = menge.Text,
Listenpreis = listenpreis.Text,
Mindermengenzuschlag = mindermengenzuschlag.Text,
Rabatt = rabatt.Text,
Skonto = skonto.Text,
Transportkosten = transportkosten.Text
});
VergleichDataGrid.Items.Add(new {
Anbieter = anbieter.Text,
Menge = menge.Text,
Listenpreis = listenpreis.Text,
Mindermengenzuschlag = mindermengenzuschlag.Text,
Rabatt = rabatt.Text,
Skonto = skonto.Text,
Transportkosten = transportkosten.Text
});
Pobiega
Pobiegaā€¢2y ago
such as?
johndoe
johndoeOPā€¢2y ago
@Pobiega
Error 1 <>f__AnonymousType0`7 List anbieter TextBlock.Text String The property "Anbieter" was not found in the object of type "<>f__AnonymousType0`7".
Error 1 <>f__AnonymousType0`7 List anbieter TextBlock.Text String The property "Anbieter" was not found in the object of type "<>f__AnonymousType0`7".
xaml:
<Label Target="{Binding ElementName=angebotName}">Anbieter</Label>
<TextBox Name="anbieter" />

<DataGridTextColumn Header="Anbieter" Binding="{Binding Anbieter}" />
<Label Target="{Binding ElementName=angebotName}">Anbieter</Label>
<TextBox Name="anbieter" />

<DataGridTextColumn Header="Anbieter" Binding="{Binding Anbieter}" />
seems like having an anonymous object causes trouble
Pobiega
Pobiegaā€¢2y ago
don't use anonymous types like that make an actual type
johndoe
johndoeOPā€¢2y ago
Ah, the erros are from the columns which are not filled out by the form. They are initially empty and only calculated once the other cells are filled out
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server