如何访问其他形式的列表视图

本文关键字:列表 视图 其他 何访问 访问 | 更新日期: 2023-09-27 18:01:36

我正在开发一个带有人脸检测功能的mp3播放器。我有两种形式。

第一种形式是媒体播放器和人脸检测代码,第二种形式是用于创建播放列表的代码。

我想,当我点击创建按钮的播放列表的名称应显示在列表框的第一种形式。

这是我的一些form1

代码
 private Capture cap;
  public Form1()
    {
        InitializeComponent();
        cap = new Capture(0);
        _dataBasePath = Directory.GetCurrentDirectory() + @"'db1.mdb";

        // adjust path to find your xml
        haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        mouth = new HaarCascade("Mouth.xml");
        lefteye = new HaarCascade("eye_left.xml");
        righteye = new HaarCascade("haarcascade_righteye_2splits.xml");
    }
    public void DataBasePath(string path)
    {
        _dataBasePath = path;
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
        {
            if (nextFrame != null)
            {
                // there's only one channel (greyscale), hence the zero index
                //var faces = nextFrame.DetectHaarCascade(haar)[0];
                Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
                Image<Gray, Byte> gray = nextFrame.Convert<Gray, Byte>();
                Image<Gray, Byte> gray1 = nextFrame.Convert<Gray, Byte>();
                Image<Gray, Byte> gray2 = nextFrame.Convert<Gray, Byte>();
                var faces = grayframe.DetectHaarCascade(
                         haar, 1.4, 4,
                         HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                         new Size(nextFrame.Width / 8, nextFrame.Height / 8)
                         )[0];
                foreach (MCvAvgComp f in faces)
                {
                    //draw the face detected in the 0th (gray) channel with blue color
                    nextFrame.Draw(f.rect, new Bgr(Color.Blue), 2);
                    facesnap = f.rect;

                    int halfheight = facesnap.Height/2;
                    int start = facesnap.X;
                    int start1 = facesnap.Y;
                    Rectangle top = new Rectangle(start,start1,facesnap.Width,halfheight);
                    int start2 = top.Bottom;
                    Rectangle bottom = new Rectangle(start, start2, facesnap.Width, halfheight);

                    //Set the region of interest on the faces
                    gray.ROI = bottom;
                    MCvAvgComp[][] mouthsDetected = gray.DetectHaarCascade(mouth,
                                                    1.1, 10,
                                                  Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                                    new Size(40,30));
                    gray.ROI = Rectangle.Empty;

                    foreach (MCvAvgComp m in mouthsDetected[0])
                    {
                        Rectangle mouthRect = m.rect;
                        mouthRect.Offset(bottom.X, bottom.Y);
                        nextFrame.Draw(mouthRect, new Bgr(Color.Red), 2);
                        Rectangle mouthphoto = new Rectangle(mouthRect.X - 5, mouthRect.Y - 10, mouthRect.Width + 5, mouthRect.Height +10);
                        detectedmouth = mouthphoto;
                    }
                    int halfwidth =facesnap.Width/2;
                    Rectangle toprighteye =new Rectangle(start,start1,halfwidth,halfheight);
                    int leftx = toprighteye.Right;
                    Rectangle toplefteye = new Rectangle(leftx, start1, halfwidth, halfheight);
                    gray1.ROI =toplefteye;
                    MCvAvgComp[][] detectedlefteye = gray1.DetectHaarCascade(lefteye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30,10));
                    gray1.ROI = Rectangle.Empty;
                    foreach (MCvAvgComp eyesnapleft in detectedlefteye[0])
                    {
                        Rectangle eyeRectleft = eyesnapleft.rect;
                        eyeRectleft.Offset(toplefteye.X, toplefteye.Y);
                        nextFrame.Draw(eyeRectleft, new Bgr(Color.Green), 2);
                        lefteyesnap = eyeRectleft;

                    }
                    gray2.ROI = toprighteye;
                    MCvAvgComp[][] detectedrighteye = gray2.DetectHaarCascade(righteye, 1.1, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(30, 10));
                    gray2.ROI = Rectangle.Empty;
                    foreach (MCvAvgComp eyesnapright in detectedrighteye[0])
                    {
                        Rectangle eyeRectright = eyesnapright.rect;
                        eyeRectright.Offset(toprighteye.X, toprighteye.Y);
                        nextFrame.Draw(eyeRectright, new Bgr(Color.Yellow), 2);
                        righteyesnap = eyeRectright;

                    }


                }

                pictureBox1.Image = nextFrame.ToBitmap();
            }
        }
    }
 private void Form1_Load(object sender, EventArgs e)
    {
        _dataBasePath = Directory.GetCurrentDirectory() + @"'db1.mdb";

        // adjust path to find your xml
        haar = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
        mouth = new HaarCascade("Mouth.xml");
        lefteye = new HaarCascade("eye_left.xml");
        righteye = new HaarCascade("haarcascade_righteye_2splits.xml");
    }

这是form2

的一些代码
namespace Face_Detection_Concept
{
    public partial class Form2 : Form
{
    #region Global Variables
    public const string Separator = ",";
    public static ArrayList searchAudio = new ArrayList();
    public static ArrayList searchVideo = new ArrayList();

    public static StreamWriter sw;
    public string fnm;
    #endregion
    public Form2()
    {
        InitializeComponent();
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        // Set default values
        cmbType.SelectedIndex = 0;
        chkIncSubDir.Checked = true;
        LoadFormats();

        txtFilename.Text = "PlayList1.wpl";
    }

当我使用这个代码访问form1的listbox on button click event

new Form1().playlistviewbar.items.add(playlistname);

,但这并没有显示播放列表的名称。有解决办法吗?

如何访问其他形式的列表视图

虽然可以通过在public中创建直接公开控件,但更好的方法是创建操作ListBox内容的方法。这使您可以灵活地更改控件,而不会破坏调用表单:

public void AddPlayList(string playlistname)
{
    this.playlistviewbar.Items.Add(playlistname);
}
public string[] GetPlayLists()
{
    return this.playlistviewbar.Items;  // converting to strings if necessary.
}

将ListBox的modifiers属性设置为public,您可以通过已知的方式访问它。

将调用表单的引用传递给被调用表单,并带它访问LB。

// Call:
Form2 test = new Form2(this);
//Constructor Form2:
public Form2 (Form1 FormToAccess)
//Access:
FormToAccess.listBox1.... = ... ;

你的Capture对象cap是空的,你应该在开始计时器之前初始化它,例如:

public void form_load()
    {
        cap = new Capture();
        //Init and start Timer
    }