C#将Variable传递给类,然后以表单形式返回类输出

时间:2014-04-24 19:21:26

标签: c# class

我正忙着解决如何在课堂之间来回传递数据的问题。我参加了一些大学课程OOP入门,初学C ++,初学C#,并经历了几个自定进度的在线教程。我相信我正在抓住何时使用课程而不是如何使用课程。所以我正在尝试做什么,我只需要一些指导如何实现这一目标。请不要告诉我谷歌,因为我一直在搜索,直到我的手指流血(确实不是字面意思),并且在这一点上需要更多的东西,比如特定的术语。如果有人能用C#术语向我解释,那么它会有所帮助,因为我会有一些特定的东西可以重新开始搜索。

所以这就是我所在的地方......

我编写了一个应用程序,将所有内容放入同一个类(Form1)并使其正常工作。我正在使用下拉列表中的值来执行数据库查询并分配一些我用来播放视频的变量。

继承我在Form1中的完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Tests
{
public partial class Tests : Form
{
    public Tests()
    {
        InitializeComponent();


        using (var conn = new SqlCeConnection("Data Source=TestDB.sdf;Password=;Persist Security Info=True"))
        {
            conn.Open();
            var comm = new SqlCeCommand("SELECT DISTINCT ColumnOne FROM main", conn);                
            SqlCeDataReader reader = comm.ExecuteReader();

            while (reader.Read())
                columnOneComboBox.Items.Add(reader["ColumnOne"]);
        }
    }

    private void columnOneComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        testsListBox.Items.Clear();
        testVideoMediaPlayer.Visible = false;

        var columnOne = columnOneComboBox.SelectedItem.ToString();
        using (var conn = new SqlCeConnection("Data Source=TestDB.sdf;Password=;Persist Security Info=True"))
        {
            conn.Open();
            var comm = new SqlCeCommand("SELECT * FROM main WHERE ColumnOne='" + columnOne + "' ORDER BY ColumnTwo", conn);
            SqlCeDataReader reader = comm.ExecuteReader();

            while (reader.Read())
                testsListBox.Items.Add(reader["ColumnTwo"]);
        }
    }

    private void TestListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        var columnTwo = testsListBox.SelectedItem.ToString();
        using (var conn = new SqlCeConnection("Data Source=TestDB.sdf;Password=Tr@1n!ng;Persist Security Info=True"))
        {
            conn.Open();
            var comm = new SqlCeCommand("SELECT * FROM main WHERE ColumnTwo='" + columnTwo + "'", conn);
            SqlCeDataReader reader = comm.ExecuteReader();

            if (reader.Read())
            {
                bodyPositionRichTextBox.Text = reader["ColumnThree"].ToString();
                techniqueRichTextBox.Text = reader["ColumnFour"].ToString();
                string videoPath = reader["Video"].ToString();

                if (videoPath != "")
                {
                    testVideoMediaPlayer.URL = @"Videos/" + videoPath;
                    testVideoMediaPlayer.settings.playCount = 1000;
                    testVideoMediaPlayer.uiMode = "none";
                    testVideoMediaPlayer.Visible = true;
                    testVideoMediaPlayer.stretchToFit = true;
                    testVideoMediaPlayer.fullScreen = false;
                }
                else
                {
                    testVideoMediaPlayer.Visible = false;
                }
            }
        }
    }
}
}

现在,我正在考虑做的是创建一个名为MediaPlayer的单独类,但我需要在MediaPlayer类中使用Form1中的变量,其中指定了所有视频控件,然后让From1调用该播放器。以下是我想要转移到MediaPlayer.cs的内容:

                if (videoPath != "")
                {
                    testVideoMediaPlayer.URL = @"Videos/" + videoPath;
                    testVideoMediaPlayer.settings.playCount = 1000;
                    testVideoMediaPlayer.uiMode = "none";
                    testVideoMediaPlayer.Visible = true;
                    testVideoMediaPlayer.stretchToFit = true;
                    testVideoMediaPlayer.fullScreen = false;
                }
                else
                {
                    testVideoMediaPlayer.Visible = false;
                }

我是以错误的方式来做这件事的吗?我没有得到如何将字符串变量“Videos”从Test.cs传递给MediaPlayer.cs或如何从Test.cs调用MediaPlayer.cs。感谢您提供的任何指导。

1 个答案:

答案 0 :(得分:0)

你的类MediaPlayer是这样的(替换你的组件类型):

public class MediaPlayer
{
    /* your video type */ TestVideoMediaPlayer { get; set; }

    public MediaPlayer(/* your video type */ testVideoMediaPlayer)
    {
        TestVideoMediaPlayer = testVideoMediaPlayer;
    }

    public void SetMediaPlayer(string url, int playerCount, string uiMode, bool visible, bool stretch, bool fullScreen)
    {
         if (url != "")
            {
                TestVideoMediaPlayer.URL = @"Videos/" + url;
                TestVideoMediaPlayer.settings.playCount = playerCount;
                TestVideoMediaPlayer.uiMode = uiMode;
                TestVideoMediaPlayer.Visible = visible;
                TestVideoMediaPlayer.stretchToFit = stretch;
                TestVideoMediaPlayer.fullScreen = fullScreen;
            }
            else
            {
                TestVideoMediaPlayer.Visible = false;
            }
    }
}

用你的电话代替:

 if (reader.Read())
 {
      bodyPositionRichTextBox.Text = reader["ColumnThree"].ToString();
      techniqueRichTextBox.Text = reader["ColumnFour"].ToString();
      string videoPath = reader["Video"].ToString();

      MediaPlayer mp = new MediaPlayer(testVideoMediaPlayer);
      mp.SetMediaPlayer(videoPath, 1000, "none", true, true, false);
 }