とりあえず作ってみた。その2。

前回はこれです。→とりあえず作ってみた。
ひろえむさんのとこと同じようにTextBoxを継承してみました。基本的におんなじことしてるはず・・・。
επιστημη さんとこ


このコントロール、int 計算(int price) ってメソッドと、
int XXX(int price) をdelegateとするevent: 横流しを持ってます。

int 計算(int price) では、たとえば一万円であれば price を 10000 で割った商を
枠で囲ったラベルに書き込み、余りを 横流しの引数にして呼び出します。

なので
 一万円.横流し += 五千円.計算;
 五千円.横流し += 千円.計算;
 千円.横流し += 五百円.計算;

ってなイベントの連鎖をこしらえておき、
一万円.計算(入金) すると金種計算が行われるですます。

が実装の元です。

[Visual Basic]

Imports System
Imports System.Windows.Forms

Namespace Torikobito.GennyControl


Public Class 貨幣
Inherits TextBox
Implements IComparable(Of 貨幣)


Public Event 横流し(ByVal price As Integer)


Private _金額 As Integer = -1

Public Property 金額() As Integer
Get
Return Me._金額
End Get
Set(ByVal value As Integer)
Me._金額 = value
End Set
End Property


Private _計算結果 As Integer = 0

Public ReadOnly Property 計算結果() As Integer
Get
Return Me._計算結果
End Get
End Property


Public Sub New()
MyBase.New()

Me.ReadOnly = True
Me.Text = Me.計算結果.ToString

End Sub


Public Sub 計算(ByVal 値段 As Integer)

Me._計算結果 = 値段 \ Me._金額
Me.Text = Me.計算結果.ToString

Dim 横流しする金額 As Integer = 値段 Mod Me._金額

Me.On横流し(横流しする金額)

End Sub


Protected Sub On横流し(ByVal 横流し金額 As Integer)

RaiseEvent 横流し(横流し金額)

End Sub


Public Function CompareTo(ByVal other As 貨幣) As Integer Implements System.IComparable(Of 貨幣).CompareTo

Return Me.金額 - other.金額

End Function


End Class


End Namespace
で、フォーム側。ちょっとLoadイベントでいろいろやりすぎかも。

[Visual Basic]

Imports System
Imports System.Collections.Generic
Imports Torikobito.GennyControl


Public Class GennyForm


Private 一番大きな金額の貨幣 As 貨幣

Private Sub GennyForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' ほんとはデザイナから設定できるけどエントリ用にコードで書いておく。
Me.一円玉.金額 = 1
Me.五円玉.金額 = 5
Me.十円玉.金額 = 10
Me.五十円玉.金額 = 50
Me.百円玉.金額 = 100
Me.五百円玉.金額 = 500
Me.千円札.金額 = 1000
Me.五千円札.金額 = 5000
Me.一万円札.金額 = 10000

Dim 貨幣リスト As New List(Of 貨幣)

貨幣リスト.Add(Me.一円玉)
貨幣リスト.Add(Me.五円玉)
貨幣リスト.Add(Me.十円玉)
貨幣リスト.Add(Me.五十円玉)
貨幣リスト.Add(Me.百円玉)
貨幣リスト.Add(Me.五百円玉)
貨幣リスト.Add(Me.千円札)
貨幣リスト.Add(Me.五千円札)
貨幣リスト.Add(Me.一万円札)

貨幣リスト.Sort()
貨幣リスト.Reverse()

Me.一番大きな金額の貨幣 = 貨幣リスト.Item(0)

For i As Integer = 0 To 貨幣リスト.Count - 2

AddHandler 貨幣リスト.Item(i).横流し, AddressOf 貨幣リスト.Item(i + 1).計算

Next i

End Sub


Private Sub おあいそButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles おあいそButton.Click

Dim 計算する金額 As Integer

If Integer.TryParse(Me.お支払い金額TextBox.Text, 計算する金額) Then

Me.一番大きな金額の貨幣.計算(計算する金額)

End If

End Sub



End Class
相変わらず二千円札がなかったりしてw