Ниже приведен пример кода на C# с реализацией указанных требований:
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace EmployeeDataForm
{
public partial class MainForm : Form
{
private string fileName;
public MainForm()
{
InitializeComponent();
}
private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fileText = File.ReadAllText(openFileDialog.FileName, Encoding.Default);
string[] employeeData = fileText.Split(',');
textBoxSurname.Text = employeeData[0];
textBoxPosition.Text = employeeData[1];
textBoxSalary.Text = employeeData[2];
textBoxID.Text = employeeData[3];
fileName = openFileDialog.FileName;
this.Text = $"Открыт файл: {fileName}";
ShowAllControls();
}
}
private void CreateToolStripMenuItem_Click(object sender, EventArgs e)
{
if (SaveChangesPrompt())
{
fileName = null;
this.Text = "Новый сотрудник";
textBoxSurname.Text = string.Empty;
textBoxPosition.Text = string.Empty;
textBoxSalary.Text = string.Empty;
textBoxID.Text = string.Empty;
ShowAllControls();
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(fileName))
{
SaveEmployeeDataToFile();
}
else
{
SaveAsToolStripMenuItem_Click(sender, e);
}
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
fileName = saveFileDialog.FileName;
SaveEmployeeDataToFile();
this.Text = $"Открыт файл: {fileName}";
}
}
private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
{
if (SaveChangesPrompt())
{
fileName = null;
this.Text = string.Empty;
HideAllControls();
}
}
private void SaveEmployeeDataToFile()
{
if (IsValidInput())
{
string fileText = $"{textBoxSurname.Text},{textBoxPosition.Text},{textBoxSalary.Text},{textBoxID.Text}";
File.WriteAllText(fileName, fileText, Encoding.Default);
}
else
{
MessageBox.Show("Введены некорректные данные", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool SaveChangesPrompt()
{
if (!string.IsNullOrEmpty(textBoxSurname.Text)
|| !string.IsNullOrEmpty(textBoxPosition.Text)
|| !string.IsNullOrEmpty(textBoxSalary.Text)
|| !string.IsNullOrEmpty(textBoxID.Text))
{
DialogResult result = MessageBox.Show("Хотите сохранить изменения?", "", MessageBoxButtons.YesNoCancel);
if (result == DialogResult.Yes)
{
if (!string.IsNullOrEmpty(fileName))
{
SaveEmployeeDataToFile();
}
else
{
SaveAsToolStripMenuItem_Click(null, null);
}
}
else if (result == DialogResult.Cancel)
{
return false;
}
}
return true;
}
private bool IsValidInput()
{
if (string.IsNullOrEmpty(textBoxSurname.Text)
|| string.IsNullOrEmpty(textBoxPosition.Text)
|| string.IsNullOrEmpty(textBoxSalary.Text)
|| string.IsNullOrEmpty(textBoxID.Text))
{
return false;
}
if (!IsRussianLetters(textBoxSurname.Text) || !IsRussianLetters(textBoxPosition.Text))
{
return false;
}
if (!IsNumeric(textBoxSalary.Text) || !IsNumeric(textBoxID.Text))
{
return false;
}
return true;
}
private bool IsRussianLetters(string text)
{
foreach (char c in text)
{
if (!char.IsLetter(c) || !char.IsUpper(c))
{
return false;
}
}
return true;
}
private bool IsNumeric(string text)
{
foreach (char c in text)
{
if (!char.IsDigit(c))
{
return false;
}
}
return true;
}
private void ShowAllControls()
{
textBoxSurname.Visible = true;
textBoxPosition.Visible = true;
textBoxSalary.Visible = true;
textBoxID.Visible = true;
labelSurname.Visible = true;
labelPosition.Visible = true;
labelSalary.Visible = true;
labelID.Visible = true;
}
private void HideAllControls()
{
textBoxSurname.Visible = false;
textBoxPosition.Visible = false;
textBoxSalary.Visible = false;
textBoxID.Visible = false;
labelSurname.Visible = false;
labelPosition.Visible = false;
labelSalary.Visible = false;
labelID.Visible = false;
}
}
}
Вышеуказанный код предполагает использование winforms для создания формы. Файл EmployeeDataForm.Designer.cs будет содержать код разметки окна. Также, в проекте должен быть создан файл ресурсов с названием Resources.resx, в котором должны быть добавлены соответствующие ресурсы, такие как строки и иконка для меню.
Для создания проекта и добавления winforms формы в Visual Studio, следуйте инструкциям, приведенным в документации Microsoft: https://docs.microsoft.com/ru-ru/visualstudio/ide/step-1-create-a-windows-forms-application-project?view=vs-2022