SUBD_LAB/SUBD_LABA/View/FormPhoto.cs

104 lines
2.4 KiB
C#
Raw Normal View History

2024-05-14 22:32:28 +04:00
using Commentbase;
using Database;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace View
{
public partial class FormPhoto : Form
{
public int? PhotoId { get; set; }
private Abstracts db;
public FormPhoto(Abstracts abstracts)
{
InitializeComponent();
db = abstracts;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (PhotoId.HasValue)
{
db.UpdatePhoto(new()
{
Id = PhotoId.Value,
Title = textBox1.Text,
Description = textBox2.Text,
Privacy = textBox3.Text,
UploadDate = dateTimePicker1.Value,
ImagePath = textBox4.Text,
AlbumId = (comboBoxAlbum.SelectedItem as Album).Id,
LocationId = (comboBoxLocation.SelectedItem as Location).Id,
AuthorId = (comboBoxAuthor.SelectedItem as Author).Id
});
}
else
{
db.CreatePhoto(new()
{
Title = textBox1.Text,
Description = textBox2.Text,
Privacy = textBox3.Text,
UploadDate = dateTimePicker1.Value,
ImagePath = textBox4.Text,
AlbumId = (comboBoxAlbum.SelectedItem as Album).Id,
LocationId = (comboBoxLocation.SelectedItem as Location).Id,
AuthorId = (comboBoxAuthor.SelectedItem as Author).Id
});
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LoadData()
{
try
{
var albums = db.GetAlbums();
var locations = db.GetLocations();
var authors = db.GetAuthors();
comboBoxAlbum.DataSource = albums;
comboBoxLocation.DataSource = locations;
comboBoxAuthor.DataSource = authors;
comboBoxAlbum.DisplayMember = "Title";
comboBoxLocation.DisplayMember = "Name";
comboBoxAuthor.DisplayMember = "Name";
comboBoxAlbum.ValueMember = "Id";
comboBoxLocation.ValueMember = "Id";
comboBoxAuthor.ValueMember = "Id";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormPhoto_Load(object sender, EventArgs e)
{
LoadData();
}
}
}