如何在C#中从一种表单获取数据到另一种表单#

问题描述:

我的功能是从选定的文件夹中加载图片。现在我想通过以新形式打开ZoomLogo来缩放此图片。在这种新形式中,我想从我的主窗体中'获取'fullPath1,然后使用ZoomLogo窗体中的此路径加载图片。如何做到这一点?如何在C#中从一种表单获取数据到另一种表单#

void Picture() 
{ 

...

if (DataBaseSelection.SelectedIndex+1==1) 
    { 
    Logo1_pictureBox.Image=new Bitmap(@"Logos\\aa.bmp"); 
    var file1 = Path.ChangeExtension(Printer2_TextBox.Text, ".jpg"); 
    var fullPath1 = Path.Combine(@"Documents\\Base\\", file1); 
    if (!File.Exists(fullPath1)) 
    { 
    MessageBox.Show("No picture!"); 
    } 
    else 
    { 
     Logo_pictureBox.Image = new Bitmap(fullPath1); 
    } 

...

} 

打开新的形式:

void ZoomPictureBoxClick(object sender, EventArgs e) 
    { 
     ZoomSchematic settings = new ZoomSchematic(); 
     settings.ShowDialog();   
    } 

我试图用类似的东西在我的主要形式:

void ZoomPictureBoxClick(object sender, EventArgs e) 
    { 
     ZoomSchematic settings = new ZoomSchematic(this.fullPath1); 
     settings.ShowDialog();   
    } 

,但我不知道如何从功能图像()这个变量..

+0

您是否需要从ZoomSchematic对话框中获取指定的设置? – 2011-04-16 09:37:20

+0

我需要的是fullPath1,所以我可以使用此位置路径显示图片。 ZoomSchematic对话框只包含1个图片框。 – Elfoc 2011-04-16 09:39:37

只需使fullPath1成为您主表单的成员字段即可。

class MainForm 
{ 
    private string fullPath1; 

    void Picture() 
    { 
     if (DataBaseSelection.SelectedIndex+1==1) 
     { 
      Logo1_pictureBox.Image=new Bitmap(@"Logos\\aa.bmp"); 
      var file1 = Path.ChangeExtension(Printer2_TextBox.Text, ".jpg"); 
      fullPath1 = Path.Combine(@"Documents\\Base\\", file1); 
      if (!File.Exists(fullPath1)) 
      { 
       MessageBox.Show("No picture!"); 
      } 
      else 
      { 
       Logo_pictureBox.Image = new Bitmap(fullPath1); 
      } 
     } 
     } 

    void ZoomPictureBoxClick(object sender, EventArgs e) 
    { 
     ZoomSchematic settings = new ZoomSchematic(this.fullPath1); 
     settings.ShowDialog();   
    } 
} 

class ZoomSchematic 
{ 
    string _fullPath1; 

    public ZoomSchematic(string fullPath1) 
    { 
     _fullPath1 = fullPath1; 
    } 
} 
+0

我得到错误:错误CS1729:'App.ZoomSchematic'不包含一个构造函数,需要1个参数 – Elfoc 2011-04-16 09:51:57

+0

请参阅上面的编辑。 – briantyler 2011-04-16 09:56:55

+0

这是放这个类的正确位置吗?:namespace App { \t class ZoomSchematic {...} \t public partial class MainForm:Form {..} cause i'm getting error:The item“obj \ x86 \ Release \ App.ZoomSchematic.resources“在”资源“参数中指定了多次。 “资源”参数不支持重复的项目。 – Elfoc 2011-04-16 10:06:20

如果你究竟要放大,为什么不将图片发送,而不是重新加载图片?例如,

void ZoomPictureBoxClick(Bitmap zoomthis) 
{ 
... 
} 

,因为这将是你的形式还是内,例如

ZoomSchematic settings = new ZoomSchematic(Logo_pictureBox.Image) 

这取决于你想用它做一次那里,如果你只是要显示的设置是什么一点点,我想这取决于你为什么需要这条路。

一个大的答案的位,但我觉得这个问题是开放的更多的问题。

如果你需要稍后发送的路径,那么当你创建你的位图时,你可以/应该保持路径。而不是局部变量。

+0

ZoomSchematic设置=新ZoomSchematic(Logo_pictureBox.Image); settings.ShowDialog();和我gettin错误:错误CS1729:'App.ZoomSchematic'不包含带1个参数的构造函数。 ZoomSchematic由我顺便创建。 – Elfoc 2011-04-16 09:56:25

+0

好吧,那么你需要创建一个允许参数的构造函数,这就是为什么它的抱怨 – BugFinder 2011-04-16 09:58:45

+0

该死的..非常多的学习..:) – Elfoc 2011-04-16 10:08:57

使用形式的构造函数传递文件路径

Form2 form2 = new Form2(string path); 
Form2.ShowDialog(); 

要看是什么Picture()不和它叫什么......如果画面提供了多张图片到你的主要形式,你将不得不排序哪些是有问题的图片...

如果只有一张图片,你可以简单地一个新的私人字符串成员fullpath1添加到您的形式,将不得不每次Picture()设置叫...