Documentos de Académico
Documentos de Profesional
Documentos de Cultura
9.
C#
Copy Code
MessageBox.Show("Today is " +
DateTime.Today.ToLongDateString());
10.
11.
3.
4.
5.
C#
Copy Code
8.
C#
Copy Code
5.
Property
Value
Name
changeText
Text
Change Text
Size
80, 23
Double-click the button to create the changeText_Click event handler, and add the following
code:
C#
Copy Code
7.
Add another Button control to the form, and change the following properties
Property
Value
Name
changeColor
Text
Change Color
Size
80, 23
Double-click the button to create the changeColor_Click event handler, and add the following
code:
C#
Copy Code
this.label1.ForeColor =
Color.FromArgb(randomColor.Next(0, 256),
randomColor.Next(0, 256), randomColor.Next(0,
256));
8.
9.
10.
4.
5.
Property
Value
Name
inputText
Multiline
True
Size
175, 90
Add a Button control next to the text box, and change the following properties:
Property
Value
Name
retrieveInput
Text
Retrieve
Double-click the button to create the retrieveInput_Click event handler, and add the following
code:
C#
Copy Code
MessageBox.Show(this.inputText.Text);
6.
7.
8.
Example
C#
Copy Code
int anInteger;
anInteger = Convert.ToInt32(textBox1.Text);
anInteger = int.Parse(textBox1.Text);
Compiling the Code
This example requires:
sau varianta
private void button1_Click(object sender, EventArgs e)
{
int intreg1, intreg2, intreg3;
string cuvint;
intreg1=Convert.ToInt32(textBox1.Text);
intreg2=Convert.ToInt32(textBox2.Text);
intreg3 = intreg1 + intreg2;
cuvint = Convert.ToString(intreg3);
textBox3.Text =cuvint;
MessageBox.Show(cuvint);
}
private void button1_Click(object sender, EventArgs e)
{
int intreg1, intreg2, intreg3;
string cuvint;
intreg1=int.Parse(textBox1.Text);
intreg2=int.Parse(textBox2.Text);
intreg3 = intreg1 + intreg2;
cuvint = Convert.ToString(intreg3);
textBox3.Text =cuvint;
MessageBox.Show(cuvint);
}
The text converts to a number that is too large or too small to store as an int.
Example
C#
Copy Code
A form with a TextBox control named textBox1 and a Button control named button1. Set the
Note:
The code can also be used with a RichTextBox control by
substituting a RichTextBox control named richTextBox1 for the
TextBox control and changing the code from textBox1 to
richTextBox1.
Robust Programming
In this example you are setting the Text property before you retrieve the SelectedText value. In most
cases, you will be retrieving text typed by the user. Therefore, you will want to add error-handling code in
case the text is too short.
Example
C#
Copy Code
Robust Programming
The rich text format is very flexible, but any errors in the format lead to errors in the displayed
Example
C#
Copy Code
A RichTextBox control named richTextBox1. Insert the code segment into the Form1_Load
method
Property
Value
Name
formColor
6
Text
4.
Color
Drag a ColorDialog component from the Dialogs tab of the Toolbox to the form.
Double-click the Color button to create the default event handler in the Code Editor.
In the formColor_Click event handler, add the following code to display the color dialog box and
change the background color of the form according to the user's choice.
C#
Copy Code
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
this.BackColor = colorDialog1.Color;
}
7.
8.
9.
10.
5.
Property
Value
Name
folderPath
Text
Path
Drag a FolderBrowserDialog component from the Dialogs tab of the Toolbox to the form.
Double-click the button to create the default event handler in the Code Editor.
In the folderPath_Click event handler, add the following code to display the folder browser
dialog box and display the selected path in the label.
C#
Copy Code
if (folderBrowserDialog1.ShowDialog() ==
DialogResult.OK)
{
this.label1.Text =
folderBrowserDialog1.SelectedPath;
}
8.
9.
10.
11.
4.
5.
Property
Value
Name
labelFont
Text
Sample text
Add a Button control to the form, and change the following properties in the Properties window:
Property
Value
Name
setFont
Text
Change Font
Drag a FontDialog component from the Dialogs tab of the Toolbox to the form.
Double-click the button to create the default event handler in the Code Editor.
In the setFont_Click event handler, add the following code to display the font dialog box and
change the font of the text in the label according to the user's choice.
C#
Copy Code
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
this.labelFont.Font = fontDialog1.Font;
}
8.
9.
10.
11.
Property
Value
Name
saveTextFile
Text
Save As
5.
Double-click the button to add the default event handler in the Code Editor.
In the saveTextFile_Click event handler, add the following code to display the Save As dialog
box. This code saves the text typed in the RichTextBox control to a text file at the specified location.
C#
Copy Code
Example
Copy Code
Copy the code into the Load_Form1 event handler. When you run the program, you will be
prompted to select a text file. The contents of the selected file will be displayed in a RichTextBox control.
Robust Programming
Use the CheckFileExists, CheckPathExists, DefaultExt, Filter, Multiselect, and ValidateNames properties of the
OpenFileDialog control to limit run-time errors.
4.
Property
Value
Modifiers
Public
Add a Button control to the form, and change the following properties in the Properties window:
Property
Value
Name
addItems
Text
Add
On the Project menu, click Add Windows Form, leave the default name Form2, and then click
Add.
From the Toolbox, drag a Label control to the form, and change the Text property to Enter the
String (one per line).
Add a TextBox control to the form, and change the following properties in the Properties window.
Property
Value
10
Multiline
True
Scrollbars
Both
Size
255, 160
4.
Add a Button control to the form, and change the following properties in the Properties window:
Property
Value
Name
okButton
Text
OK
2.
Add the following code below the default constructor. This code creates an overloaded constructor
that requires Form1 as a parameter.
C#
Copy Code
Form1 mainForm;
public Form2(Form1 mainForm)
{
this.mainForm = mainForm;
InitializeComponent();
}
3.
In the Click event handler of okButton, add the following code. This code clears all existing text in
the list box, assigns each line of text of the text box on Form2 to an array, and then adds each item of
the array to the list box on Form1.
C#
Copy Code
if (this.textBox1.Text != string.Empty)
{
mainForm.listBox1.Items.Clear();
string[] stringsEntered = textBox1.Lines;
for (int count = 0; count < stringsEntered.Length;
count++)
{
11
mainForm.listBox1.Items.Add(stringsEntered[count]);
}
}
this.Close();
4.
5.
C#
Copy Code
Example
Copy Code
A form with a ListBox control named listBox1 and two buttons named button1 and button2. Add
the button1Click event handler to button1_Click, and the button2Click event handler to
button2_Click.
Example
Copy Code
A form named Form1 with a ListBox control named listBox1. Set the Load event handler of Form1
to Form1_Load. Set the SelectedIndexChanged event handler of listBox1 to
listBox1_SelectedIndexChanged.
Note:
This code can also be used with a ComboBox control by
substituting a ComboBox control named comboBox1 for the ListBox
control and changing the code from listBox1 to comboBox1.
Example
Copy Code
13
=
=
=
=
"One";
"Two";
"Three";
"Four";
listBox1.Items.AddRange(myList);
A form named Form1 with a ListBox control named listBox1. Set the Load event handler of Form1
to Form1_Load.
Note:
This code can also be used with a ComboBox control by
substituting a ComboBox control named comboBox1 for the ListBox
control and changing the code from listBox1 to comboBox1.
Robust Programming
The following conditions may cause an exception:
Example
Copy Code
}
else
}
Compiling the Code
This example requires:
A form with a ListBox control named listBox1 and a Button control named button1. Set the
button1Click event handler to button1_Click.
Note:
This code can also be used with a ComboBox control by
substituting a ComboBox control named comboBox1 for the
ListBox control and changing the code from listBox1 to
comboBox1.
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form2
otherForm =
new Form2();
15
}
private void InitForm32_Click(object sender, EventArgs e)
{Form2
ff32 = new Form2();
ff32.button2.Text = "BUTON2";
ff32.button2.Location = new System.Drawing.Point(50, 200);
ff32.button2.ForeColor = System.Drawing.Color.Green;
ff32.button2.Font = new System.Drawing.Font("Microsoft
Sans Serif", 13F);
ff32.Show();
}
private void button3_Click(object sender, EventArgs e)
{
preluc();
}
16
}
}
17