0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

cscの作法 その89

Posted at

概要

cscの作法、調べてみた。
Dictionary使ってみた。

参考にしたページ

写真

image.png

サンプルコード

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

public class Form1 : Form {
	Panel panel1;
	Panel panel2;
	Panel panel3;
	Button button1,
		button2,
		button3;
	Dictionary<Button, Panel> _dic = new Dictionary<Button, Panel>();
	public Form1() {
		this.ClientSize = new Size(500, 500);
		this.Text = "panel";
		panel1 = new Panel();
		panel1.Location = new Point(10, 50);
		panel1.Width = 400;
		panel1.Height = 400;
		panel1.BackColor = Color.Gray;
		panel2 = new Panel();
		panel2.Location = new Point(10, 50);
		panel2.Width = 400;
		panel2.Height = 400;
		panel2.BackColor = Color.Red;
		panel3 = new Panel();
		panel3.Location = new Point(10, 50);
		panel3.Width = 400;
		panel3.Height = 400;
		panel3.BackColor = Color.Blue;
		button1 = new Button();
		button1.Location = new Point(10, 10);
		button1.Size = new Size(100, 30);
		button1.Text = "1";
		button2 = new Button();
		button2.Location = new Point(160, 10);
		button2.Size = new Size(100, 30);
		button2.Text = "2";
		button3 = new Button();
		button3.Location = new Point(310, 10);
		button3.Size = new Size(100, 30);
		button3.Text = "3";
		this.Controls.Add(panel1);
		this.Controls.Add(panel2);
		this.Controls.Add(panel3);
		this.Controls.Add(button1);
		this.Controls.Add(button2);
		this.Controls.Add(button3);
		_dic.Add(this.button1, this.panel1);
		_dic.Add(this.button2, this.panel2);
		_dic.Add(this.button3, this.panel3);
		AddEvent();
		SelectPanel(this.button3);
	}
	private void AddEvent() {
		foreach (KeyValuePair<Button, Panel> pair in _dic)
		{
			Button button = pair.Key;
			button.Click += new System.EventHandler(this.Button_Click);
		}
	}
	private void Button_Click(object sender, EventArgs e) {
		Button button = (Button) sender;
		SelectPanel(button);
	}
	private void SelectPanel(Button selectedButton) {
		foreach (KeyValuePair<Button, Panel> pair in _dic)
		{
			Button button = pair.Key;
			Panel panel = pair.Value;
			if (button.Equals(selectedButton))
			{
				button.BackColor = Color.DarkBlue;
				button.ForeColor = Color.White;
				panel.Visible = true;
			}
			else
			{
				button.BackColor = Color.Gray;
				button.ForeColor = Color.Black;
				panel.Visible = false;
			}
		}
	}
	[STAThread]
	public static void Main() {
		Application.Run(new Form1());
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?