C#从另一个类声明一个方法

时间:2013-03-12 10:33:52

标签: c# methods

以下是我遇到的问题,尤其是围绕它的问题。我想在打开ImageViewer时调用方法“toolStripButton1_Click()”。就目前而言,当打开ImageViewer时,所选的图像不会随之打开。我已经公开了这个方法,希望能解决这个问题,我没有。

public partial class MediaPlayer : Form
{

    public ImageViewer iv;
    public AudioPlayer ap;
    public VideoPlayer vp;

    public MediaPlayer()
    {
        InitializeComponent();
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (ofdSelectMedia.ShowDialog() == DialogResult.OK)

            switch(ofdSelectMedia.FilterIndex){
                **case 1: new ImageViewer().Show(); public void toolStripButton1_Click(object sender, EventArgs e);** 
                    break;
                case 2: new AudioPlayer().Show();
                    break;
                case 3: new VideoPlayer().Show();
                    break;
       }
    }
}

谢谢。

5 个答案:

答案 0 :(得分:0)

试试这个..

public void toolStripButton1_Click(object sender, EventArgs e)
{
somemethod();
}

void somemethod()
{
 //your code which u want to execute on click of a button
}

在case1中调用方法somemethod();而不是调用事件。

答案 1 :(得分:0)

将toolStripButton1_Click方法中的代码移动到函数中,并从case子句和toolStripButton1_Click处理程序中调用它。

private void DoIt()
{
    // The code from the toolStripButton1_Click handler
}    

private void toolStripButton1_Click(object sender, EventArgs e)
{
    DoIt();
}



private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (ofdSelectMedia.ShowDialog() == DialogResult.OK)

        switch(ofdSelectMedia.FilterIndex){
            case 1: new ImageViewer().Show(); 
                DoIt();
                break;
            case 2: new AudioPlayer().Show();
                break;
            case 3: new VideoPlayer().Show();
                break;
   }
}

答案 2 :(得分:0)

您正在做的是方法声明,而不是方法调用。试试

switch(ofdSelectMedia.FilterIndex){
    case 1: new ImageViewer().Show(); 
        toolStripButton1_Click(sender, e);
        break;
    case 2: new AudioPlayer().Show();
        break;
    case 3: new VideoPlayer().Show();
        break;
}

答案 3 :(得分:0)

使用PerformClick(),如下所示

public partial class MediaPlayer : Form
{

public ImageViewer iv;
public AudioPlayer ap;
public VideoPlayer vp;

public MediaPlayer()
{
    InitializeComponent();
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (ofdSelectMedia.ShowDialog() == DialogResult.OK)

        switch(ofdSelectMedia.FilterIndex){
            case 1: 
                new ImageViewer().Show();
                toolStripButton1.PerformClick();
                break;
            case 2: new AudioPlayer().Show();
                break;
            case 3: new VideoPlayer().Show();
                break;
   }
}

答案 4 :(得分:0)

听起来就像你想要的那样:

case 1:
    var imgViewer = new ImageViewer();
    imgViewer.Show();
    imgViewer.toolStripButton1_Click(sender, e);
    break;

虽然就个人而言,我会重构ImageViewer以使用ShowImage方法或类似方法,因此变为:

case 1:
    var imgViewer = new ImageViewer()
    imgViewer.Show();
    imgViewer.ShowImage();
    break;

并且您的ImageViewer有:

public void ShowImage() {...}

void openToolStripMenuItem_Click(object sender, EventArgs e) {
    ShowImage();
}