Instead of implementing your own ActiveX component in VB.NET you can embed a Windows form into a SAP GUI screen using the guixt.EmbedForm() function.

When you store your VB.NET dll in a central location such as SAP Web Repository or Mime Repository no installation or registration will be needed on the user's PC. GUiXT loads the dll and stores it in a local cache where it can be executed.

In this tutorial we demonstrate the  embedding technique using an example that displays customer sales information. The example is relatively extensive and contains various techniques that can be useful for applications of this kind.


In Visual Studio the design of our form looks as follows:


Visual Studio, Forms Designer

The form contains various data grids, buttons and a chart. In SAP GUI the user interface is at follows:


Enter a customer number and press the "Start"" button

 

 


The customer info is displayed , offering several user interactions

 

The table at the upper right hand side contains the products sold during the past 5 years, in descending order of total sales value. By clicking on a product line you can display the sales chart for the selected product:


Sales chart for selected product


This action is implemented in VB.NET without requiring further database access. There are other user interactions that result in executing an InputScript, for example the "Fidelity 3 years" button which generates a customer order for the fidelity package.


"Fidelity 3 years"  calls up VA01 and enters the selected package.

The whole implementation is a joint effort of  GuiXT and VB.NET: the database access and the SAP GUI actions are carried out by GuiXT and the user interface, with data grids and the chart, are done in VB.NET.

By using the standard .NET controls we get a couple of features thrown in, for example ordering a column by clicking on its header:


Order by product name

 

Our customer sales dashboard can be embedded into any SAP transaction. Here we have created a separate demo transaction "ZCSO"  as a parameter transaction for an existing SAP transaction; "Start_Report" in our example, which is sufficient for demo purposes.

 You find all scripts and the whole VB.NET project in zcso.zip.  If you want to install it for your own tests you will also need the free MS Chart package which is not auomatically installed in Windows. We have included the setup file in zcso.zip. Use Visual Studio 2012 or higher. After downloading the file zcso.zip it will often be necessary to "unblock" it (right mouse click -> File attributes -> Unblock), otherwise the execution of the files will be blocked by Windows later on. Or unblock each file separately after unziping the files.

GuiXT Skript

// 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
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
Imports guinet
Imports System.Windows.Forms
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms.DataVisualization.Charting



Public Class zcso


#Region "Private data"
    ' interface to GuiXT
    Private myguixt As guixt

    ' Dictionary for sales per product and month
    Private matdict1 As New Dictionary(Of String, Decimal)

    ' Dictionary for sales per product
    Private matdict2 As New Dictionary(Of String, Decimal)

    ' Dictionary for product names
    Private matdict3 As New Dictionary(Of String, String)

