using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton5.Text = "TEP Code RB5";
radioButton6.Text = "TEP Code RB6";
radioButton7.Text = "TEP Code RB7";
radioButton8.Text = "TEP Code RB8";
}
}
}
It depends on your ease, you can use the both method to set the name of radio buttons. For your better understanding, we have attached the screenshot of output along with the code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Checked = true;
}
}
}
If you will add more radio buttons checked true then the only last radio button will be checked. Because you can only be checked one radio button at the same time. In the following image, you can preview that only radio button1 is checked.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace strnull
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.BackColor = Color.AliceBlue;
radioButton2.BackColor = Color.AntiqueWhite;
radioButton3.BackColor = Color.Aqua;
radioButton4.BackColor = Color.Aquamarine;
radioButton5.BackColor = Color.Azure;
radioButton6.BackColor = Color.Beige;
radioButton7.BackColor = Color.Bisque;
radioButton8.BackColor = Color.BlueViolet;
}
}
}
You must have to use the Color object to set any color. For the better understanding, we have taken the screenshot of output along with the code which is overriding the color properties.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.ForeColor = Color.YellowGreen;
radioButton2.ForeColor = Color.Tomato;
radioButton3.ForeColor = Color.Violet;
radioButton4.ForeColor = Color.Turquoise;
radioButton5.ForeColor = Color.SteelBlue;
radioButton6.ForeColor = Color.Sienna;
radioButton7.ForeColor = Color.SeaGreen;
radioButton8.ForeColor = Color.BlueViolet;
}
}
}
We used the Color object to set the color. In the following image, you can observe that how the color changes the appearance and interface. By using the color schemes you can make eye attractive windows form and make easier for the user to identified usable features of your app.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Font = new Font(Font.FontFamily,10);
radioButton2.Font = new Font(Font.FontFamily, 11);
radioButton3.Font = new Font(Font.FontFamily, 12);
radioButton4.Font = new Font(Font.FontFamily, 13);
radioButton5.Font = new Font(Font.FontFamily, 12);
radioButton6.Font = new Font(Font.FontFamily, 11);
radioButton7.Font = new Font(Font.FontFamily, 10);
radioButton8.Font = new Font(Font.FontFamily, 9);
}
}
}
In the above code, we have used a different size for each RadioButton. The first four RadioButton size is increased and other four RadioButton size get a decrease. In the following image, you can observe the size of radio buttons.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Font = new Font("Times New Roman", 11);
radioButton2.Font = new Font("Century", 11);
radioButton3.Font = new Font("Arial", 11);
radioButton4.Font = new Font("Comic Sans MS", 11);
radioButton5.Font = new Font("Copperplate Gothic Light", 11);
radioButton6.Font = new Font("Georgia", 11);
radioButton7.Font = new Font("Impact", 11);
radioButton8.Font = new Font("Lucida Console", 11);
}
}
}
All the above-used fonts are the default font which is available in Windows. If you don't know which font you have to used then used Google Font. It will help you to select the best font for your desktop application. In the following image, you can observe how font changes the appearance of your desktop application.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show(radioButton1.Text);
}
else
if (radioButton2.Checked == true)
{
MessageBox.Show(radioButton2.Text);
}
else
if (radioButton3.Checked == true)
{
MessageBox.Show(radioButton3.Text);
}
else
if (radioButton4.Checked == true)
{
MessageBox.Show(radioButton4.Text);
}
else
if (radioButton5.Checked == true)
{
MessageBox.Show(radioButton5.Text);
}
else
if (radioButton6.Checked == true)
{
MessageBox.Show(radioButton6.Text);
}
else
if (radioButton7.Checked == true)
{
MessageBox.Show(radioButton7.Text);
}
else
if (radioButton8.Checked == true)
{
MessageBox.Show(radioButton8.Text);
}
}
}
}
If you want to try the above code in your own compiler, then you have to add button click event first then you can execute the above code. It will allow you to select an option, and when you press the button it will generate the popup message and return the value of selected radio button. In the following image, you can observe the output of the above code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Image = Image.FromFile("C:\\Users\\Jade\\Pictures\\brownImage.jpg");
}
}
}
| First Method | Second Method |
|---|---|
using System;
using System.Drawing;
using System.Windows.Forms;
namespace strnull
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
radioButton1.BackColor = Color.AliceBlue;
}
private void radioButton1_BackColorChanged(object sender, EventArgs e)
{
MessageBox.Show("BackColor is changed to " + radioButton1.BackColor.ToString());
}
}
}
In the above code, when the background color of the radio button is changed. Message prompt with the name of color which is set as the background. In the following image, you can observe the output of the above code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("Option changed.");
}
}
}
In the above code, we have used two radio button. Whenever you will change the radio buttons value or option CheckedChanged event gets executed. In the below image you can observe the output
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_Click(object sender, EventArgs e)
{
MessageBox.Show("You Clicked!");
}
}
}
In the above code, we have used two radio buttons and just added the click event to the first radio button. If you will click the radio button1 then you will observe the popup message but if you clicked in the radio button2 then nothing will happen except that radio button2 get selected. It's because we have added the Click event just for the radio button1. In the following image, you can observe the output of above code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
radioButton1.ForeColor = Color.BlueViolet;
}
private void radioButton1_ForeColorChanged(object sender, EventArgs e)
{
MessageBox.Show("ForeColor Is changed to " + radioButton1.ForeColor.ToString());
}
}
}
In the above code, we have set the ForeColor ti BlueViolet when user will click the button. Then ForeColorChanged Event executed and prompt the message which color is selected as the ForeColor. In the following image, you can observe the output of the above code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_MouseHover(object sender, EventArgs e)
{
MessageBox.Show("Mouse Hover to " + radioButton1.Text.ToString());
}
}
}
In the above code, we have set the prompt message when user will hover the first radio button. You can declare any kind of functionality which you want to perform. In the following Image, you can be observed the output.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace strnull
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_MouseHover(object sender, EventArgs e)
{
MessageBox.Show("Mouse Hover to " + radioButton1.Text.ToString());
}
private void radioButton1_MouseLeave(object sender, EventArgs e)
{
MessageBox.Show("Mouse Left " + radioButton1.Text.ToString());
}
}
}
In the above code, you cab observed the MouseHover and MouseLeave both events. When user will hover the mouse a prompt message show that user is hover mouse cursor on radiobutton1 and when a user left the radiobutton1 boundaries its show prompt message again. In the below image you can observe the output of above code.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
radioButton1.Text = "New Text";
}
private void radioButton1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Radio Button Text changed to " + radioButton1.Text.ToString());
}
}
}
You can observe in the above code, there are two events. The button click event changed the text of radio button and TextChanged event get executed when text gets changed. In the following image, you can be observed the output. You also observed that when button event is executed radio button text doesn't change but the value is set to new text. After the execute of textChanged Event, it's get changed. It's because all the events have same priority or execution, so first events get executed then their effects applied.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("Pakistan");
listBox1.Items.Add("United States");
listBox1.Items.Add("United Kingdom");
listBox1.Items.Add("India");
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
String var1 = "Lahore";
String var2 = "Multan";
String var3 = "Islamabad";
String var4 = "Karachi";
listBox1.Items.Add(var1);
listBox1.Items.Add(var2);
listBox1.Items.Add(var3);
listBox1.Items.Add(var4);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
String[] city = new String[] { "Lahore", "Multan", "Islamabad", "Karachi"};
for (int x = 0; x < city.Length; x++)
{
listBox1.Items.Add(city[x]);
}
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("A");
listBox1.Items.Add("B");
listBox1.Items.Add("C");
listBox1.Items.Add("D");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(listBox1.SelectedItem.ToString());
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Sunday");
listBox1.Items.Add("Monday");
listBox1.Items.Add("Tuesday");
listBox1.Items.Add("Wednesday");
listBox1.Items.Add("Thursday");
listBox1.Items.Add("Friday");
listBox1.Items.Add("Saturday");
listBox1.SelectionMode = SelectionMode.MultiSimple;
}
private void button1_Click(object sender, EventArgs e)
{
foreach (Object obj in listBox1.SelectedItems )
{
MessageBox.Show(obj.ToString ());
}
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("A");
listBox1.Items.Add("B");
listBox1.Items.Add("C");
listBox1.Items.Add("D");
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("A");
listBox1.Items.Add("B");
listBox1.Items.Add("C");
listBox1.Items.Add("D");
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.RemoveAt(0);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("A");
listBox1.Items.Add("B");
listBox1.Items.Add("C");
listBox1.Items.Add("D");
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Remove("A");
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("A");
listBox1.Items.Add("B");
listBox1.Items.Add("C");
listBox1.Items.Add("D");
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Font = new Font(Font.FontFamily, 12);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("A");
listBox1.Items.Add("B");
listBox1.Items.Add("C");
listBox1.Items.Add("D");
listBox1.BackColor = Color.LightCyan;
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("The");
listBox1.Items.Add("Engineering");
listBox1.Items.Add("Projects");
listBox1.Font = new Font(Font.FontFamily, 15);
listBox1.BackColor = Color.Black;
listBox1.ForeColor = Color.White;
}
}
}
There are several more events which you can try to make list box more interactive. These events are used to give innovative concepts.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Warn ! Click is disable..");
}
}
}
This event occurs when user will click twice on the Listbox. Supposed you are wanted to perform any action when user will double click on the ListBox, then we will use this event. In the following code, we are changing the back color of ListBox when user will click twice.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
listBox1.BackColor = Color.Black;
}
}
}
To demonstrate double click event, we changed the color. You can code any functionality according to your needs.
This event occurs when user will hover mouse cursor on the Listbox. In the following code, we have changed the foreColor on mouse hover. When the mouse cursor will hover the color of font will be changed.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("The Engineering Projects");
}
private void listBox1_MouseHover(object sender, EventArgs e)
{
listBox1.ForeColor = Color.Bisque;
}
}
}
This event occurs when user will remove the cursor from the Listbox. In the above code, we have changed the forecolor when a user hovers the mouse cursor. In the below code, we will change the forecolor again to black when a user removes mouse cursor.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("The Engineering Projects");
}
private void listBox1_MouseHover(object sender, EventArgs e)
{
listBox1.ForeColor = Color.Bisque;
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
listBox1.ForeColor = Color.Black;
}
}
}
This event occurs when user will change the background color of the Listbox. In the following code, we are going to change the color of background when user will hover the mouse on ListBox. When color will change, BackColorChanged event will occur. In which we will show popup message.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("The Engineering Projects");
}
private void listBox1_MouseHover(object sender, EventArgs e)
{
listBox1.BackColor = Color.Bisque;
}
private void listBox1_BackColorChanged(object sender, EventArgs e)
{
MessageBox.Show("BackGround Color is changed !!");
}
}
}
This event occurs when user will change the foreground color of the Listbox. In the following code, we will be used mouse hover event to changed the foreColor and ForeColorChanged Event also. The forecolorchanged event will raise the message popup when a color of the foreground is getting changed.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("The Engineering Projects");
}
private void listBox1_MouseHover(object sender, EventArgs e)
{
listBox1.ForeColor = Color.Bisque;
}
private void listBox1_ForeColorChanged(object sender, EventArgs e)
{
MessageBox.Show("ForeGround Color is changed !!");
}
}
}
This event occurs when user will select any item in the Listbox. In the below code I have taken two list box. When user will select any value from the first list box then according to that value listbox2 will show it's valued.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.Items.Add("The Engineering Projects");
listBox1.Items.Add("C# Tutorials");
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == 0)
{
listBox2.Items.Clear();
listBox2.Items.Add("Arduino Projects");
listBox2.Items.Add("Proteus Projects");
listBox2.Items.Add("Matlab Projects");
}
else
if (listBox1.SelectedIndex == 1)
{
listBox2.Items.Clear();
listBox2.Items.Add("C# Label");
listBox2.Items.Add("C# TextBox");
listBox2.Items.Add("C# ComboBox");
}
}
}
}
If you wanted to test the above code, then place two list boxes and copied the above code.
We have tried to give basics to major concepts of C# ListBox. All the above codes are working fine and added after testing. If you will cover all the above codes then you will become able to play with ListBoxes. ListBoxes play very important roles in Point Of Sale applications. Thanks for reading our tutorial, moreover you can watch ListBox video.
Button are reusable components such as the exit and quit buttons which will perform the same functionality in each form and able to reuse again and again. The button will give end-user quite a clear navigation of software. The shape of the button is unique and have its own appearance properties by which its user can easily judge where is the button located. In short, the button is the controller which is used to create interactivity between user and application. Did you know that button is directly inherited from its base class? which is ButtonBase. You can click the button with the mouse and even with keyboard keys, it depends on the situation. So, let's have a look at How to sue C# Button Control:
There are two ways using which you can add the C# button into your program. First one is the programming way to create a button.It's quite difficult one. The second way is to drag the button directly from the toolbar and place it on your application. Which is much easier than the programming one. When you drag the button you will get the default name of the button as button1 which you can change from the right panel under the properties tab.
button1.Text = "TEP Button Example Text";
button1.Image = Image.FromFile("C:\\Users\\Jade\\Pictures\\brownImage.jpg");
button1.BackColor = Color.Aqua;
button1.ForeColor = Color.White; button1.BackColor = Color.Black;
button1.ForeColor = Color.White; button1.BackColor = Color.Black; button1.Font = new Font(button1.Font.FontFamily, 33);
int newSize = 33; button1.ForeColor = Color.White; button1.BackColor = Color.Black; button1.Font = new Font(button1.Font.FontFamily, newSize);
Now we will create the code of above-listed events and handle them with their methods. You can create separate methods and appoint them in replacement of built-in methods. But that's some difficult, so we will simply use the built-in methods.
This event will occur when end user will click on the button once. If you want to perform any functionality when user will click on the button then we will use the _Click event to handle this functionality. After the name of a button, you have to write the _Click, suppose you go with default name which is button1. Then you have to create the method with name button1_Click. In the following code, I have used the button when user will click, it will generate the message box.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("C# Button Click Event Executed");
}
}
}
This event occurs when the text of the button is changed. This is the built-in method with the name _TextChanged. In the following code, there is a button1_TextChanged event which will generate the message popup when the text of the button is changed. Button1_Click event is used to change the text when user will click. Logic is when user will click the button, the text of button will be changed and button1_TextChanged is performed.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "New Text";
}
private void button1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("C# Button TextChanged Event");
}
}
}
This event occurs when user will hover the mouse cursor on the button. This is a built-in event with the name _MouseHover after the default name of the button or your desired button named. Here in this code, we have used the default name of a button which is button1. When user will hover the mouse cursor over the button it will generate the message popup.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_MouseHover(object sender, EventArgs e)
{
MessageBox.Show("C# Button MouseHover Event");
}
}
}
This event is occurring when user will leave the button or move the cursor from the button boundaries. In the following code, we have created the _MouseLeave event with the default name of the button. When user will remove the mouse cursor out of button it will get executed and generate the message popup.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TEPTUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_MouseLeave(object sender, EventArgs e)
{
MessageBox.Show("C# Button MouseLeave Event");
}
}
}
Now you are able to add functionalities in C# button. We have tried to explain you the major concepts so that you can get the idea to work with C# button. By using the above events you can make your software more accurate and interactive. We have described the basic two major functionalities. Now you are able to change text, color, style, and handle the events. If are facing any kind of problem regarding the codes, let us know through the comment section.
System.Windows.Forms
label1.Text = "TEP C# Label Control Example Text";
label1.BackColor = Color.Aqua;
label1.ForeColor = Color.BlueViolet;
label1.Image = Image.FromFile("C:\\Users\\Jade\\Pictures\\brownImage.jpg");
There are several more events which we can use, but the main task here is to just give you the basic concept of C# Label. That's why we will focus on most commonly used events.
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "Text Changed";
}
private void label1_DoubleClick(object sender, EventArgs e)
{
label1.Text = "Text Changed";
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "Text Changed";
}
private void label1_TextChanged(object sender, EventArgs e)
{
label2.Text = "2nd Text Changed";
}
private void label1_MouseHover(object sender, EventArgs e)
{
label1.Text = "Text Changed";
}
private void label1_MouseLeave(object sender, EventArgs e)
{
label1.Text = "again Text Changed";
}
If you are looking to learn more about the C# label control and other features of the C# programming language than you should subscribe our YouTube channel where we have uploaded latest tutorials on C#. Thanks for reading, have a good day !!! :)
C# TextBox is used to get input from users or it can also be used to display some values to the user. The textbox is a container for text blocks, you can take inputs or show the text as you required in the form of paragraphs. If you used TextBox as the input field then you can only take one line of text as the input but if you want multiple line input then you have to activate the multiple line property of C# TextBox.
You can also change your input field as the password field if you are using the TextBox for password. A user can write input in the textbox and the even user can simply paste the data too in the field. You can also add some advance features like grabbing the copied data automatically when user hover the mouse on the input field, it will be done by accessing the clipboard where the copied data is saved temporarily, but this is the advance feature which we will cover after the basics. So, now let me show you how to use C# TextBox:
textBox1.Text = "TheEngineeringProjects.com";
string var = "TheEngineeringProjects.com"; textBox1.Text = var;
string var; var = textBox1.Text;
textBox1.Width = 250; textBox1.Height = 50;
textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;
textBox1.BorderStyle = BorderStyle.Fixed3D; textBox1.BorderStyle = BorderStyle.FixedSingle; textBox1.BorderStyle = BorderStyle.None;
private void textBox1_keydown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
MessageBox.Show("You Pressed Enter");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
}
textBox1.MaxLength = 40;
textBox1.ReadOnly = true;
textBox1.Multiline = true;
textBox1.PasswordChar = '*';
//First Method textBox1.Text += "your text" + "\r\n"; //Second Method textBox1.Text += "your text" + Environment.NewLine;
int i; i = int.Parse (textBox1.Text);
//String to Float conversion float i; i = float.Parse (textBox1.Text); //String to Double conversion double i; i = float.Parse (textBox1.Text);
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Width = 250;
textBox1.Height = 50;
textBox1.Multiline = true;
textBox1.BackColor = Color.Blue;
textBox1.ForeColor = Color.White;
textBox1.BorderStyle = BorderStyle.Fixed3D;
}
private void button1_Click(object sender, EventArgs e)
{
string var;
var = textBox1.Text;
MessageBox.Show(var);
}
}
}
You can add the combo box from the toolbar and drag it on the form. When you drag that you can set the height and width by just moving the coordinates of combo box field. In other words, ComboBox is a combination of textbox and list box to show the list data as the text.So,now let's have a look at How to control C# ComboBox:
comboBox1.Items.Add("The");
comboBox1.Items.Add("Engineering");
comboBox1.Items.Add("Projects");
var item = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
var item = this.comboBox1.GetItemText(this.comboBox1.SelectedItem); MessageBox.Show(item);
We have inserted the values in above codes, now move on to how we can delete or remove the inserted values. As we used the Add attribute to inserted the values now we will use the Remove and RemoveAt to delete the values. These are the two attributes which we can be used for the same purpose. The first attribute will remove the values directly like you have added the value "Name" then you can delete is as Remove("Name") and the second method is RemoveAt which will remove the value by index like RemoveAt(1) if you don't get the concept, then check the example code below.
comboBox1.Items.RemoveAt(1);
comboBox1.Items.Remove("The Engineering Projects");
You can even change the properties of dropdown by using the DropDownStyle property. It's used to determine that what you actually wanted, mean, are you wanted to show the list always or the data is displayed in a list. You can also edit the text alignment inside of the list.
There are three values which you can be used for the DropDownStyle Property.
You can check the following code where I have shared how you can use these properties, don't copy all the properties to run them at once. Because the last property will get override on all above two properties. So always do code with precautions.
comboBox1.DropDownStyle = ComboBoxStyle.Simple; comboBox1.DropDownStyle = ComboBoxStyle.DropDown; comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
You can set the default selected item in the combo box by declaring the SelectedItem property. There are two ways, one you can set it by SelectedItem or you can do by passing the value as FindStringExact. Let's move on code and understand the concept behind these two properties.
comboBox1.Items.Add("The");
comboBox1.Items.Add("Engineering");
comboBox1.Items.Add("Projects");
comboBox1.SelectedItem = "Projects";
In the above code we have inserted three values and by SelectedItem we passed the value which we want to set as the default in a combo box. Let's view the below code which is the second method.
comboBox1.Items.Add("The");
comboBox1.Items.Add("Engineering");
comboBox1.Items.Add("Projects");
comboBox1.SelectedIndex = comboBox1.FindStringExact("Projects");
In this method, we have used the SelectedIndex property and passed the string value by parsing them from FindStringExact method. This is the alternative technique to set the combo box selected item and difficult to remember. You can use them both but the first is easy to remember and understand.
You can also retrieve the database values in a combo box, by using the DataSource property. Let's check the code below how we retrieve the database values in Combo Box.
comboBox1.DataSource = datasetVariable.Tables[0]; comboBox1.ValueMember = "databaseTable_ID"; comboBox1.DisplayMember = "databaseTable_NAME";
We have to use the DataSet variable to retrieve the values from a database, then we will pass that values to DataSource property of C# ComboBox. ValueMember will store the ID values and DisplayMember stored the values of Names, just suppose a SQL query it's same like that. Such as "SELECT databaseTable_ID, databaseTable_NAME FROM StudentsTable".
If you want to manipulate the data between two C# comboBox then you can do easily. Suppose you have taken two combo box and wanted that when you select any value in the first combo box then the value of second combo box gets changed according to the first combo box. Then you have to use the SelectIndexChanged Event. Let's create the code to demonstrate the concept.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("TEP Arduino Projects");
comboBox1.Items.Add("TEP C# Tutorials");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
if (comboBox1.SelectedItem == "TEP Arduino Projects")
{
comboBox2.Items.Add("Stepper Motor Direction Control");
comboBox2.Items.Add("Stepper Motor Speed Control");
}
else if (comboBox1.SelectedItem == "TEP C# Tutorials")
{
comboBox2.Items.Add("How to use C# Comments");
comboBox2.Items.Add("How to add C# Control in Windows Form");
comboBox2.Items.Add("How to use Button in C# Windows Form");
}
}
}
}
Before using this code, you have to drag two C# combobox on your form and don't change their name. Then go to the main code and replace that with the above code and execute. The first C# combobox gives you two options in the list as TEP C# Tutorials and TEP Arduino Projects. When you selected any of these values the second combo values get changed. Such as if you selected TEP C# Tutorials then the second combo box will show the values as below.
But if you selected the TEP Arduino Projects then the second combo box value is as follows.
By this, you can create innovative software and give maximum utilities to a user. This is the simple example of multiple combo boxes to give the better understanding of the concept, you can make more advanced code than the above.
You make your combo box unable for a user to give input. Sometimes we need to block the combo box on some conditions. Like combo box will not become enabled until a user will not fill some pre-check statements. There are two ways by which we will change the combo box to read-only. The first way is by changing the DropDownStyle to DropDownList. It will allow users to just read the data but a user will not able to insert any kind of data. The second method is to completely disable the combo box for a user by make the Enable property to false.
First Method:
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
Second Method:
comboBox1.Enabled = false;
Let's revised all above concepts once again and summarize then in one code. There are much more functionalities which we don't share right now. Because that each function is the whole new concept, such as the OLED or different types of databases which you can connect with your application and the different method of retrieved the data.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("one");
comboBox1.Items.Add("Two");
comboBox1.Items.Add("Three");
comboBox1.Items.Add("Four");
comboBox1.SelectedIndex = comboBox1.FindStringExact("Two");
}
private void button1_Click(object sender, EventArgs e)
{
string var;
var = comboBox1.Text;
MessageBox.Show(var);
}
}
}
In the above code first, drag the combo box and button on your application and copy the code for execution. When you will execute the code it will allow you to select any item from the list and by default Two is selected. When you click the button it will show you the message with the value of selected item from C# combobox.
Here is the screenshot of C# comboBox Code. You can observe which libraries you have to use for testing the codes.So, before starting this C# if else tutorial, I would suggest you to first read all of those basic C# tutorials so that you know how to use different data types. Before starting this tutorial, you should also read How to add C# Control in Windows Form because we are gonna use three C# controls in this tutorial.So, let's get started with How to use C# if else statement:
IF ( Condition is TRUE ) ====> Then do this TASK.
IF ( MULTIPLY button is pressed ) ====> Then multiply the two Integer variables
if (txtClick.Text == "TEP")
{
label1.Text = "My Blog";
}
if (txtClick.Text == "TEP")
{
label1.Text = "My Blog";
}
else
{
label1.Text = " NOT my Blog";
}
I am gonna use the same project which we have designed in our first lecture on C# named as Introduction to C# Windows Forms. So, you should also use the same project and as we know this project has one Button and one Text box. Just a recall that I have changed the Text of the button to Click Here and the name of the button to ClickHere. Similarly, I have changed the name of the text box to txtClick. Here's the image of that form:
So, let's continue with How to use C# ArrayList. :)
Before going into the details of using C# ArrayList, let's first have a look at its definition i.e. what is C# ArrayList and why we have to use it.
// .... Initialization of C# ArrayList .....
ArrayList TEP = new ArrayList();
// .... Ends Here ......
// .... Adding Data in ArrayList ....
TEP.Add("The");
TEP.Add("Engineering");
TEP.Add("Projects");
// .... Data added in ArrayList ....
// ... Displaying values .....
txtClick.Text = TEP[0].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP[1].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP[2].ToString();
// ... Values Displayed .....
// .... Display Values .....
txtClick.Text = TEP[0].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP[1].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP[2].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP.Count;
// ..... Values Displayed .....
txtClick.Text = TEP[0].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP[1].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP[2].ToString();
txtClick.Text += " , ";
txtClick.Text += TEP.Count;
TEP.Clear();
txtClick.Text += " , ";
txtClick.Text += TEP.Count;
Now, I hope that you are well aware of ArrayList and can easily use it in your program if you have to. So, that's all for today, will see you guys in the next tutorial. Till then take care and have fun !!! :)
Let's take an example for C# arrays from normal life. You can consider a classroom of a school as an array and its elements will be all the students in that class. :) So, if there's a 9th class then all the students in 9th class are the members of that 9th class array but the students of 10th class will not be members of 9th class array, instead they will be members of 10th class array. :P Now, a question arises that how to call a single member of any array then in that case for class arrays we have roll numbers. so you can call any member of 9th class by calling his roll number. Similarly, in C# array we have indexes for each member of an array and we can call individual members using these indexes, we will have a look at it below. The members of any arrary are normally called the Elements of that array. So, let's get started with C# arrays.
You may also like:
FirstArray[3] = {Element1, Element2, Element3};
// C# Array Initializing String[] students = new String[5]; // Initializing Complete
// C# Array Initializing String[] students = new String[]; // Initializing Complete
// Adding values to C# Array. students[0] = "Zain"; students[1] = "Nasir"; students[2] = "Kamraan"; students[3] = "John"; students[4] = "Jack"; // Values added.
// Adding values to C# Array.
String[] students = new String[5] {"Zain", "Nasir", "Kamraan", "John", "Jack"};
// Values added.
txtClick.Text = students[0]; txtClick.Text += " , "; txtClick.Text += students[1]; txtClick.Text += " , "; txtClick.Text += students[2]; txtClick.Text += " , "; txtClick.Text += students[3]; txtClick.Text += " , "; txtClick.Text += students[4];
I have also shared a tutorial on How to use comments in c++, you should read that post as well because I have explained few basic concepts in it. You guys are also welcome to give us feedback about this new C# tutorial series. If you have any suggestions about it then do let us know in comments.