单列

来源:互联网 发布:喜扑传奇翅膀升级数据 编辑:程序博客网 时间:2024/06/05 19:42

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


namespace Day10_0100单例
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            Person p = Person.getInstance(); 
            Person p2 = Person.getInstance();
            MessageBox.Show((p == p2).ToString());
            //让dgv可以整行选中
            dgvList.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
            dgvList.ReadOnly = true;
            //让dgv和右键菜单
            dgvList.ContextMenuStrip = cmsList;
            LoadData();
            BindData();
        }
        private void BindData()
        {
            dgvList.DataSource = list;
        }
        List<Song> list = new List<Song>();
        string leftPath = "C:\\song\\";
        private void LoadData()
        {
            Song song1 = new Song();
            song1.SongName = "不要太早遇到对的人   柠檬";
            song1.SongPath = "不要太早遇到对的人   柠檬.mp3";


            Song song2 = new Song();
            song2.SongName = "妈妈";
            song2.SongPath = "妈妈.mp3";


            Song song3 = new Song();
            song3.SongName = "所谓朋友";
            song3.SongPath = "所谓朋友.mp3";


            list.Add(song1);
            list.Add(song2);
            list.Add(song3);
        }
        private void 播放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string rightPath = dgvList.SelectedRows[0].Cells[1].Value.ToString();
            string fullPath = leftPath + rightPath;
            Play p = Play.getInstance();
            p.path = fullPath;
            p.pp();
            p.Show();
        }
    }
}



using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Day10_0100单例
{
    public partial class Play : Form
    {
        private Play()
        {
            InitializeComponent();
        }
        private static Play play=new Play();
        public static Play getInstance()
        {
            return play;
        }
        public string path;
        private void Play_Load(object sender, EventArgs e)
        {
        }
        public void pp()
        {
            Player1.URL = path;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Day10_0100单例
{
    public class Song
    {
        public string SongName { get; set; }
        public string SongPath { get; set; }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Day10_0100单例
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmMain());
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Day10_0100单例
{
    public class Person
    {
        //1.构造私有
        private Person()
        {
        }
        //饿汉模式  单例
      //  static Person p = new Person();
        private static Person p;//懒汉模式
        //2.共有方法
        public static Person getInstance()
        {
            if (p==null)
            {
                p=new Person();
            }
            return p;
        }






    }
}

1 0