Windowsのフォームを組み込む

VB.NETに独自のActiveXコンポーネントを実装する代わりに、guixt.EmbedForm()関数を用いてSAP GUI画面にWindowsのフォームを組み込めます。

作成したVB.NETのDLLを、SAP WebリポジトリやSAP Mimeリポジトリなどの一元管理された場所に保存する場合、ユーザーのPCでインストールや登録を行う必要ありません。GuiXTはそのDLLを読み込み、それを実行可能なローカルのキャッシュに保存します。

このチュートリアルでは、顧客の販売情報を表示するサンプルを使用して、この組み込み手法について説明します。このサンプルは比較的広範囲にわたっており、この類のアプリケーションにおいて有用なさまざまな手法が含まれています。

Visual Studioでは、今回のフォームのデザインは以下のようになります。

Visual Studioのフォームデザイナー

このフォームには、さまざまなデータグリッド、ボタン、チャートが含まれています。SAP GUIでは、ユーザーインターフェイスは以下のようになります。

顧客番号を入力して”Start”ボタンを押す

顧客情報が表示され、いくつかのユーザーインターフェイスが用意されている

右上にあるテーブルには、過去5年間に販売された商品が、総売上金額の降順で表示されます。商品の行をクリックすると、選択した商品の販売チャートが表示されます。

このアクションは、データベースへのアクセスを追加することなくVB.NETにて実装できます。他にも、InputScriptが実行されるユーザーインターフェイスが用意されています。例えば”Fidelity 3 years”ボタンを押すと、顧客のfidelity package(お得意様パッケージ)の発注金額が生成されます。

“Fidelity 3 years”を押すとVA01が呼び出され、選択したパッケージが入力される

全体の実装は、GuiXTとVB.NETを組み合わせて実現します。データベースのアクセスとSAP GUIのアクションはGuiXTにより実行され、データグリッドやチャートを含むユーザーインターフェイスについてはVB.NETで実現します。

標準の.NETコントロールを使用して、用意された機能(ヘッダーをクリックして列を並べ替えるなど)を取得することができます。

商品名で並べ替える

顧客販売ダッシュボードは、任意のSAPトランザクションに組み込むことができます。ここでは、既存のSAPトランザクションに対するパラメータートランザクションとして、デモ用のトランザクション”ZCSO”を別途作成しました。今回の例では、”Start_Report”です。デモ用としては十分なものです。

全スクリプトとVB.NETプロジェクトの全体は、zcso.zipに収められています。自身でのテスト用にインストールする場合、無償のMSチャートパッケージも必要となります(Windowsには自動的にインストールされません)。今回は、zcso.zipにセットアップファイルも含めました。Visual Studio 2012以降を使用してください。zcso.zipをダウンロードした後、通常、このファイルを”ブロック解除”する必要があります(マウスを右クリック-> File attributes -> Unblock)。これを行わないと、このファイルの実行がWindowsによってブロックされることになります。あるいは、ファイルの解凍後に、各ファイルを個別にブロック解除してください。

GuiXTスクリプト

// ZCSO
// demo transaction of embedded Windows Forms

// delete existing elements (necessary if implemented as parameter transaction)
del (0,0) (20,120)

// initial screen?
if not V[zcso_main_screen=X]

  // title
Title “Customer Sales Order Dashboard”

InputField (1,1) “Customer” (1,20) size=10 name=“zcso_kunnr_ext” techname=“KNA1-KUNNR” -numerical

On “Enter” process=“zcso_init_enter.txt”
Pushbutton (toolbar) “Start” “/0” “F8” process=“zcso_init_enter.txt”

else

  // title
Title “&V[zcso_kunnr_ext]: &V[zcso_name1], &V[zcso_city], &V[zcso_country]”

  // area for VB.NET control.
  // We start at (-1,-1) and use a very large area in order to avoid any padding
