From 5a0d61fa98a5a741e60895db66cd5aa96d8247fb Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 10 Mar 2021 18:09:33 +0200 Subject: Moved input tab and purchase tab logic ot their own files (partial classes), improved names of methods, removed unecessary method parameters (sender and e) --- .../Bookstore/Bookstore/Bookstore.vbproj.user | 20 +++- .../Bookstore/Bookstore/Form1.Designer.vb | 122 ++++++++++----------- .../Bookstore/Bookstore/Form1.InputTab.vb | 56 ++++++++++ .../Bookstore/Bookstore/Form1.PurchaseTab.vb | 41 +++++++ VB.Net Projects/Bookstore/Bookstore/Form1.vb | 92 ---------------- VB.Net Projects/Bookstore/Bookstore/Purchase.vb | 2 +- 6 files changed, 173 insertions(+), 160 deletions(-) create mode 100644 VB.Net Projects/Bookstore/Bookstore/Form1.InputTab.vb create mode 100644 VB.Net Projects/Bookstore/Bookstore/Form1.PurchaseTab.vb diff --git a/VB.Net Projects/Bookstore/Bookstore/Bookstore.vbproj.user b/VB.Net Projects/Bookstore/Bookstore/Bookstore.vbproj.user index c392cf0..5f63513 100644 --- a/VB.Net Projects/Bookstore/Bookstore/Bookstore.vbproj.user +++ b/VB.Net Projects/Bookstore/Bookstore/Bookstore.vbproj.user @@ -1,8 +1,16 @@  - - - Form - - - + + + Form + Form1.vb + + + Form + Form1.vb + + + Form + + + \ No newline at end of file diff --git a/VB.Net Projects/Bookstore/Bookstore/Form1.Designer.vb b/VB.Net Projects/Bookstore/Bookstore/Form1.Designer.vb index 9299ce9..594794d 100644 --- a/VB.Net Projects/Bookstore/Bookstore/Form1.Designer.vb +++ b/VB.Net Projects/Bookstore/Bookstore/Form1.Designer.vb @@ -23,7 +23,7 @@ Partial Class Form1 _ Private Sub InitializeComponent() Me.TabControl1 = New System.Windows.Forms.TabControl() - Me.TabPage1 = New System.Windows.Forms.TabPage() + Me.tbInputTab = New System.Windows.Forms.TabPage() Me.cbGenre = New System.Windows.Forms.ComboBox() Me.txtAmount = New System.Windows.Forms.TextBox() Me.txtPrice = New System.Windows.Forms.TextBox() @@ -44,7 +44,7 @@ Partial Class Form1 Me.btnReport = New System.Windows.Forms.Button() Me.btnSave = New System.Windows.Forms.Button() Me.btnLoad = New System.Windows.Forms.Button() - Me.TabPage2 = New System.Windows.Forms.TabPage() + Me.tbPurchaseTab = New System.Windows.Forms.TabPage() Me.btnClearPurchases = New System.Windows.Forms.Button() Me.lblSum = New System.Windows.Forms.Label() Me.Label12 = New System.Windows.Forms.Label() @@ -57,49 +57,49 @@ Partial Class Form1 Me.cbPurchase = New System.Windows.Forms.ComboBox() Me.Label2 = New System.Windows.Forms.Label() Me.TabControl1.SuspendLayout() - Me.TabPage1.SuspendLayout() - Me.TabPage2.SuspendLayout() + Me.tbInputTab.SuspendLayout() + Me.tbPurchaseTab.SuspendLayout() Me.SuspendLayout() ' 'TabControl1 ' - Me.TabControl1.Controls.Add(Me.TabPage1) - Me.TabControl1.Controls.Add(Me.TabPage2) + Me.TabControl1.Controls.Add(Me.tbInputTab) + Me.TabControl1.Controls.Add(Me.tbPurchaseTab) Me.TabControl1.Location = New System.Drawing.Point(12, 12) Me.TabControl1.Name = "TabControl1" Me.TabControl1.SelectedIndex = 0 Me.TabControl1.Size = New System.Drawing.Size(460, 337) Me.TabControl1.TabIndex = 0 ' - 'TabPage1 - ' - Me.TabPage1.Controls.Add(Me.cbGenre) - Me.TabPage1.Controls.Add(Me.txtAmount) - Me.TabPage1.Controls.Add(Me.txtPrice) - Me.TabPage1.Controls.Add(Me.txtPublisher) - Me.TabPage1.Controls.Add(Me.txtAuthor) - Me.TabPage1.Controls.Add(Me.txtTitle) - Me.TabPage1.Controls.Add(Me.Label8) - Me.TabPage1.Controls.Add(Me.Label7) - Me.TabPage1.Controls.Add(Me.Label6) - Me.TabPage1.Controls.Add(Me.Label5) - Me.TabPage1.Controls.Add(Me.Label4) - Me.TabPage1.Controls.Add(Me.Label3) - Me.TabPage1.Controls.Add(Me.cbReport) - Me.TabPage1.Controls.Add(Me.lblTotalCount) - Me.TabPage1.Controls.Add(Me.Label1) - Me.TabPage1.Controls.Add(Me.btnCreateNew) - Me.TabPage1.Controls.Add(Me.btnAdd) - Me.TabPage1.Controls.Add(Me.btnReport) - Me.TabPage1.Controls.Add(Me.btnSave) - Me.TabPage1.Controls.Add(Me.btnLoad) - Me.TabPage1.Location = New System.Drawing.Point(4, 24) - Me.TabPage1.Name = "TabPage1" - Me.TabPage1.Padding = New System.Windows.Forms.Padding(3) - Me.TabPage1.Size = New System.Drawing.Size(452, 309) - Me.TabPage1.TabIndex = 0 - Me.TabPage1.Text = "Въвеждане" - Me.TabPage1.UseVisualStyleBackColor = True + 'tbInputTab + ' + Me.tbInputTab.Controls.Add(Me.cbGenre) + Me.tbInputTab.Controls.Add(Me.txtAmount) + Me.tbInputTab.Controls.Add(Me.txtPrice) + Me.tbInputTab.Controls.Add(Me.txtPublisher) + Me.tbInputTab.Controls.Add(Me.txtAuthor) + Me.tbInputTab.Controls.Add(Me.txtTitle) + Me.tbInputTab.Controls.Add(Me.Label8) + Me.tbInputTab.Controls.Add(Me.Label7) + Me.tbInputTab.Controls.Add(Me.Label6) + Me.tbInputTab.Controls.Add(Me.Label5) + Me.tbInputTab.Controls.Add(Me.Label4) + Me.tbInputTab.Controls.Add(Me.Label3) + Me.tbInputTab.Controls.Add(Me.cbReport) + Me.tbInputTab.Controls.Add(Me.lblTotalCount) + Me.tbInputTab.Controls.Add(Me.Label1) + Me.tbInputTab.Controls.Add(Me.btnCreateNew) + Me.tbInputTab.Controls.Add(Me.btnAdd) + Me.tbInputTab.Controls.Add(Me.btnReport) + Me.tbInputTab.Controls.Add(Me.btnSave) + Me.tbInputTab.Controls.Add(Me.btnLoad) + Me.tbInputTab.Location = New System.Drawing.Point(4, 24) + Me.tbInputTab.Name = "tbInputTab" + Me.tbInputTab.Padding = New System.Windows.Forms.Padding(3) + Me.tbInputTab.Size = New System.Drawing.Size(452, 309) + Me.tbInputTab.TabIndex = 0 + Me.tbInputTab.Text = "Въвеждане" + Me.tbInputTab.UseVisualStyleBackColor = True ' 'cbGenre ' @@ -272,26 +272,26 @@ Partial Class Form1 Me.btnLoad.Text = "Зареди" Me.btnLoad.UseVisualStyleBackColor = True ' - 'TabPage2 - ' - Me.TabPage2.Controls.Add(Me.btnClearPurchases) - Me.TabPage2.Controls.Add(Me.lblSum) - Me.TabPage2.Controls.Add(Me.Label12) - Me.TabPage2.Controls.Add(Me.lbChosenBooks) - Me.TabPage2.Controls.Add(Me.btnPurchase) - Me.TabPage2.Controls.Add(Me.Label11) - Me.TabPage2.Controls.Add(Me.lblPrice) - Me.TabPage2.Controls.Add(Me.Label9) - Me.TabPage2.Controls.Add(Me.txtAmountToBuy) - Me.TabPage2.Controls.Add(Me.cbPurchase) - Me.TabPage2.Controls.Add(Me.Label2) - Me.TabPage2.Location = New System.Drawing.Point(4, 24) - Me.TabPage2.Name = "TabPage2" - Me.TabPage2.Padding = New System.Windows.Forms.Padding(3) - Me.TabPage2.Size = New System.Drawing.Size(452, 309) - Me.TabPage2.TabIndex = 1 - Me.TabPage2.Text = "Продажба" - Me.TabPage2.UseVisualStyleBackColor = True + 'tbPurchaseTab + ' + Me.tbPurchaseTab.Controls.Add(Me.btnClearPurchases) + Me.tbPurchaseTab.Controls.Add(Me.lblSum) + Me.tbPurchaseTab.Controls.Add(Me.Label12) + Me.tbPurchaseTab.Controls.Add(Me.lbChosenBooks) + Me.tbPurchaseTab.Controls.Add(Me.btnPurchase) + Me.tbPurchaseTab.Controls.Add(Me.Label11) + Me.tbPurchaseTab.Controls.Add(Me.lblPrice) + Me.tbPurchaseTab.Controls.Add(Me.Label9) + Me.tbPurchaseTab.Controls.Add(Me.txtAmountToBuy) + Me.tbPurchaseTab.Controls.Add(Me.cbPurchase) + Me.tbPurchaseTab.Controls.Add(Me.Label2) + Me.tbPurchaseTab.Location = New System.Drawing.Point(4, 24) + Me.tbPurchaseTab.Name = "tbPurchaseTab" + Me.tbPurchaseTab.Padding = New System.Windows.Forms.Padding(3) + Me.tbPurchaseTab.Size = New System.Drawing.Size(452, 309) + Me.tbPurchaseTab.TabIndex = 1 + Me.tbPurchaseTab.Text = "Продажба" + Me.tbPurchaseTab.UseVisualStyleBackColor = True ' 'btnClearPurchases ' @@ -401,16 +401,16 @@ Partial Class Form1 Me.Name = "Form1" Me.Text = "Книжарница" Me.TabControl1.ResumeLayout(False) - Me.TabPage1.ResumeLayout(False) - Me.TabPage1.PerformLayout() - Me.TabPage2.ResumeLayout(False) - Me.TabPage2.PerformLayout() + Me.tbInputTab.ResumeLayout(False) + Me.tbInputTab.PerformLayout() + Me.tbPurchaseTab.ResumeLayout(False) + Me.tbPurchaseTab.PerformLayout() Me.ResumeLayout(False) End Sub Friend WithEvents TabControl1 As TabControl - Friend WithEvents TabPage1 As TabPage + Friend WithEvents tbInputTab As TabPage Friend WithEvents cbGenre As ComboBox Friend WithEvents txtAmount As TextBox Friend WithEvents txtPrice As TextBox @@ -431,7 +431,7 @@ Partial Class Form1 Friend WithEvents btnReport As Button Friend WithEvents btnSave As Button Friend WithEvents btnLoad As Button - Friend WithEvents TabPage2 As TabPage + Friend WithEvents tbPurchaseTab As TabPage Friend WithEvents btnClearPurchases As Button Friend WithEvents lblSum As Label Friend WithEvents Label12 As Label diff --git a/VB.Net Projects/Bookstore/Bookstore/Form1.InputTab.vb b/VB.Net Projects/Bookstore/Bookstore/Form1.InputTab.vb new file mode 100644 index 0000000..32d934a --- /dev/null +++ b/VB.Net Projects/Bookstore/Bookstore/Form1.InputTab.vb @@ -0,0 +1,56 @@ +Imports System.IO +Imports Newtonsoft.Json + +Partial Class Form1 ' InputTab + + Private Sub DefineInputTabDataSources() Handles MyBase.Load + cbPurchase.DataSource = Books + lbChosenBooks.DataSource = Purchases + End Sub + + Private Sub AddBook() Handles btnAdd.Click + If Not IsNumeric(txtPrice.Text) Or Not IsNumeric(txtAmount.Text) Then + MsgBox("Цена и брой могат да бъдат само числа!", MsgBoxStyle.Critical, "Грешка!") + Return + End If + + Books.Add(New Book(txtTitle.Text, txtPublisher.Text, txtAuthor.Text, cbGenre.SelectedIndex, txtPrice.Text, txtAmount.Text)) + MsgBox("Книгата е добавена!", MsgBoxStyle.OkOnly, "Успех!") + End Sub + + Private Sub SaveToFile() Handles btnSave.Click + File.WriteAllText(filePath, JsonConvert.SerializeObject(Books)) + MsgBox("Данните са запазени!", MsgBoxStyle.OkOnly, "Успех!") + End Sub + + Private Sub LoadFromFile() Handles btnLoad.Click + Dim deserializedBooks = JsonConvert.DeserializeObject(Of List(Of Book))(File.ReadAllText(filePath)) + Books.Clear() + deserializedBooks.ForEach(Sub(b) Books.Add(b)) + MsgBox("Данните са заредени!", MsgBoxStyle.OkOnly, "Успех!") + End Sub + + Private Sub ShowReportBook() Handles btnReport.Click + If cbReport.SelectedItem Is Nothing Then + Return + End If + + Dim selectedBook As Book = cbReport.SelectedItem + txtTitle.Text = selectedBook.Title + txtAuthor.Text = selectedBook.Author + txtPublisher.Text = selectedBook.Publisher + txtPrice.Text = selectedBook.Price + txtAmount.Text = selectedBook.Amount + cbGenre.SelectedIndex = selectedBook.GenreIndex + End Sub + + Private Sub ClearInputs() Handles btnCreateNew.Click, MyBase.Load + For Each control As Control In {txtTitle, txtAuthor, txtAmount, txtPrice, txtPublisher} + control.ResetText() + Next + End Sub + + Private Sub UpdateTotalCountLabel() Handles btnAdd.Click, btnLoad.Click, MyBase.Load + lblTotalCount.Text = Books.Count() + End Sub +End Class \ No newline at end of file diff --git a/VB.Net Projects/Bookstore/Bookstore/Form1.PurchaseTab.vb b/VB.Net Projects/Bookstore/Bookstore/Form1.PurchaseTab.vb new file mode 100644 index 0000000..8ee6eda --- /dev/null +++ b/VB.Net Projects/Bookstore/Bookstore/Form1.PurchaseTab.vb @@ -0,0 +1,41 @@ +Partial Class Form1 ' PurchaseTab + + Private Sub DefinePurchaseTabDataSources() Handles MyBase.Load + cbReport.DataSource = Books + cbGenre.DataSource = Book.Genres + End Sub + + Private Sub PurchaseSelectedBook() Handles btnPurchase.Click + If Books(cbPurchase.SelectedIndex).Amount = 0 Then + MsgBox("Няма налични издания на тази книга!", MsgBoxStyle.Critical, "Грешка!") + Return + ElseIf Not IsNumeric(txtAmountToBuy.Text) Then + MsgBox("Трябва да въведете брой на закупени издания!", MsgBoxStyle.Critical, "Грешка!") + Return + ElseIf txtAmountToBuy.Text > Books(cbPurchase.SelectedIndex).Amount Then + MsgBox($"Има само {Books(cbPurchase.SelectedIndex).Amount} брой налични издания!", MsgBoxStyle.Critical, "Грешка!") + Return + End If + + Dim purchasedBook = cbPurchase.SelectedItem + Purchases.Add(New Purchase(purchasedBook.Title, purchasedBook.Price, txtAmountToBuy.Text)) + purchasedBook.Amount -= txtAmountToBuy.Text + End Sub + + Private Sub ClearPurchases() Handles btnClearPurchases.Click, btnLoad.Click + Purchases.Clear() + txtAmountToBuy.ResetText() + End Sub + + Private Sub UpdateItemPriceLabel() Handles cbPurchase.SelectedIndexChanged, btnLoad.Click, MyBase.Load + If cbPurchase.SelectedIndex > -1 Then + lblPrice.Text = FormatCurrency(Books(cbPurchase.SelectedIndex).Price, 2) + Else + lblPrice.Text = FormatCurrency(0, 2) + End If + End Sub + + Private Sub UpdateSumLabel() Handles btnPurchase.Click, btnClearPurchases.Click, btnLoad.Click, MyBase.Load + lblSum.Text = FormatCurrency(Purchases.Sum(Function(p) p.ItemPrice * p.Amount), 2) + End Sub +End Class \ No newline at end of file diff --git a/VB.Net Projects/Bookstore/Bookstore/Form1.vb b/VB.Net Projects/Bookstore/Bookstore/Form1.vb index 56cadb4..5519dcf 100644 --- a/VB.Net Projects/Bookstore/Bookstore/Form1.vb +++ b/VB.Net Projects/Bookstore/Bookstore/Form1.vb @@ -1,99 +1,7 @@ Imports System.ComponentModel -Imports System.IO -Imports Newtonsoft.Json Public Class Form1 ReadOnly filePath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\Desktop\data.json" ReadOnly Books As BindingList(Of Book) = New BindingList(Of Book) ReadOnly Purchases As BindingList(Of Purchase) = New BindingList(Of Purchase) - - Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load - cbReport.DataSource = Books - cbGenre.DataSource = Book.Genres - cbPurchase.DataSource = Books - lbChosenBooks.DataSource = Purchases - End Sub - - ' Въвеждане tab - - Private Sub ClearInputs(sender As Object, e As EventArgs) Handles btnCreateNew.Click, MyBase.Load - For Each control As Control In {txtTitle, txtAuthor, txtAmount, txtPrice, txtPublisher} - control.ResetText() - Next - End Sub - - Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click - If Not IsNumeric(txtPrice.Text) Or Not IsNumeric(txtAmount.Text) Then - MsgBox("Цена и брой могат да бъдат само числа!", MsgBoxStyle.Critical, "Грешка!") - Return - End If - - Books.Add(New Book(txtTitle.Text, txtPublisher.Text, txtAuthor.Text, cbGenre.SelectedIndex, txtPrice.Text, txtAmount.Text)) - MsgBox("Книгата е добавена!", MsgBoxStyle.DefaultButton1, "Успех!") - End Sub - - Private Sub btnReport_Click(sender As Object, e As EventArgs) Handles btnReport.Click - If cbReport.SelectedItem Is Nothing Then - Return - End If - - txtTitle.Text = Books(cbReport.SelectedIndex).Title - txtAuthor.Text = Books(cbReport.SelectedIndex).Author - cbGenre.SelectedIndex = Books(cbReport.SelectedIndex).GenreIndex - txtPublisher.Text = Books(cbReport.SelectedIndex).Publisher - txtPrice.Text = Books(cbReport.SelectedIndex).Price - txtAmount.Text = Books(cbReport.SelectedIndex).Amount - End Sub - - Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click - File.WriteAllText(filePath, JsonConvert.SerializeObject(Books)) - MsgBox("Данните са запазени!", MsgBoxStyle.OkOnly, "Успех!") - End Sub - - Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click - Dim deserializedBooks = JsonConvert.DeserializeObject(Of List(Of Book))(File.ReadAllText(filePath)) - Books.Clear() - deserializedBooks.ForEach(Sub(b) Books.Add(b)) - MsgBox("Данните са заредени!", MsgBoxStyle.OkOnly, "Успех!") - End Sub - - Private Sub UpdateTotalCountLabel(sender As Object, e As EventArgs) Handles btnAdd.Click, btnLoad.Click, MyBase.Load - lblTotalCount.Text = Books.Count() - End Sub - - ' Продажба tab - - Private Sub cbPurchase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbPurchase.SelectedIndexChanged, btnLoad.Click, MyBase.Load - If cbPurchase.SelectedIndex > -1 Then - lblPrice.Text = FormatCurrency(Books(cbPurchase.SelectedIndex).Price, 2) - Else - lblPrice.Text = FormatCurrency(0, 2) - End If - End Sub - - Private Sub btnPurchase_Click(sender As Object, e As EventArgs) Handles btnPurchase.Click - If Books(cbPurchase.SelectedIndex).Amount = 0 Then - MsgBox("Няма налични издания на тази книга!", MsgBoxStyle.Critical, "Грешка!") - Return - ElseIf Not IsNumeric(txtAmountToBuy.Text) Then - MsgBox("Трябва да въведете брой на закупени издания!", MsgBoxStyle.Critical, "Грешка!") - Return - ElseIf txtAmountToBuy.Text > Books(cbPurchase.SelectedIndex).Amount Then - MsgBox($"Има само {Books(cbPurchase.SelectedIndex).Amount} брой налични издания!", MsgBoxStyle.Critical, "Грешка!") - Return - End If - - Dim purchasedBook = Books(cbPurchase.SelectedIndex) - Purchases.Add(New Purchase(purchasedBook.Title, purchasedBook.Price, txtAmountToBuy.Text)) - purchasedBook.Amount -= txtAmountToBuy.Text - End Sub - - Private Sub btnClearPurchases_Click(sender As Object, e As EventArgs) Handles btnClearPurchases.Click, btnLoad.Click - Purchases.Clear() - txtAmountToBuy.ResetText() - End Sub - - Private Sub UpdateSum(sender As Object, e As EventArgs) Handles btnPurchase.Click, btnClearPurchases.Click, btnLoad.Click, MyBase.Load - lblSum.Text = FormatCurrency(Purchases.Sum(Function(p) p.ItemPrice * p.Amount), 2) - End Sub End Class diff --git a/VB.Net Projects/Bookstore/Bookstore/Purchase.vb b/VB.Net Projects/Bookstore/Bookstore/Purchase.vb index 6bb9171..29da649 100644 --- a/VB.Net Projects/Bookstore/Bookstore/Purchase.vb +++ b/VB.Net Projects/Bookstore/Bookstore/Purchase.vb @@ -12,4 +12,4 @@ Public Overrides Function ToString() As String Return $"{Me.Amount} {Me.ItemTitle}" End Function -End Class +End Class \ No newline at end of file -- cgit v1.2.3