首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#中多个Form之间调用的有关问题,求大神们指导一上啊

2012-09-16 
C#中多个Form之间调用的问题,求大神们指导一下啊。。。我这里有两个Form1和Formht我在Formht中写了一个Report

C#中多个Form之间调用的问题,求大神们指导一下啊。。。
我这里有两个Form1和Formht我在Formht中写了一个Report类并创建了一个Report实例report,然后我想在Form1中也能调用这个实例。。我在Form1中添加了一个Button,在Button中应该写什么代码额。。求大神指导。。。。
[code=C#][/code]public partial class Form1 : Form 
  {
  class Report
  {
  private _Application wordApp = null;
  private _Document wordDoc = null;
  public _Application Application
  {
  get
  {
  return wordApp;
  }
  set
  {
  wordApp = value;
  }
  }
  public _Document Document
  {
  get
  {
  return wordDoc;
  }
  set
  {
  wordDoc = value;
  }
  }
  // 杀掉winword.exe进程
  public void killWinWordProcess()
  {
  System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
  foreach (System.Diagnostics.Process process in processes)
  {
  bool b = process.MainWindowTitle == "";
  if (process.MainWindowTitle == "")
  {
  process.Kill();
  }
  }
  }
  }

  public Form1()
  {
  InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {

  }

  private void 借据合同ToolStripMenuItem_Click(object sender, EventArgs e)
  {
   
  //弹出一个新的窗口,显示合同所需填写的部分
  Formht f = null;
  try
  {
  f = new Formht();
  f.ShowDialog(this);
  }
  catch (Exception exp)
  {
  MessageBox.Show(exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }


  }

  private void button3_Click(object sender, EventArgs e)
  {
  // string RepPath = @"F:\学习\soarto要求\offce小应用";
  // Report report = new Report();  
  // report.SaveDocument(RepPath);
  }
[code=C#][/code]
  public partial class Formht : Form
  {
  class Report
  {
  private _Application wordApp = null;
  private _Document wordDoc = null;
  public _Application Application
  {
  get
  {
  return wordApp;
  }
  set
  {
  wordApp = value;
  }
  }
  public _Document Document
  {
  get
  {
  return wordDoc;
  }
  set
  {
  wordDoc = value;
  }
  }



  //通过模板创建新文档
  public void CreateNewDocument(string filePath)
  {
  killWinWordProcess();
  wordApp = new ApplicationClass();
  wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
  wordApp.Visible = false;
  object missing = System.Reflection.Missing.Value;
  object templateName = filePath;
  wordDoc = wordApp.Documents.Open(ref templateName, ref missing,
  ref missing, ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref missing);
  }

  //保存新文件
  public void SaveDocument(string filePath)
  {
  object fileName = filePath;
  object format = WdSaveFormat.wdFormatDocument;//保存格式
  object miss = System.Reflection.Missing.Value;
  wordDoc.SaveAs(ref fileName, ref format, ref miss,
  ref miss, ref miss, ref miss, ref miss,
  ref miss, ref miss, ref miss, ref miss,
  ref miss, ref miss, ref miss, ref miss,
  ref miss);
  //关闭wordDoc,wordApp对象
  object SaveChanges = WdSaveOptions.wdSaveChanges;
  object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
  object RouteDocument = false;
  wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
  wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
  }

  //在书签处插入值
  public bool InsertValue(string bookmark, string value)
  {
  object bkObj = bookmark;
  if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
  {
  wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
  wordApp.Selection.TypeText(value);
  return true;
  }
  return false;
  }

  //插入图片
  public void InsertPicture(string bookmark, string picturePath, float width, float hight)
  {
  object miss = System.Reflection.Missing.Value;
  object oStart = bookmark;
  Object linkToFile = false; //图片是否为外部链接
  Object saveWithDocument = true; //图片是否随文档一起保存 
  object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//图片插入位置
  wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
  wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width; //设置图片宽度
  wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight; //设置图片高度
  }

  //插入一段文字,text为文字内容
  public void InsertText(string bookmark, string text)
  {
  object oStart = bookmark;
  object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
  Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range);
  wp.Format.SpaceBefore = 6;
  wp.Range.Text = text;
  wp.Format.SpaceAfter = 24;


  wp.Range.InsertParagraphAfter();
  wordDoc.Paragraphs.Last.Range.Text = " ";
  }

  // 杀掉winword.exe进程
  public void killWinWordProcess()
  {
  System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
  foreach (System.Diagnostics.Process process in processes)
  {
  bool b = process.MainWindowTitle == "";
  if (process.MainWindowTitle == "")
  {
  process.Kill();
  }
  }
  }
  }
  public Formht()
  {
  InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  { //打开借据合同并向合同中添加信息
  string TemPath = @"F:\学习\soarto要求\offce小应用\word模版\借据.docx";
  Report report = new Report();  
  report.CreateNewDocument(TemPath); //模板路径


  //关闭当前窗口
  this.Close();
  }

[解决办法]
form1中发布一个属性就好了
public Report YourReport
{
{get;set;}
}
,或者增加带参的构造函数

http://topic.csdn.net/u/20120710/14/c88a8f3c-c096-4f93-a0ee-b82b810ebd9d.html
[解决办法]
一楼正解 写个类的属性 get set 就好了

热点排行