代码如下:
Option Explicit
Private p_AppPath As String
Private Sub Form_Load()
' 获得程序运行目录
p_AppPath = App.Path
If Right$(p_AppPath, 1) <> "\" Then p_AppPath = p_AppPath & "\"
End Sub
Private Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
' 建立XML文件
Set xml_document = New DOMDocument
If Text1.Text = "" Then
Set values_node = xml_document.createElement("资源")
Else
Set values_node = xml_document.createElement(Text1.Text)
End If
values_node.appendChild xml_document.createTextNode(vbCrLf)
xml_document.appendChild values_node
CreateNode values_node, Label1.Caption, Text1.Text
CreateNode values_node, Label2.Caption, Text2.Text
CreateNode values_node, Label3.Caption, Text3.Text
CreateNode values_node, Label4.Caption, Text4.Text
CreateNode values_node, Label5.Caption, Text5.Text
CreateNode values_node, Label6.Caption, Text6.Text
CreateNode values_node, Label7.Caption, Text7.Text
CreateNode values_node, Label8.Caption, Text8.Text
CreateNode values_node, Label9.Caption, Text9.Text
CreateNode values_node, Label10.Caption, Text10.Text
CreateNode values_node, Label11.Caption, Text11.Text
CreateNode values_node, Label12.Caption, Text12.Text
CreateNode values_node, Label13.Caption, Text13.Text
CreateNode values_node, Label14.Caption, Text14.Text
CreateNode values_node, Label15.Caption, Text15.Text
CreateNode values_node, Label16.Caption, Text16.Text
CreateNode values_node, Label17.Caption, Text17.Text
CreateNode values_node, Label18.Caption, Text18.Text
CreateNode values_node, Label19.Caption, Text19.Text
CreateNode values_node, Label20.Caption, Text20.Text
' 保存XML文件
xml_document.save p_AppPath & "自建模板.xml"
End Sub
Private Sub CreateNode(ByVal parent As IXMLDOMNode, _
ByVal node_name As String, ByVal node_value As String)Dim new_node As IXMLDOMNode
Set new_node = parent.ownerDocument.createElement(node_name)
new_node.Text = node_value
parent.appendChild new_node
parent.appendChild parent.ownerDocument.createTextNode(vbCrLf)
End SubPrivate Sub Command1_Click()
 Dim a As String
   Dim i As Integer
   i = InputBox("请输入一个整数", "输入对话框")
   a = InputBox("请输入内容", "输入对话框")
   Select Case i
   Case 1
   Label1.Caption = a
   Case 2
   Label2.Caption = a
   Case 3
   Label3.Caption = a
   Case 4
   Label4.Caption = a
   Case 5
   Label5.Caption = a
   Case 6
   Label6.Caption = a
   Case 7
   Label7.Caption = a
   Case 8
   Label8.Caption = a
    Case 9
   Label9.Caption = a
   Case 10
   Label10.Caption = a
   Case 11
   Label11.Caption = a
   Case 12
   Label12.Caption = a
   Case 13
   Label13.Caption = a
   Case 14
   Label14.Caption = a
   Case 15
   Label15.Caption = a
   Case 16
   Label16.Caption = a
     Case 17
   Label17.Caption = a
   Case 18
   Label18.Caption = a
   Case 19
   Label19.Caption = a
   Case 20
   Label20.Caption = a
   
   End Select
End SubPrivate Sub Command2_Click()
frmMain.Show
Unload Me
End SubPrivate Sub Command3_Click()
SaveValues
End Sub
作用就是通过界面输入信息,并保存为.XML文档。我觉得代码有点复杂,能否简化一下。有20个标签和文本框,能用控件数组简化吗?
界面如下: 

解决方案 »

  1.   

    可以,将label2~20全部修改成Label1,把index设置为1~19
    Label1(a - 1).Caption = a
      

  2.   

    在问一下,那Case 语句不要了?
    Label1(a - 1).Caption = a其中a我定义为字符串,那a-1不行吧。
      

  3.   

    写错了
    Label1(i - 1).Caption = a
      

  4.   

    谢谢,下面的搞定了。但上面CreateNode values_node, Label20.Caption, Text20.Text
    那一块好像不行,要用的循环语句吗?能否再帮看看,最好写出代码!谢谢。
      

  5.   

    Private Sub SaveValues()
    Dim xml_document As DOMDocument
    Dim values_node As IXMLDOMNode
    Dim i As Integer
    i = 20
    ' 建立XML文件
    Set xml_document = New DOMDocument
    If Text(i-1).Text = "" Then
    Set values_node = xml_document.createElement("资源")
    Else
    Set values_node = xml_document.createElement(Text(i-1).Text)
    End If
    values_node.appendChild xml_document.createTextNode(vbCrLf)
    xml_document.appendChild values_node
    For i = 1 To 20
    CreateNode values_node, Label(i - 1).Caption, Text(i - 1).Text
    next i
    ' 保存XML文件
    xml_document.save p_AppPath & "自建模板.xml"
    End Sub
    我这些改的,但运行时出现“子程序或函数未定义”?
    大侠能帮改改吗?
      

  6.   

    将textbox也做成数组:
    For i = 0 To 19
        CreateNode values_node, Label1(i).Caption, Text1(i).Text
    Next
      

  7.   


    For i = 0 To Label1.UBound
        CreateNode values_node, Label1(i).Caption, Text1(i).Text
    Next
      

  8.   

    谢谢。搞定了。我知道哪里错了,CreateNode values_node, Label(i).Caption, Text(i).Text
    我label后面少个1.
    再次感谢。