-1

以下是我遇到的问题,特别是部分有 ** 。我想在打开 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;
       }
    }
}

谢谢你。

4

5 回答 5

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();而不是调用事件。

于 2013-03-12T10:39:33.123 回答
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;
   }
}
于 2013-03-12T10:39:55.920 回答
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;
}
于 2013-03-12T10:42:06.613 回答
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;
   }
}
于 2013-03-12T10:42:24.377 回答
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();
}
于 2013-03-12T10:43:38.153 回答