Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Combobox如何使所选项目显示主题_C#_Winforms - Fatal编程技术网

C# Combobox如何使所选项目显示主题

C# Combobox如何使所选项目显示主题,c#,winforms,C#,Winforms,我正在做一个tic-tac-toe游戏,我正在尝试添加一个组合框,该组合框将根据用户选择的内容更改应用程序的背景。现在我有夏天、春天、秋天、冬天,图像都在bin/debug文件夹中。我怎么才能让它工作呢?我不知道从哪里开始,教程有点混乱。你能帮我一下吗?有很多方法可以做到这一点。这可能是最简单的: 将主窗体的BackgroundImageLayout设置为Stretch 在窗体上放置4个PictureBox控件,并将其可见属性设置为false。将它们命名为pbWinter,pbSpring等。通

我正在做一个tic-tac-toe游戏,我正在尝试添加一个组合框,该组合框将根据用户选择的内容更改应用程序的背景。现在我有夏天、春天、秋天、冬天,图像都在bin/debug文件夹中。我怎么才能让它工作呢?我不知道从哪里开始,教程有点混乱。你能帮我一下吗?有很多方法可以做到这一点。这可能是最简单的:

  • 将主窗体的
    BackgroundImageLayout
    设置为
    Stretch
  • 在窗体上放置4个
    PictureBox
    控件,并将其
    可见属性设置为
    false
    。将它们命名为
    pbWinter
    pbSpring
    等。通过浏览每个季节的图像文件,设置每个季节的
    Image
    属性
  • 在表单中添加一个组合框。添加项目“冬季”、“春季”、“夏季”和“秋季”
  • 在组合框的
    SelectedIndexChanged
    事件处理程序中,使用
    switch
    语句检查框的
    Text
    属性,并使用如下代码设置相应的返回图像:

    this.BackgroundImage=pbWinter.Image;//等等……

  • 更新:以下是如何执行switch语句:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (comboBox1.Text)
        {
            case "Winter":
                this.BackgroundImage = pbWinter.Image;
                break;
            case "Spring":
                this.BackgroundImage = pbSpring.Image;
                break;
            // etc...
        }
    }
    

    有很多方法可以做到这一点。这可能是最简单的:

  • 将主窗体的
    BackgroundImageLayout
    设置为
    Stretch
  • 在窗体上放置4个
    PictureBox
    控件,并将其
    可见属性设置为
    false
    。将它们命名为
    pbWinter
    pbSpring
    等。通过浏览每个季节的图像文件,设置每个季节的
    Image
    属性
  • 在表单中添加一个组合框。添加项目“冬季”、“春季”、“夏季”和“秋季”
  • 在组合框的
    SelectedIndexChanged
    事件处理程序中,使用
    switch
    语句检查框的
    Text
    属性,并使用如下代码设置相应的返回图像:

    this.BackgroundImage=pbWinter.Image;//等等……

  • 更新:以下是如何执行switch语句:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (comboBox1.Text)
        {
            case "Winter":
                this.BackgroundImage = pbWinter.Image;
                break;
            case "Spring":
                this.BackgroundImage = pbSpring.Image;
                break;
            // etc...
        }
    }
    

    你问的问题并不十分清楚。假设在bin\Debug文件夹中有名为“spring.png”等的位图文件,这应该可以:

    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            comboBox1.Items.AddRange(new string[] { "Spring", "Summer", "Fall", "Winter" });
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            string folder = Application.StartupPath;
            string theme = (string)comboBox1.Items[comboBox1.SelectedIndex];
            string path = System.IO.Path.Combine(folder, theme + ".png");
            Image newImage = new Bitmap(path);
            if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
            this.BackgroundImage = newImage;
        }
    }
    

    你问的问题并不十分清楚。假设在bin\Debug文件夹中有名为“spring.png”等的位图文件,这应该可以:

    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            comboBox1.Items.AddRange(new string[] { "Spring", "Summer", "Fall", "Winter" });
            comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            string folder = Application.StartupPath;
            string theme = (string)comboBox1.Items[comboBox1.SelectedIndex];
            string path = System.IO.Path.Combine(folder, theme + ".png");
            Image newImage = new Bitmap(path);
            if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
            this.BackgroundImage = newImage;
        }
    }
    

    你到底需要帮助哪些部分?更改图像、处理combobox SelectedIndexChanged事件或其他事情?我可以找出我需要帮助的图像是我在combo box属性集合中的combo box“summer”“winter”如何使其在选中时执行操作?确切地说,您需要帮助哪些部分?更改图像、处理combobox SelectedIndexChanged事件或其他事情?我可以找出图像我需要帮助的是combo box属性集合中的combo box“summer”“winter”如何使其在选中时执行操作?