Control (-1,-1) (50,300) progid=“Shell.Explorer” name=“r1” initflag=“r1init”

   // embed control
if V[r1init]
CallVB charts.zcso.embed “&V[r1]”
endif

Pushbutton (toolbar) “Other customer” process=“zcso_main_back.txt”
Pushbutton (toolbar) “Display customer data (XD03) process=“zcso_display_customer.txt”

On “Enter” process=“zcso_main_enter.txt”
On “/3” process=“zcso_main_back.txt”

endif

InputScipt zcso_init_enter.txt

// customer number specified?
if not V[zcso_kunnr_ext]
return “E: Please specify a customer number” -statusline
endif

// customer number with 10 digits
Set V[zcso_kunnr](1-10) “0000000000&V[zcso_kunnr_ext]” -alignright

// set organization key values (option:read from user parameters)
Set V[zcso_vkorg] “1000”
Set V[zcso_vtweg] “10”
Set V[zcso_spart] “00”

// prepare VB.NET form
StatusMessage title=“Processing…”

CallVB charts.zcso.initialize

// read customer address
StatusMessage addstring=“1 Address”
include “zcso_read_address.txt”

// read contacts
StatusMessage addstring=“2 Contacts”
include “zcso_read_contacts.txt”

// read orders
StatusMessage addstring=“3 Open orders”
include “zcso_read_orders.txt”

// read diagram data from S001
StatusMessage addstring=“4 Sales figures”
include “zcso_read_diagramdata.txt”

// build tables, diagrams etc.
CallVB charts.zcso.display

// display main screen
Set V[zcso_main_screen] “X”

StatusMessage -remove

Return
InputScipt zcso_read_address.txt

// read customer address
Call “/guixt/select” cache=“session” in.table=“KNA1” in.fields=“NAME1,ORT01,LAND1” in.condition=“KUNNR = ‘&V[zcso_kunnr]'” out.found=“found” out.v1=“zcso_name1” out.v2=“zcso_city” out.v3=“zcso_countrycode”

// found?
if not V[found]
StatusMessage -remove
return “E: Customer &V[zcso_kunnr_ext] not found” -statusline
endif

// read country text
Call “/guixt/select” cache=“session” in.table=“T005T” in.fields=“LANDX50” in.condition=“SPRAS = ‘&V[_language]’ AND LAND1 = ‘&V[zcso_countrycode]'” out.v1=”zcso_country”

InputScipt zcso_read_contacts.txt

// Build key table for function
Set V[crwa] “”
Set V[crwa](BAPICUSTOMER_IDRANGE-SIGN) “I”
Set V[crwa](BAPICUSTOMER_IDRANGE-OPTION) “EQ”
Set V[crwa](BAPICUSTOMER_IDRANGE-LOW) “&V[zcso_kunnr]”
Set text[cr] “&V[crwa]”

// clear result table
Set text[contacts] “”

// call function
Call “BAPI_CUSTOMER_GETCONTACTLIST” table.CUSTOMERRANGE=“cr” table.CONTACTADDRESSDATA=“contacts”

// transfer data to VB
Set V[i] 1

label contacts_line
CopyText fromText=“contacts” toString=“wa” line=&V[i]

if Q[ok]
Set V[zcso_contactname] “&V[wa](BAPICONTACT_ADDRESSDATA-LASTNAME), &V[wa](BAPICONTACT_ADDRESSDATA-FIRSTNAME)
Set V[zcso_contacttitle] “&V[wa](BAPICONTACT_ADDRESSDATA-FUNCTION)
Set V[zcso_contactphone] “&V[wa](BAPICONTACT_ADDRESSDATA-TEL1_NUMBR)
 Set V[zcso_contactemail] “&V[wa](BAPICONTACT_ADDRESSDATA-E_MAIL)

CallVB charts.zcso.newcontact

  Set V[i] &V[i] + 1

goto contacts_line
endif

InputScipt zcso_read_orders.txt

// read open orders

// search condition
Set V[condition] “KUNNR = ‘&V[zcso_kunnr]’ and VKORG = ‘&V[zcso_vkorg]'”
Set V[condition] “&V[condition] and VTWEG = ‘&V[zcso_vtweg]’ and SPART = ‘&V[zcso_spart]’ and TRVOG = ‘0’”
Set V[condition] “&V[condition] and AUDAT GE ‘&V[today-365_ymd]'”

// clear result tables
Set text[r1] “”
Set text[r2] “”
Set text[r3] “”
Set text[r4] “”
Set text[r5] “”

// read orders
Call /guixt/select in.Table=“VBAK” in.Condition=“&V[condition]” in.Fields=“VBELN,AUDAT,AUART,NETWR,BSTNK”table.V1table=“r1” table.V2table=“r2” table.V3table=“r3” table.V4table=“r4” table.V5table=“r5”

// pass to VB
CallVB charts.zcso.neworders

// clear result tables
Set text[r1] “”
Set text[r2] “”
Set text[r3] “”
Set text[r4] “”
Set text[r5] “”

InputScipt zcso_read_diagramdata.txt

// Determine years
Set V[zcso_year0] &V[today_y]
Set V[zcso_year1] &V[zcso_year0] – 1
Set V[zcso_year2] &V[zcso_year0] – 2
Set V[zcso_year3] &V[zcso_year0] – 3
Set V[zcso_year4] &V[zcso_year0] – 4

// search condition
Set V[condition] “KUNNR = ‘&V[zcso_kunnr]’ and VKORG = ‘&V[zcso_vkorg]’ and VTWEG = ‘&V[zcso_vtweg]’ and SPART = ‘&V[zcso_spart]’ and SPMON GE ‘&V[zcso_year4]01′”

// Clear result tables
Set text[r1] “”
Set text[r2] “”
Set text[r3] “”

// read VIS statistics table S001
Call /guixt/select in.Table=”S001″ in.Condition=“&V[condition]” in.Fields=“MATNR,SPMON,AENETWR”table.V1table=“r1” table.V2table=“r2” table.V3table=“r3”

// pass data to VB
CallVB charts.zcso.materialsales

// Clear result tables
Set text[r1] “”
Set text[r2] “”
Set text[r3] “”

// obtain all product numbers from VB
CallVB charts.zcso.products_to_guixt

// read product names
if text[zcso_products]
Set text[ctab] “”
Set V[i] 1

label next

CopyText fromText=“zcso_products” toString=“matnr” line=“&V[i]”
if Q[ok]
if V[i=1]
Set V[condline] “SPRAS = ‘E’ AND (MATNR = ‘&V[matnr]’
  else
Set V[condline] “OR MATNR = ‘&V[matnr]'”
endif

CopyText fromString=“condline” toText=“ctab” -appendline
Set V[i] &V[i] + 1
 goto next

endif

  if V[i>1] Set V[condline] “)”
CopyText fromString=“condline” toText=“ctab” -appendline
  endif

Call /guixt/select in.table=“MAKT” in.fields=“MATNR,MAKTX” table.conditiontable=“ctab”table.V1table=“r1” table.V2table=“r2”

endif

// product names to VB
CallVB charts.zcso.productnames_from_guixt

 

InputScipt zcso_create_order.txt

Parameter PROMOTION
Enter “/nVA01”

// Enter order type
Screen SAPMV45A.0101

Set F[VBAK-AUART] “TA”
Enter

// Enter customer number
Screen SAPMV45A.4001
Set F[KUAGV-KUNNR] “&V[zcso_kunnr]”
 if U[PROMOTION=1]
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,1] “M-20”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,1] “1”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,2] “PA-1000”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,2] “1”
endif

 if U[PROMOTION=2]
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,1] “M-20”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,1] “2”
endif

 if U[PROMOTION=3]
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,1] “PA-1000”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,1] “1”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,2] “M-10”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,2] “1”
Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-MABNR,3] “P-103”
 Set cell[SAPMV45A_TCTRL_U_ERF_AUFTRAG,RV45A-KWMENG,3] “1”
  endif

