cscの作法 その89
2959 ワード
概要
cscの作法、調べてみた。
Dictionary使ってみた。
参考にしたページ
写真
サンプルコード
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());
}
}
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());
}
}
以上。
Author And Source
この問題について(cscの作法 その89), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/5e15653f52c8703141e3著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .