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 1 year has passed since last update.

cscの作法 その427

Posted at

概要

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

写真

image.png

サンプルコード


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;

namespace App
{
	public class RGB: INotifyPropertyChanged {
		private int _R;
		private int _G;
		private int _B;
		public int R {
			get {
				return _R;
			}
			set {
				_R = value;
				NotifyPropertyChanged("R");
			}
		}
		public int G {
			get {
				return _G;
			}
			set {
				_G = value;
				NotifyPropertyChanged("G");
			}
		}
		public int B {
			get {
				return _B;
			}
			set {
				_B = value;
				NotifyPropertyChanged("B");
			}
		}
		public event PropertyChangedEventHandler PropertyChanged = (_, __) => {
		};
		private void NotifyPropertyChanged(string propertyName = "") {
			PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}
	public partial class Form1: Form {
		private Label label1;
		private Label label2;
		private Label label3;
		private TrackBar trackBar1;
		private TrackBar trackBar2;
		private TrackBar trackBar3;
		private NumericUpDown numericUpDown1;
		private NumericUpDown numericUpDown2;
		private NumericUpDown numericUpDown3;
		private BindingSource bindingSource1 = new BindingSource();
		public Form1() {
			InitializeComponent();
			this.numericUpDown1.DataBindings.Add(new Binding("Value", this.bindingSource1, "R", true, DataSourceUpdateMode.OnPropertyChanged));
			this.numericUpDown2.DataBindings.Add(new Binding("Value", this.bindingSource1, "G", true, DataSourceUpdateMode.OnPropertyChanged));
			this.numericUpDown3.DataBindings.Add(new Binding("Value", this.bindingSource1, "B", true, DataSourceUpdateMode.OnPropertyChanged));
			this.trackBar1.DataBindings.Add(new Binding("Value", this.bindingSource1, "R", true, DataSourceUpdateMode.OnPropertyChanged));
			this.trackBar2.DataBindings.Add(new Binding("Value", this.bindingSource1, "G", true, DataSourceUpdateMode.OnPropertyChanged));
			this.trackBar3.DataBindings.Add(new Binding("Value", this.bindingSource1, "B", true, DataSourceUpdateMode.OnPropertyChanged));
			RGB rgb1 = new RGB();
			rgb1.R = 100;
			rgb1.G = 200;
			rgb1.B = 50;
			this.bindingSource1.DataSource = rgb1;
			((RGB) (this.bindingSource1.DataSource)).R = 5;
			((RGB) (this.bindingSource1.DataSource)).G = 205;
			((RGB) (this.bindingSource1.DataSource)).B = 55;
		}
		private void InitializeComponent() {
			this.label1 = new Label();
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(2, 4);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(13, 12);
			this.label1.TabIndex = 0;
			this.label1.Text = "R";
			this.Controls.Add(this.label1);
			this.label2 = new Label();
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(3, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(12, 12);
			this.label2.TabIndex = 1;
			this.label2.Text = "G";
			this.Controls.Add(this.label2);
			this.label3 = new Label();
			this.label3.AutoSize = true;
			this.label3.Location = new System.Drawing.Point(2, 124);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(13, 12);
			this.label3.TabIndex = 2;
			this.label3.Text = "B";
			this.Controls.Add(this.label3);
			this.numericUpDown1 = new NumericUpDown();
			this.numericUpDown1.BorderStyle = BorderStyle.None;
			this.numericUpDown1.DecimalPlaces = 0;
			this.numericUpDown1.Increment = new decimal(new int[] {
				0,
				0,
				0,
				65536
			});
			this.numericUpDown1.Location = new System.Drawing.Point(18, 10);
			this.numericUpDown1.Maximum = new decimal(new int[] {
				256,
				0,
				0,
				0
			});
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.Size = new System.Drawing.Size(46, 15);
			this.numericUpDown1.TabIndex = 3;
			this.numericUpDown1.TextAlign = HorizontalAlignment.Right;
			this.Controls.Add(this.numericUpDown1);

			this.numericUpDown2 = new NumericUpDown();
			this.numericUpDown2.BorderStyle = BorderStyle.None;
			this.numericUpDown2.DecimalPlaces = 0;
			this.numericUpDown2.Increment = new decimal(new int[] {
				0,
				0,
				0,
				65536
			});
			this.numericUpDown2.Location = new System.Drawing.Point(18, 70);
			this.numericUpDown2.Maximum = new decimal(new int[] {
				256,
				0,
				0,
				0
			});
			this.numericUpDown2.Name = "numericUpDown2";
			this.numericUpDown2.Size = new System.Drawing.Size(46, 15);
			this.numericUpDown2.TabIndex = 5;
			this.numericUpDown2.TextAlign = HorizontalAlignment.Right;
			this.Controls.Add(this.numericUpDown2);

			this.numericUpDown3 = new NumericUpDown();
			this.numericUpDown3.BorderStyle = BorderStyle.None;
			this.numericUpDown3.DecimalPlaces = 0;
			this.numericUpDown3.Increment = new decimal(new int[] {
				0,
				0,
				0,
				65536
			});
			this.numericUpDown3.Location = new System.Drawing.Point(18, 130);
			this.numericUpDown3.Maximum = new decimal(new int[] {
				256,
				0,
				0,
				0
			});
			this.numericUpDown3.Name = "numericUpDown3";
			this.numericUpDown3.Size = new System.Drawing.Size(46, 15);
			this.numericUpDown3.TabIndex = 7;
			this.numericUpDown3.TextAlign = HorizontalAlignment.Right;
			this.Controls.Add(this.numericUpDown3);

			this.trackBar1 = new TrackBar();
			//this.trackBar1.Anchor = ((AnchorStyles) ((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
			//this.trackBar1.AutoSize = false;
			this.trackBar1.Location = new System.Drawing.Point(70, 10);
			this.trackBar1.Maximum = 256;
			//this.trackBar1.Name = "trackBar1";
			this.trackBar1.Size = new System.Drawing.Size(218, 19);
			//this.trackBar1.TabIndex = 4;
			this.trackBar1.TabStop = false;
			this.trackBar1.TickStyle = TickStyle.None;
			this.Controls.Add(this.trackBar1);

			this.trackBar2 = new TrackBar();
			//this.trackBar2.Anchor = ((AnchorStyles) ((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
			//this.trackBar2.AutoSize = false;
			this.trackBar2.Location = new System.Drawing.Point(70, 70);
			this.trackBar2.Maximum = 256;
			//this.trackBar2.Name = "trackBar2";
			this.trackBar2.Size = new System.Drawing.Size(218, 19);
			//this.trackBar2.TabIndex = 6;
			this.trackBar2.TabStop = false;
			this.trackBar2.TickStyle = TickStyle.None;
			this.Controls.Add(this.trackBar2);

			this.trackBar3 = new TrackBar();
			//this.trackBar3.Anchor = ((AnchorStyles) ((((AnchorStyles.Top | AnchorStyles.Bottom) | AnchorStyles.Left) | AnchorStyles.Right)));
			//this.trackBar3.AutoSize = false;
			this.trackBar3.Location = new System.Drawing.Point(70, 130);
			this.trackBar3.Maximum = 256;
			//this.trackBar3.Name = "trackBar3";
			this.trackBar3.Size = new System.Drawing.Size(218, 19);
			//this.trackBar3.TabIndex = 8;
			this.trackBar3.TabStop = false;
			this.trackBar3.TickStyle = TickStyle.None;
			this.Controls.Add(this.trackBar3);

			this.ClientSize = new System.Drawing.Size(330, 200);
			this.Text = "RGB";
		}
		[STAThread]
		static void Main() {
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			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?