Enter

// no delivery proposal?
Screen SAPLATP4.0500
Enter “=WEIT”

// no delivery proposal?
Screen SAPLATP4.0500
Enter “=WEIT”

 

InputScipt zcso_display_order.txt

if not V[zcso_vbeln]
return “E: Please select an order” -statusline
endif

Enter “/nVA03”

// Display order
Screen SAPMV45A.0102
Set F[VBAK-VBELN] “&V[zcso_vbeln]”
Enter

// Skip Popup
Screen SAPMSDYP.0010
Enter

InputScipt zcso_change_order.txt

if not V[zcso_vbeln]
return “E: Please select an order” -statusline
endif

Enter “/nVA02”

// Display order
Screen SAPMV45A.0102
Set F[VBAK-VBELN] “&V[zcso_vbeln]”
Enter

// Skip Popup
Screen SAPMSDYP.0010
Enter

InputScipt Zcso_display_customer.txt

Enter “/nxd03”

// Display customer
Screen SAPMF02D.7101
Set F[RF02D-KUNNR] “&V[zcso_kunnr]”
Set F[RF02D-VKORG] “&V[zcso_vkorg]”
Set F[RF02D-VTWEG] “&V[zcso_vtweg]”
Set F[RF02D-SPART] “&V[zcso_spart]”

  Enter

InputScipt zcso_main_back.txt

// back to initial screen
Set V[zcso_main_screen] “”

Return

VB.NET

