|
そうですか。黒しか残りませんか。。。
こちらで確認のとれたソースをUpするので、これで正常に動作するようならご自分のソースと見比べてみて下さい。
新規プロジェクトを作成して、Form1のソースを以下で置き換えればOKです。
public partial class Form1 : Form
{
private Color BLACK = Color.Black;
private Color BLUE = Color.Blue;
private Color RED = Color.Red;
private System.Windows.Forms.RichTextBox RichTextBox1;
private System.Windows.Forms.Button btnColor;
private System.Windows.Forms.Button btnRtf;
private System.Windows.Forms.RadioButton radioBlack;
private System.Windows.Forms.RadioButton radioBlue;
private System.Windows.Forms.RadioButton radioRed;
public Form1()
{
InitializeComponent();
Initialize();
this.RichTextBox1.GotFocus += new EventHandler(RichTextBox1_GotFocus);
this.radioBlack.Click += new EventHandler(radioBlack_Click);
this.radioBlue.Click += new EventHandler(radioBlue_Click);
this.radioRed.Click += new EventHandler(radioRed_Click);
this.btnRtf.Click += new EventHandler(buttonRtf_Click);
this.btnColor.Click += new EventHandler(btnColor_Click);
}
private void Initialize()
{
this.RichTextBox1 = new System.Windows.Forms.RichTextBox();
this.btnColor = new System.Windows.Forms.Button();
this.btnRtf = new System.Windows.Forms.Button();
this.radioBlack = new System.Windows.Forms.RadioButton();
this.radioBlue = new System.Windows.Forms.RadioButton();
this.radioRed = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// RichTextBox1
//
this.RichTextBox1.Location = new System.Drawing.Point(13, 13);
this.RichTextBox1.Name = "RichTextBox1";
this.RichTextBox1.Size = new System.Drawing.Size(424, 248);
this.RichTextBox1.TabIndex = 0;
this.RichTextBox1.Text = "";
//
// btnColor
//
this.btnColor.Location = new System.Drawing.Point(235, 274);
this.btnColor.Name = "btnColor";
this.btnColor.Size = new System.Drawing.Size(75, 23);
this.btnColor.TabIndex = 1;
this.btnColor.Text = "色判定";
this.btnColor.UseVisualStyleBackColor = true;
//
// btnRtf
//
this.btnRtf.Location = new System.Drawing.Point(154, 274);
this.btnRtf.Name = "btnRtf";
this.btnRtf.Size = new System.Drawing.Size(75, 23);
this.btnRtf.TabIndex = 4;
this.btnRtf.Text = "RTF出力";
this.btnRtf.UseVisualStyleBackColor = true;
//
// radioBlack
//
this.radioBlack.Appearance = System.Windows.Forms.Appearance.Button;
this.radioBlack.AutoSize = true;
this.radioBlack.Location = new System.Drawing.Point(13, 274);
this.radioBlack.Name = "radioBlack";
this.radioBlack.Size = new System.Drawing.Size(27, 22);
this.radioBlack.TabIndex = 5;
this.radioBlack.TabStop = true;
this.radioBlack.Text = "黒";
this.radioBlack.UseVisualStyleBackColor = true;
//
// radioBlue
//
this.radioBlue.Appearance = System.Windows.Forms.Appearance.Button;
this.radioBlue.AutoSize = true;
this.radioBlue.Location = new System.Drawing.Point(46, 274);
this.radioBlue.Name = "radioBlue";
this.radioBlue.Size = new System.Drawing.Size(27, 22);
this.radioBlue.TabIndex = 6;
this.radioBlue.TabStop = true;
this.radioBlue.Text = "青";
this.radioBlue.UseVisualStyleBackColor = true;
//
// radioRed
//
this.radioRed.Appearance = System.Windows.Forms.Appearance.Button;
this.radioRed.AutoSize = true;
this.radioRed.Location = new System.Drawing.Point(79, 274);
this.radioRed.Name = "radioRed";
this.radioRed.Size = new System.Drawing.Size(27, 22);
this.radioRed.TabIndex = 7;
this.radioRed.TabStop = true;
this.radioRed.Text = "赤";
this.radioRed.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(452, 308);
this.Controls.Add(this.radioRed);
this.Controls.Add(this.radioBlue);
this.Controls.Add(this.radioBlack);
this.Controls.Add(this.btnRtf);
this.Controls.Add(this.btnColor);
this.Controls.Add(this.RichTextBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
void btnColor_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
Color colSelect = new Color();
for (int i = 0 ; i < RichTextBox1.Text.Length ; i++)
{
RichTextBox1.Select(i, 1);
// 文字表示色
if (RichTextBox1.SelectionColor != colSelect)
{
if (RichTextBox1.SelectionColor == BLACK)
{
// 黒色
sb.Append("BLACK\n");
}
else if (RichTextBox1.SelectionColor == BLUE)
{
// 青色
sb.Append("BLUE\n");
}
if (RichTextBox1.SelectionColor == RED)
{
sb.Append("RED\n");
}
// 中略
colSelect = RichTextBox1.SelectionColor;
}
}
MessageBox.Show(sb.ToString());
}
void RichTextBox1_GotFocus(object sender, EventArgs e)
{
// 文字色再設定
if (radioBlack.Checked) RichTextBox1.SelectionColor = BLACK;
else if (radioBlue.Checked) RichTextBox1.SelectionColor = BLUE;
else if (radioRed.Checked) RichTextBox1.SelectionColor = RED;
// 以下略
}
private void radioBlack_Click(object sender, EventArgs e)
{
// 選択部分の文字色更新
RichTextBox1.SelectionColor = BLACK;
RichTextBox1.Focus();
}
private void radioBlue_Click(object sender, EventArgs e)
{
RichTextBox1.SelectionColor = BLUE;
RichTextBox1.Focus();
}
private void radioRed_Click(object sender, EventArgs e)
{
RichTextBox1.SelectionColor = RED;
RichTextBox1.Focus();
}
private void buttonRtf_Click(object sender, EventArgs e)
{
MessageBox.Show(RichTextBox1.Rtf);
}
}
それと、本題からは逸れますが気になった点があるので書いておきます。
・GotFocusの使用は推奨されていません。代わりにEnterを使用します。
Control.GotFocus イベント (System.Windows.Forms)
http://msdn2.microsoft.com/ja-jp/library/system.windows.forms.control.gotfocus.aspx
・そもそもGotFocusの文字色再設定の処理は不要ではないですか?ラジオボタンのクリックで色を設定していますから。
|