VB如何将多维数组传入和传出Active X控件

时间:2013-05-01 08:48:05

标签: arrays vb.net activex

我有一个名为OPIMG(2000,2000,10)的多维数组,它包含我在主窗体上创建的图像数据集(16位灰度图像作为用户控件中使用的数据)。我需要能够从活动X中访问这些数据,但无法将数组放入控件中。

我尝试在控件中创建一个新数组,并按值复制数据,但这需要花费太多时间,所以我需要找到一种能够从用户控件直接访问公共数组的方法。 / p>

1 个答案:

答案 0 :(得分:0)

我同意盛强的说法,因为它有很多记忆可以传递给

2000 x 2000 x 10 = 40,000,000 
Approximately 40Mb

然而,为了回答你的问题,我已经粘贴了页面和用户控件的代码。

PAGE CODE

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Src="~/MyUserControl.ascx" TagName="MyUserCtrlItem" TagPrefix="UC1" %>
<script runat="server">

Private _MyArray(,,) As Long
'
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
'
Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim Number As Long
  '
  ' Populate array with test data
  ReDim _MyArray(2000, 2000, 10)
  For Z = 0 To 9
    For Y = 0 To 199
      For X = 0 To 199
        Number = Number + 1
        _MyArray(X, Y, Z) = Number
      Next
    Next
  Next
  MyUserCtrlItem1.MyCtrlArray = _MyArray
  '
End Sub
'
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <UC1:MyUserCtrlItem ID="MyUserCtrlItem1" runat="server" />
  </form>
</body>
</html>

用户控件的代码

USER CONTROL

<%@ Control Language="VB" ClassName="MyUserControl" %>

<script runat="server">

Private _MyCtrlArray(,,) As Long

Property MyCtrlArray As Array
  Get
    Return _MyCtrlArray
  End Get
  Set(value As Array)
    _MyCtrlArray = value
  End Set
End Property

Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
  If TextBox1.Text <> "" Then
    If TextBox2.Text <> "" Then
      If TextBox3.Text <> "" Then
        Label1.Text = _MyCtrlArray(CInt(TextBox1.Text), CInt(TextBox2.Text), CInt(TextBox3.Text))
      Else
        Label1.Text = "Z value missing/invalid!"
      End If
    Else
      Label1.Text = "Y value missing/invalid!"
    End If
  Else
    Label1.Text = "X value missing/invalid!"
  End If
  TextBox1.Text = ""
  TextBox2.Text = ""
  TextBox3.Text = ""
End Sub

</script>

<!-- HTML -->
X Value: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
Y Value: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
Z Value: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />
Data: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

在示例代码中,输入X Y和Z的值,它将返回NUMBER元素中保存的VALUE(我刚刚在页面加载事件中递增)