Добавление элементов управления Windows Forms во время выполнения
В этом примере новый экземпляр элемента управления TextBox Windows Forms создается во время выполнения.
Пример
private void Form1_Load(object sender, System.EventArgs e)
{
TextBox MyTextBox = new System.Windows.Forms.TextBox();
MyTextBox.Location = new System.Drawing.Point(64, 40);
MyTextBox.Name = "MyTextBox";
MyTextBox.Size = new System.Drawing.Size(88, 21);
MyTextBox.TabIndex = 0;
MyTextBox.Text = "MyTextBox";
Controls.Add(MyTextBox);
// Adding the DoubleClick event.
MyTextBox.DoubleClick += new EventHandler( MyTextBox_DoubleClick );
}
private void MyTextBox_DoubleClick(object sender, System.EventArgs e)
{
MessageBox.Show("MyTextBox doubleClick event.");
}
Компиляция кода[15]
· В этом примере требуется ссылка на пространство имен System.Windows.Forms.
Accessing and Displaying Data
This section provides links to topics that describe how to access XML data and how to display data in an application.
How to: Write Class Data to an XML File
This example writes the data stored in an object to a sample file that is named IntroToVCS.xml by using the Serialize method of the XmlSerializer class.
Example
public class Book { public string title; static void Main() { Book introToVCS = new Book(); introToVCS.title = "Intro to Visual CSharp"; System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer( introToVCS.GetType()); System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\IntroToVCS.xml"); writer.Serialize(file, introToVCS); file.Close(); } } |
Compiling the Code
You can compile the example directly at a command prompt, or paste the code into a console application by using the Visual Studio IDE.
Robust Programming
The following conditions may cause an exception:
· The file exists and is read-only.
· The path name may be too long.
· The disk may be full.
Security
This example creates a new file, if the file does not already exist. If the file already exists, the application overwrites it.
Доступ к данным и их отображение
В этом разделе содержатся ссылки на подразделы, описывающие способы доступа к данным XML и отображения данных в приложении.
Запись данных класса в XML-файл
В этом примере для записи данных, хранящихся в объекте, в образец файла с именем IntroToVCS.xml используется метод Serialize класса XmlSerializer.
Пример[16]
ß-------
Компиляция кода[17]
Пример можно скомпилировать непосредственно в командной строке либо вставить код в консольное приложение с помощью IDE Visual Studio.
Дата добавления: 2022-05-27; просмотров: 153;