1 Imports guinet
2 Imports System.Windows.Forms
3 Imports System.Data
4 Imports System.Drawing
5 Imports System.Windows.Forms.DataVisualization.Charting
6
7
8
9 Public Class zcso
10
11
12 #Region “Private data”
13     ‘ interface to GuiXT
14     Private myguixt As guixt
15
16     ‘ Dictionary for sales per product and month
17     Private matdict1 As New Dictionary(Of String, Decimal)
18
19     ‘ Dictionary for sales per product
20     Private matdict2 As New Dictionary(Of String, Decimal)
21
22     ‘ Dictionary for product names
23     Private matdict3 As New Dictionary(Of String, String)
24
25 #End Region
26
27
28     ‘ embed into SAP GUI window
29     Public Sub embed(ByVal w As Object)
30         guixt.EmbedForm(Me, w)
31     End Sub
32
33     ‘ initialize for new customer data
34     Public Sub initialize()
35
36         ‘ create guixt object for communication with GuiXT
37         myguixt = New guixt
38
39         ‘ clear data grids
40         contacts.RowCount = 0
41         orders.RowCount = 0
42         matbuttons.RowCount = 0
43
44         ‘ clear product dictionaries
45         matdict1.Clear()
46         matdict2.Clear()
47         matdict3.Clear()
48
49     End Sub
50
51     ‘ final display, all customer data are read
52     Public Sub display()
53
54         ‘ sort contacts
55         contacts.Sort(contacts.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
56
57         ‘ sort orders
58         orders.Sort(orders.Columns(0), System.ComponentModel.ListSortDirection.Descending)
59
60
61         ‘ Build Sales chart
62         GenerateSalesChart(“*”)
63
64         ‘ no selection
65         contacts.ClearSelection()
66         orders.ClearSelection()
67
68
69     End Sub
70
71     ‘ Pass all product numbers to GuiXT via GuiXT long text variable
72     Public Sub products_to_guixt()
73
74         ‘ Pass product numbers back to GuiXT to obtain product names
75         myguixt.SetText(“zcso_products”, String.Join(vbCrLf, matdict2.Keys))
76
77     End Sub
78
79     ‘ Obtain all product texts from GuiXT via GuiXT long text variables
80     Public Sub productnames_from_guixt()
81
82         Dim products As String() = myguixt.GetText(“r1”).Split(vbCrLf)
83         Dim productnames As String() = myguixt.GetText(“r2”).Split(vbCrLf)
84
85         For k As Integer = 0 To products.Length – 1
86             matdict3.Add(products(k).Trim, productnames(k).Trim)
87         Next
88
89
90     End Sub
91
92 #Region “Event handling”
93
94
95     ‘Create new standard order
96     Private Sub buttoncreateorder_Click(sender As Object, e As EventArgs) Handles buttoncreateorder.Click
97         myguixt.Input(“OK:/0,process=zcso_create_order.txt”)
98     End Sub
99
100     ‘Create new order, promotion 1
101     Private Sub promotion1_Click(sender As Object, e As EventArgs) Handles promotion1.Click
102         myguixt.Input(“U[PROMOTION]:1;OK:/0,process=zcso_create_order.txt”)
103     End Sub
104
105     ‘Create new order, promotion 2
106     Private Sub promotion2_Click(sender As Object, e As EventArgs) Handles promotion2.Click
107         myguixt.Input(“U[PROMOTION]:2;OK:/0,process=zcso_create_order.txt”)
108     End Sub
109
110     ‘Create new order, promotion 3
111     Private Sub promotion3_Click(sender As Object, e As EventArgs) Handles promotion3.Click
112         myguixt.Input(“U[PROMOTION]:3;OK:/0,process=zcso_create_order.txt”)
113     End Sub
114
115     ‘ Display customer order
116     Private Sub buttondisplayyorder_Click(sender As Object, e As EventArgs) Handles buttondisplayorder.Click
117         Dim ordernumber As String = “”
118         For Each row In orders.SelectedRows
119             ordernumber = row.Cells(“ordervbeln”).Value()
120         Next
121
122         myguixt.SetVariable(“zcso_vbeln”, ordernumber)
123         myguixt.Input(“OK:/0,process=zcso_display_order.txt”)
124     End Sub
125
126     ‘ Change customer order
127     Private Sub buttonchangeorder_Click(sender As Object, e As EventArgs) Handles buttonchangeorder.Click
128
129         Dim ordernumber As String = “”
130         For Each row In orders.SelectedRows
131             ordernumber = row.Cells(“ordervbeln”).Value()
132         Next
133         myguixt.SetVariable(“zcso_vbeln”, ordernumber)
134         myguixt.Input(“OK:/0,process=zcso_change_order.txt”)
135     End Sub
136
137     ‘ Click on product button to generate chart
138     Private Sub matbuttons_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles matbuttons.CellClick
139
140         Dim product As String = “”
141         For Each row In matbuttons.SelectedRows
142             product = row.Cells(“prodnumber”).Value()
143         Next
144
145         GenerateSalesChart(product)
146
147     End Sub
148 #End Region
149
150
151 #Region “contacts”
152
153     ‘ called from GuiXT for each new contact
154     Public Sub newcontact()
155
156         Dim k As Integer = contacts.Rows.Add()
157
158         Dim c As System.Windows.Forms.DataGridViewCellCollection = contacts.Rows.Item(k).Cells
159
160         c(“contactname”).Value = myguixt.GetVariable(“zcso_contactname”)
161         c(“contacttitle”).Value = myguixt.GetVariable(“zcso_contacttitle”)
162         c(“contactphone”).Value = myguixt.GetVariable(“zcso_contactphone”)
163         c(“contactemail”).Value = myguixt.GetVariable(“zcso_contactemail”)
164
165     End Sub
166
167
168
169 #End Region
170
171 #Region “orders”
172
173     ‘ transfer order data from GuiXT to datagrid
174     Public Sub neworders()
175
176         Dim r1 As String() = myguixt.GetText(“r1”).Split(vbCrLf)
177         Dim r2 As String() = myguixt.GetText(“r2”).Split(vbCrLf)
178         Dim r3 As String() = myguixt.GetText(“r3”).Split(vbCrLf)
179         Dim r4 As String() = myguixt.GetText(“r4”).Split(vbCrLf)
180         Dim r5 As String() = myguixt.GetText(“r5”).Split(vbCrLf)
181
182
183         For k As Integer = 0 To r1.Length – 1
184
185             orders.Rows.Add()
186
187             Dim o As System.Windows.Forms.DataGridViewCellCollection = orders.Rows.Item(k).Cells
188
189             o(“ordervbeln”).Value = CInt(r1(k).Trim)
190             o(“orderaudat”).Value = r2(k).Trim
191             o(“orderauart”).Value = r3(k).Trim
192             o(“ordernetwr”).Value = Decimal.Parse(r4(k).Trim, Globalization.NumberStyles.AllowDecimalPoint).ToString()
193             o(“orderbstnk”).Value = r5(k).Trim
194
195         Next
196     End Sub
197
198
199 #End Region
200
201
202 #Region “materials”
203
204     Public Sub materialsales()
205
206         Dim r1 As String() = myguixt.GetText(“r1”).Split(vbCrLf)
207         Dim r2 As String() = myguixt.GetText(“r2”).Split(vbCrLf)
208         Dim r3 As String() = myguixt.GetText(“r3”).Split(vbCrLf)
209
210         For k As Integer = 0 To r1.Length – 1
211
212             Dim product As String = r1(k).Trim
213             Dim year As String = r2(k).Trim.Substring(0, 4)
214             Dim netwr As Decimal = Decimal.Parse(r3(k).Trim)
215
216
217             Dim key As String = year & product
218
219             If matdict1.ContainsKey(key) Then
220                 matdict1.Item(key) = matdict1.Item(key) + netwr
221             Else
222                 matdict1.Add(key, netwr)
223             End If
224
225
226             ‘ product only
227             If matdict2.ContainsKey(product) Then
228                 matdict2.Item(product) = matdict2.Item(product) + netwr
229             Else
230                 matdict2.Add(product, netwr)
231             End If
232
233         Next
234
235
236
237
238     End Sub
239
240
241
242 #End Region
243
244
245 #Region “charts”
246
247     Private total_netwr0 As Decimal = 0
248     Private total_netwr1 As Decimal = 0
249     Private total_netwr2 As Decimal = 0
250     Private total_netwr3 As Decimal = 0
251     Private total_netwr4 As Decimal = 0
252
253     Public Sub GenerateSalesChart(product As String)
254
255         ‘ create chart
256         Dim Chart1 As New Chart()
257
258         Chart1.Size = New System.Drawing.Size(salesdiagram.Size.Width, salesdiagram.Size.Height)
259         Chart1.BackColor = Color.FromArgb(240, 240, 240)
260
261         Dim ChartArea1 As New ChartArea
262
263         ChartArea1.Name = “ChartArea1”
264         Chart1.ChartAreas.Add(ChartArea1)
265
266         Dim Series1 As New Series
267         Series1.ChartArea = “ChartArea1”
268         Series1.Palette = ChartColorPalette.Pastel
269         Series1.XValueMember = “Year”
270         Series1.YValueMembers = “Sales”
271         Chart1.Series.Add(Series1)
272
273         Dim table As New DataTable
274
275         ‘ Create columns in the DataTable
276         table.Columns.Add(“Year”, GetType(String))
277         table.Columns.Add(“Sales”, GetType(Integer))
278
279         Dim year0 As String = myguixt.GetVariable(“zcso_year0”)
280         Dim year1 As String = myguixt.GetVariable(“zcso_year1”)
281         Dim year2 As String = myguixt.GetVariable(“zcso_year2”)
282         Dim year3 As String = myguixt.GetVariable(“zcso_year3”)
283         Dim year4 As String = myguixt.GetVariable(“zcso_year4”)
284
285         Dim netwr0 As Decimal = 0
286         Dim netwr1 As Decimal = 0
287         Dim netwr2 As Decimal = 0
288         Dim netwr3 As Decimal = 0
289         Dim netwr4 As Decimal = 0
290
291         For Each m In matdict1
292
293             If product = “*” Or product = “***” Or product = m.Key.Substring(4) Then
294                 Select Case m.Key.Substring(0, 4)
295
296                     Case year0
297                         netwr0 += m.Value
298                     Case year1
299                         netwr1 += m.Value
300                     Case year2
301                         netwr2 += m.Value
302                     Case year3
303                         netwr3 += m.Value
304                     Case year4
305                         netwr4 += m.Value
306
307                 End Select
308             End If
309         Next
310
311         ‘ Save total values
312         If product = “*” Then
313             total_netwr0 = netwr0
314             total_netwr1 = netwr1
315             total_netwr2 = netwr2
316             total_netwr3 = netwr3
317             total_netwr4 = netwr4
318
319
320             ‘ build matbuttons
321             Dim total_netwr As Decimal = total_netwr0 + total_netwr1 + total_netwr2 + total_netwr3 + total_netwr4
322
323
324
325             matbuttons.Rows.Add()
326
327             Dim mb As System.Windows.Forms.DataGridViewCellCollection = matbuttons.Rows.Item(0).Cells
328
329             mb(“percentage”).Value = “100”
330             mb(“product”).Value = “***”
331             mb(“prodnumber”).Value = “***”
332             mb(“prodname”).Value = “All products”
333             mb(“netwr”).Value = total_netwr
334
335             ‘ select first row
336             matbuttons.ClearSelection()
337             matbuttons.Rows.Item(0).Selected = True
338
339
340             For Each m In matdict2
341                 Dim k As Integer = matbuttons.Rows.Add()
342
343                 mb = matbuttons.Rows.Item(k).Cells
344
345
346                 Dim percentage As Integer
347                 ‘ avoid to divide by 0
348                 If total_netwr = 0 Then
349                     percentage = 0
350                 Else
351                     percentage = CInt(m.Value * 100 / total_netwr)
352                 End If
353
354
355
356                 mb(“percentage”).Value = percentage.ToString
357
358
359                 ‘ Suppress leading 000
360                 If IsNumeric(m.Key) Then
361                     mb(“product”).Value = CInt(m.Key).ToString
362                 Else
363                     mb(“product”).Value = m.Key
364                 End If
365
366                 mb(“prodnumber”).Value = m.Key
367                 mb(“prodname”).Value = matdict3.Item(m.Key)
368
369                 mb(“netwr”).Value = m.Value
370
371             Next
372
373
374             ‘ sortmatbuttons
375             matbuttons.Sort(matbuttons.Columns(“netwr”), System.ComponentModel.ListSortDirection.Descending)
376
377         End If
378
379
380         ‘ Sales figures for 5 years
381         table.Rows.Add(year4, CInt(netwr4))
382         table.Rows.Add(year3, CInt(netwr3))
383         table.Rows.Add(year2, CInt(netwr2))
384         table.Rows.Add(year1, CInt(netwr1))
385         table.Rows.Add(year0, CInt(netwr0))
386
387         ‘ Chart data
388         Chart1.DataSource = table
389
390
391         Dim bmp As New Bitmap(Chart1.Width, Chart1.Height)
392         Dim rec As New Rectangle(0, 0, Chart1.Width, Chart1.Height)
393         Chart1.DrawToBitmap(bmp, rec)
394
395         salesdiagram.Image = bmp
396
397
398         ‘ Title variable
399         If product = “*” OrElse product = “***” Then
400             product_selected.Text = “All products”
401         Else
402             product_selected.Text = product & ” ” & matdict3.Item(product)
403         End If
404
405         salesdiagram.Refresh()
406
407     End Sub
408
409
410
411 #End Region
412
413
414 End Class