#End Region
   

    ' embed into SAP GUI window
    Public Sub embed(ByVal w As Object)
        guixt.EmbedForm(Me, w)
    End Sub

    ' initialize for new customer data 
    Public Sub initialize()

        ' create guixt object for communication with GuiXT
        myguixt = New guixt

        ' clear data grids
        contacts.RowCount = 0
        orders.RowCount = 0
        matbuttons.RowCount = 0

        ' clear product dictionaries
        matdict1.Clear()
        matdict2.Clear()
        matdict3.Clear()

    End Sub

    ' final display, all customer data are read
    Public Sub display()

        ' sort contacts
        contacts.Sort(contacts.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

        ' sort orders
        orders.Sort(orders.Columns(0), System.ComponentModel.ListSortDirection.Descending)


        ' Build Sales chart
        GenerateSalesChart("*")

        ' no selection
        contacts.ClearSelection()
        orders.ClearSelection()


    End Sub

    ' Pass all product numbers to GuiXT via GuiXT long text variable
    Public Sub products_to_guixt()

        ' Pass product numbers back to GuiXT to obtain product names
        myguixt.SetText("zcso_products", String.Join(vbCrLf, matdict2.Keys))

    End Sub

    ' Obtain all product texts from GuiXT via GuiXT long text variables
    Public Sub productnames_from_guixt()

        Dim products As String() = myguixt.GetText("r1").Split(vbCrLf)
        Dim productnames As String() = myguixt.GetText("r2").Split(vbCrLf)

        For k As Integer = 0 To products.Length - 1
            matdict3.Add(products(k).Trim, productnames(k).Trim)
        Next


    End Sub

#Region "Event handling"


    'Create new standard order
    Private Sub buttoncreateorder_Click(sender As Object, e As EventArgs) Handles buttoncreateorder.Click
        myguixt.Input("OK:/0,process=zcso_create_order.txt")
    End Sub

    'Create new order, promotion 1
    Private Sub promotion1_Click(sender As Object, e As EventArgs) Handles promotion1.Click
        myguixt.Input("U[PROMOTION]:1;OK:/0,process=zcso_create_order.txt")
    End Sub

    'Create new order, promotion 2 
    Private Sub promotion2_Click(sender As Object, e As EventArgs) Handles promotion2.Click
        myguixt.Input("U[PROMOTION]:2;OK:/0,process=zcso_create_order.txt")
    End Sub

    'Create new order, promotion 3
    Private Sub promotion3_Click(sender As Object, e As EventArgs) Handles promotion3.Click
        myguixt.Input("U[PROMOTION]:3;OK:/0,process=zcso_create_order.txt")
    End Sub

    ' Display customer order
    Private Sub buttondisplayyorder_Click(sender As Object, e As EventArgs) Handles buttondisplayorder.Click
        Dim ordernumber As String = ""
        For Each row In orders.SelectedRows
            ordernumber = row.Cells("ordervbeln").Value()
        Next

        myguixt.SetVariable("zcso_vbeln", ordernumber)
        myguixt.Input("OK:/0,process=zcso_display_order.txt")
    End Sub

    ' Change customer order
    Private Sub buttonchangeorder_Click(sender As Object, e As EventArgs) Handles buttonchangeorder.Click

        Dim ordernumber As String = ""
        For Each row In orders.SelectedRows
            ordernumber = row.Cells("ordervbeln").Value()
        Next
        myguixt.SetVariable("zcso_vbeln", ordernumber)
        myguixt.Input("OK:/0,process=zcso_change_order.txt")
    End Sub

    ' Click on product button to generate chart
    Private Sub matbuttons_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles matbuttons.CellClick

        Dim product As String = ""
        For Each row In matbuttons.SelectedRows
            product = row.Cells("prodnumber").Value()
        Next

        GenerateSalesChart(product)

    End Sub
#End Region


#Region "contacts"

    ' called from GuiXT for each new contact
    Public Sub newcontact()

        Dim k As Integer = contacts.Rows.Add()

        Dim c As System.Windows.Forms.DataGridViewCellCollection = contacts.Rows.Item(k).Cells

        c("contactname").Value = myguixt.GetVariable("zcso_contactname")
        c("contacttitle").Value = myguixt.GetVariable("zcso_contacttitle")
        c("contactphone").Value = myguixt.GetVariable("zcso_contactphone")
        c("contactemail").Value = myguixt.GetVariable("zcso_contactemail")

    End Sub



#End Region

#Region "orders"

    ' transfer order data from GuiXT to datagrid
    Public Sub neworders()

        Dim r1 As String() = myguixt.GetText("r1").Split(vbCrLf)
        Dim r2 As String() = myguixt.GetText("r2").Split(vbCrLf)
        Dim r3 As String() = myguixt.GetText("r3").Split(vbCrLf)
        Dim r4 As String() = myguixt.GetText("r4").Split(vbCrLf)
        Dim r5 As String() = myguixt.GetText("r5").Split(vbCrLf)


        For k As Integer = 0 To r1.Length - 1

            orders.Rows.Add()

            Dim o As System.Windows.Forms.DataGridViewCellCollection = orders.Rows.Item(k).Cells

            o("ordervbeln").Value = CInt(r1(k).Trim)
            o("orderaudat").Value = r2(k).Trim
            o("orderauart").Value = r3(k).Trim
            o("ordernetwr").Value = Decimal.Parse(r4(k).Trim, Globalization.NumberStyles.AllowDecimalPoint).ToString()
            o("orderbstnk").Value = r5(k).Trim

        Next
    End Sub


#End Region


#Region "materials"

    Public Sub materialsales()

        Dim r1 As String() = myguixt.GetText("r1").Split(vbCrLf)
        Dim r2 As String() = myguixt.GetText("r2").Split(vbCrLf)
        Dim r3 As String() = myguixt.GetText("r3").Split(vbCrLf)

        For k As Integer = 0 To r1.Length - 1

            Dim product As String = r1(k).Trim
            Dim year As String = r2(k).Trim.Substring(0, 4)
            Dim netwr As Decimal = Decimal.Parse(r3(k).Trim)


            Dim key As String = year & product

            If matdict1.ContainsKey(key) Then
                matdict1.Item(key) = matdict1.Item(key) + netwr
            Else
                matdict1.Add(key, netwr)
            End If


            ' product only
            If matdict2.ContainsKey(product) Then
                matdict2.Item(product) = matdict2.Item(product) + netwr
            Else
                matdict2.Add(product, netwr)
            End If

        Next




    End Sub



#End Region


#Region "charts"

    Private total_netwr0 As Decimal = 0
    Private total_netwr1 As Decimal = 0
    Private total_netwr2 As Decimal = 0
    Private total_netwr3 As Decimal = 0
    Private total_netwr4 As Decimal = 0

    Public Sub GenerateSalesChart(product As String)

        ' create chart
        Dim Chart1 As New Chart()

        Chart1.Size = New System.Drawing.Size(salesdiagram.Size.Width, salesdiagram.Size.Height)
        Chart1.BackColor = Color.FromArgb(240, 240, 240)

        Dim ChartArea1 As New ChartArea

        ChartArea1.Name = "ChartArea1"
        Chart1.ChartAreas.Add(ChartArea1)

        Dim Series1 As New Series
        Series1.ChartArea = "ChartArea1"
        Series1.Palette = ChartColorPalette.Pastel
        Series1.XValueMember = "Year"
        Series1.YValueMembers = "Sales"
        Chart1.Series.Add(Series1)

        Dim table As New DataTable

        ' Create columns in the DataTable
        table.Columns.Add("Year", GetType(String))
        table.Columns.Add("Sales", GetType(Integer))

        Dim year0 As String = myguixt.GetVariable("zcso_year0")
        Dim year1 As String = myguixt.GetVariable("zcso_year1")
        Dim year2 As String = myguixt.GetVariable("zcso_year2")
        Dim year3 As String = myguixt.GetVariable("zcso_year3")
        Dim year4 As String = myguixt.GetVariable("zcso_year4")

        Dim netwr0 As Decimal = 0
        Dim netwr1 As Decimal = 0
        Dim netwr2 As Decimal = 0
        Dim netwr3 As Decimal = 0
        Dim netwr4 As Decimal = 0

        For Each m In matdict1

            If product = "*" Or product = "***" Or product = m.Key.Substring(4) Then
                Select Case m.Key.Substring(0, 4)

                    Case year0
                        netwr0 += m.Value
                    Case year1
                        netwr1 += m.Value
                    Case year2
                        netwr2 += m.Value
                    Case year3
                        netwr3 += m.Value
                    Case year4
                        netwr4 += m.Value

                End Select
            End If
        Next

        ' Save total values
        If product = "*" Then
            total_netwr0 = netwr0
            total_netwr1 = netwr1
            total_netwr2 = netwr2
            total_netwr3 = netwr3
            total_netwr4 = netwr4


            ' build matbuttons
            Dim total_netwr As Decimal = total_netwr0 + total_netwr1 + total_netwr2 + total_netwr3 + total_netwr4

          

            matbuttons.Rows.Add()

            Dim mb As System.Windows.Forms.DataGridViewCellCollection = matbuttons.Rows.Item(0).Cells

            mb("percentage").Value = "100"
            mb("product").Value = "***"
            mb("prodnumber").Value = "***"
            mb("prodname").Value = "All products"
            mb("netwr").Value = total_netwr

            ' select first row
            matbuttons.ClearSelection()
            matbuttons.Rows.Item(0).Selected = True


            For Each m In matdict2
                Dim k As Integer = matbuttons.Rows.Add()

                mb = matbuttons.Rows.Item(k).Cells


                Dim percentage As Integer
                ' avoid to divide by 0
                If total_netwr = 0 Then
                    percentage = 0
                Else
                    percentage = CInt(m.Value * 100 / total_netwr)
                End If



                mb("percentage").Value = percentage.ToString


                ' Suppress leading 000
                If IsNumeric(m.Key) Then
                    mb("product").Value = CInt(m.Key).ToString
                Else
                    mb("product").Value = m.Key
                End If

                mb("prodnumber").Value = m.Key
                mb("prodname").Value = matdict3.Item(m.Key)

                mb("netwr").Value = m.Value

            Next


            ' sortmatbuttons
            matbuttons.Sort(matbuttons.Columns("netwr"), System.ComponentModel.ListSortDirection.Descending)

        End If


        ' Sales figures for 5 years
        table.Rows.Add(year4, CInt(netwr4))
        table.Rows.Add(year3, CInt(netwr3))
        table.Rows.Add(year2, CInt(netwr2))
        table.Rows.Add(year1, CInt(netwr1))
        table.Rows.Add(year0, CInt(netwr0))

        ' Chart data
        Chart1.DataSource = table


        Dim bmp As New Bitmap(Chart1.Width, Chart1.Height)
        Dim rec As New Rectangle(0, 0, Chart1.Width, Chart1.Height)
        Chart1.DrawToBitmap(bmp, rec)

        salesdiagram.Image = bmp


        ' Title variable
        If product = "*" OrElse product = "***" Then
            product_selected.Text = "All products"
        Else
            product_selected.Text = product & " " & matdict3.Item(product)
        End If

        salesdiagram.Refresh()

    End Sub

  

#End Region

   
End Class