Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Wednesday, 26 October 2016

How to use Color Dialog Box in C#

In this example, we are learning that how to use color dialog box control in c#.

First of all , we need to add Color Dialog Control in the form. In this example, one multi line textbox and two buttons were added in the form.



After adding the control, we are learning that how to change background color of textbox using color dialog box and also change font color of textbox.

When you click on the button color dialog box will be open and then you choose color. After selecting color, color will be change of textbox background color and font color.

Let's see the example :



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void background_color_Click(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.AllowFullOpen = true;
            colorDlg.AnyColor = true;
            colorDlg.SolidColorOnly = false;
            colorDlg.Color = Color.Red;
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.BackColor = colorDlg.Color;
            }
        }

        private void font_color_Click(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.AllowFullOpen = true;
            colorDlg.AnyColor = true;
            colorDlg.SolidColorOnly = false;
            colorDlg.Color = Color.Red;
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.ForeColor = colorDlg.Color;
            }
        }
    }
}

Output:

ColorDialogBox1

ColorDialogBox

0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.