Читаем Программирование КПК и смартфонов на .NET Compact Framework полностью

 ListViewItem lvi = listView.Items[listView.SelectedIndices[0]];

 bool isFolder = lvi.ImageIndex == 0;

 string s = "Are you sure you want to delete " + lvi.Text;

 if (isFolder)

  s += " and all its content";

 s += "?";

 if (MessageBox.Show(s, this.Text, MessageBoxButtons.YesNo,

  MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) ==

  DialogResult.Yes) {

 if (isFolder)

  Directory.Delete(path + lvi.Text, true);

 else

  File.Delete(path + lvi.Text);

 fillList();

}

Перед удалением папки или файла запрашивается подтверждение действий пользователя. Для создания новой папки используется следующий код, приведенный в листинге 7.19.

Листинг 7.19

private void newFolderMenuItem_Click(object sender, System.EventArgs e) {

 Cursor.Current = Cursors.WaitCursor;

 ListViewItem lvi = listView.Items[listView.SelectedIndices[0]];

 NameForm nameForm = new NameForm(this, "Новая папка", "",

  new SetNameDelegate(SetNewName));

 if (nameForm.ShowDialog() == DialogResult.OK) fillList();

 listView.Focus();

}

В результате действия этой функции отображается форма NameForm с заголовком Новая папка. Эта форма также передает информацию в главную форму при помощи метода SetNewName, который приведен в листинге 7.20.

Листинг 7.20

///

/// Устанавливает новое имя для папки

///

/// Имя для папки

public void SetNewName(string name) {

 Directory.CreateDirectory(path + name);

}

Метод создает папку с заданным именем. Как видно, код его чрезвычайно прост.

Код для выполнения команды Свойства приведен в листинге 7.21.

Листинг 7.21

private void propertiesMenuItem_Click(object sender, System.EventArgs e) {

 Cursor.Current = Cursors.WaitCursor;

 ListViewItem lvi = listView.Items[listView.SelectedIndices[0]];

 FileInfo fi = new FileInfo(path + lvi.Text);

 PropertiesForm propertiesForm =

  new PropertiesForm(this, fi, new SetNameDelegate(SetRename),

  new SetAttributesDelegate(SetAttributes));

 if (propertiesForm.ShowDialog() == DialogResult.OK) fillList();

 listView.Focus();

}

Этот код вызывает форму PropertiesForm, которая отображает атрибуты выбранного файла или папки. Также в этой форме пользователь может изменять атрибуты файла при помощи метода SetAttributes, код которого приведен в листинге 7.22.

Листинг 7.22

public void SetAttributes(FileAttributes fileAttributes) {

 ListViewItem lvi = listView.Items[listView.SelectedIndices[0]];

 bool isFolder = lvi.ImageIndex = 0;

 if (isFolder) {

  DirectoryInfo di = new DirectoryInfo(path + lvi.Text);

  di.Attributes = fileAttributes;

 } else {

  FileInfo fi = new FileInfo(path + lvi.Text);

  fi.Attributes = fileAttributes;

 }

}

Для создания градиентной заливки соответствующего элемента интерфейса применяется метод, код которого приведен в листинге 7.23.

Листинг 7.23

public static void SetGradient(System.Windows.Forms.ListView listView) {

 // Новый вариант

 // Для .NET Compact Framework 2.0

 SendMessage(listView.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE,

  LVS_EX_GRADIENT);

 listView.Refresh();

}

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных