应该是doc.Variables.FormVariables.Item("变量0").Value = "123";这一段没有“变量0”这个Item,但是在Codesoft中设置的是“变量0”。
直接上代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LabelManager2;
namespace PrintTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btPrint_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtBarcode.Text))
{
LabelManager2.ApplicationClass lbl = new LabelManager2.ApplicationClass();
string labFileName = System.Windows.Forms.Application.StartupPath + @"\Document2.lab";
try
{
if (!File.Exists(labFileName))
{
MessageBox.Show("沒有找到標簽模板文件:LotPrint.Lab,請聯系系統管理員", "溫馨提示");
return;
}
lbl.Documents.Open(labFileName, false);// 调用设计好的label文件
LabelManager2.Document doc = lbl.ActiveDocument;
doc.Variables.FormVariables.Item("变量0").Value = "123";
//doc.PrintDocument(1);
}
catch (Exception ex)
{
MessageBox.Show("出錯啦,原因如下:\n\r" + ex.Message, "出錯啦");
}
}
}
}
}