Selam bu kodun fonksiyonelliği tam çalışmıyor, Veritabanına ekleme, seçili öğeyi düzenleme ve güncelleme, seçili öğenin favori durumunu , pin durumunu tersine çevirme, gridview'de sadece işlem yapılan öğeleri güncelle performans gereksinimleri açısından, devamlı tümllisteyi güncelleme. işte düzeltmen gereken kodlar -> using System;
using System.Windows.Forms;
using Firebase.Database;
using Firebase.Database.Query;
using FirebaseAdmin;
using Google.Apis.Auth.OAuth2;
using System.Threading.Tasks;
public MainForm()
{
InitializeComponent(); // Form tasarımı yüklenecek
InitializeFirebase(); // Firebase Admin SDK başlatma
InitializeUI(); // Arayüz elemanlarını programatik olarak oluşturma
}
// Firebase Admin SDK başlatma
private void InitializeFirebase()
{
var path = "flashcard-e7f47-firebase-adminsdk-om656-e70a5506c2.json";
var credential = GoogleCredential.FromFile(path);
FirebaseApp.Create(new AppOptions
{
Credential = credential
});
firebaseClient = new FirebaseClient("https://flashcard-e7f47-default-rtdb.europe-west1.firebasedatabase.app/");
}
// Arayüz elemanlarını programatik olarak oluşturma
private void InitializeUI()
{
// DataGridView
dataGridView = new DataGridView { Dock = DockStyle.Top, Height = 250 };
Controls.Add(dataGridView);
// TextBoxes (PlaceholderText yerine geçici çözüm)
txtTitle = CreateTextBox("Title", 10, 260);
txtCategory = CreateTextBox("Category", 130, 260);
txtContent = CreateTextBox("Content", 250, 260);
txtBackgroundColor = CreateTextBox("Background Color", 370, 260);
// CheckBoxes
chkFavorite = new CheckBox { Text = "Favorite", Top = 260, Left = 490 };
chkPinned = new CheckBox { Text = "Pinned", Top = 260, Left = 560 };
Controls.Add(chkFavorite);
Controls.Add(chkPinned);
// Buttons
btnAdd = new Button { Text = "Add", Top = 300, Left = 10 };
btnUpdate = new Button { Text = "Update", Top = 300, Left = 80 };
btnDelete = new Button { Text = "Delete", Top = 300, Left = 150 };
btnAdd.Click += new EventHandler(btnAdd_Click);
btnUpdate.Click += new EventHandler(btnUpdate_Click);
btnDelete.Click += new EventHandler(btnDelete_Click);
Controls.Add(btnAdd);
Controls.Add(btnUpdate);
Controls.Add(btnDelete);
// StatusBar
statusBar = new StatusBar();
Controls.Add(statusBar);
}
// Placeholder metni simüle eden TextBox oluşturucu
private TextBox CreateTextBox(string placeholder, int left, int top)
{
TextBox textBox = new TextBox { Left = left, Top = top, Width = 100, Text = placeholder };
textBox.GotFocus += (sender, e) =>
{
if (textBox.Text == placeholder) textBox.Text = "";
};
textBox.LostFocus += (sender, e) =>
{
if (string.IsNullOrWhiteSpace(textBox.Text)) textBox.Text = placeholder;
};
Controls.Add(textBox);
return textBox;
}
// Form yüklendiğinde çalışacak işlemler
private async void MainForm_Load(object sender, EventArgs e)
{
await LoadDataAsync(); // Verileri Firebase'den yükleyin
RealTimeListener(); // Firebase Realtime Database ile dinleyici başlatın
}
// Verileri DataGridView'de yükleme
private async Task LoadDataAsync()
{
var data = await firebaseClient
.Child("flashcards")
.OnceAsync<Flashcard>();
// DataGridView'i temizleyin
dataGridView.Rows.Clear();
dataGridView.Columns.Clear();
// Sütunları oluşturma (JSON yapısına göre)
dataGridView.Columns.Add("id", "ID");
dataGridView.Columns.Add("title", "Title");
dataGridView.Columns.Add("category", "Category");
dataGridView.Columns.Add("content", "Content");
dataGridView.Columns.Add("favorite", "Favorite");
dataGridView.Columns.Add("pinned", "Pinned");
dataGridView.Columns.Add("timestamp", "Timestamp");
dataGridView.Columns.Add("backgroundColor", "Background Color");
// Verileri ekleme
foreach (var item in data)
{
dataGridView.Rows.Add(
item.Object.id,
item.Object.title,
item.Object.category,
item.Object.content,
item.Object.favorite,
item.Object.pinned,
item.Object.timestamp,
item.Object.backgroundColor
);
}
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {data.Count}";
}
// Veri ekleme
private async void AddData(Flashcard newFlashcard)
{
await firebaseClient
.Child("flashcards")
.PostAsync(newFlashcard);
await LoadDataAsync(); // Eklemeden sonra verileri tekrar yükleme
}
// Veri güncelleme
private async void UpdateData(string key, Flashcard updatedFlashcard)
{
await firebaseClient
.Child("flashcards")
.Child(key)
.PutAsync(updatedFlashcard);
await LoadDataAsync(); // Güncellemeden sonra verileri tekrar yükleme
}
// Veri silme
private async void DeleteData(string key)
{
await firebaseClient
.Child("flashcards")
.Child(key)
.DeleteAsync();
await LoadDataAsync(); // Silmeden sonra verileri tekrar yükleme
}
// Gerçek zamanlı dinleyici
private void RealTimeListener()
{
firebaseClient
.Child("flashcards")
.AsObservable<Flashcard>()
.Subscribe(d =>
{
// Verilerde değişiklik olduğunda otomatik güncelleme
Invoke((Action)(async () => await LoadDataAsync())); // UI thread'ine geri döner
});
}
// Ekle butonu click olayı
private void btnAdd_Click(object sender, EventArgs e)
{
var newFlashcard = new Flashcard
{
id = Guid.NewGuid().ToString(),
title = txtTitle.Text,
category = txtCategory.Text,
content = txtContent.Text,
favorite = chkFavorite.Checked,
pinned = chkPinned.Checked,
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
backgroundColor = txtBackgroundColor.Text
};
AddData(newFlashcard);
}
// Güncelle butonu click olayı
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
var selectedRow = dataGridView.SelectedRows[0];
var key = selectedRow.Cells["id"].Value.ToString(); // Firebase'deki anahtarı kullanın
var updatedFlashcard = new Flashcard
{
id = key,
title = selectedRow.Cells["title"].Value.ToString(),
category = selectedRow.Cells["category"].Value.ToString(),
content = selectedRow.Cells["content"].Value.ToString(),
favorite = (bool)selectedRow.Cells["favorite"].Value,
pinned = (bool)selectedRow.Cells["pinned"].Value,
timestamp = long.Parse(selectedRow.Cells["timestamp"].Value.ToString()),
backgroundColor = selectedRow.Cells["backgroundColor"].Value.ToString()
};
UpdateData(key, updatedFlashcard);
}
}
// Silme butonu click olayı
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
var selectedRow = dataGridView.SelectedRows[0];
var key = selectedRow.Cells["id"].Value.ToString(); // Firebase'deki anahtarı kullanın
DeleteData(key);
}
}
}
// Flashcard modeli
public class Flashcard
{
public string id { get; set; }
public string title { get; set; }
public string category { get; set; }
public string content { get; set; }
public bool favorite { get; set; }
public bool pinned { get; set; }
public long timestamp { get; set; }
public string backgroundColor { get; set; }
}
}
Merhaba! Sağladığınız kodu inceledim ve belirttiğiniz işlevsellik sorunlarını çözmek için bazı düzeltmeler ve iyileştirmeler yapmamız gerekecek. Aşağıda, mevcut sorunları belirleyip, bunları nasıl düzeltebileceğinizi ve performansı artırmak için neler yapabileceğinizi adım adım açıklayacağım.
Ana Sorunlar ve Çözümleri
Firebase Anahtarlarının Yönetimi:
Firebase Realtime Database, her veri ekleme işleminde benzersiz bir anahtar (key) oluşturur. Bu anahtarı doğru şekilde yönetmezsek, verileri güncelleme veya silme işlemleri sorun çıkarabilir.
Çözüm: Her Flashcard nesnesine Firebase tarafından oluşturulan key'i ekleyin ve bu key'i güncelleme/silme işlemlerinde kullanın.
DataGridView’in Verimli Güncellenmesi:
Mevcut kodda, veritabanında herhangi bir değişiklik olduğunda tüm DataGridView yenileniyor. Bu, performans açısından verimsizdir.
Çözüm: Sadece değişiklik yapılan satırı güncelleyin, ekleyin veya silin.
Olayların Doğru Bağlanması:
MainForm_Load olayı kodda tanımlanmış ancak forma yüklenirken tetiklenip tetiklenmediği net değil.
Firebase Admin SDK ve FirebaseClient birlikte kullanılırken doğru yapılandırılmalıdır.
Çözüm: FirebaseClient, genellikle istemci tarafında kullanılır. Firebase Admin SDK ise sunucu taraflı işlemler için uygundur. Projede sadece FirebaseClient kullanarak işlemleri yönetmek daha uygun olabilir.
Adım Adım Düzeltmeler
Aşağıda, yukarıdaki sorunları gideren ve performansı artıran güncellenmiş bir kod örneği bulabilirsiniz:
1. Flashcard Modeline Firebase Key Eklemek
Öncelikle, her Flashcard nesnesine Firebase tarafından oluşturulan benzersiz bir key ekleyelim. Bu key, güncelleme ve silme işlemleri için önemlidir.
csharp
public class Flashcard
{
public string Key { get; set; } // Firebase tarafından oluşturulan key
public string id { get; set; }
public string title { get; set; }
public string category { get; set; }
public string content { get; set; }
public bool favorite { get; set; }
public bool pinned { get; set; }
public long timestamp { get; set; }
public string backgroundColor { get; set; }
}
2. Initialization ve Load Event’in Bağlanması
Form yüklenirken MainForm_Load olayının tetiklendiğinden emin olalım.
csharp
public MainForm()
{
InitializeComponent(); // Form tasarımı yüklenecek
InitializeFirebase(); // Firebase Admin SDK başlatma
InitializeUI(); // Arayüz elemanlarını programatik olarak oluşturma
this.Load += MainForm_Load; // Load olayını bağlama
}
3. FirebaseClient Kullanımı ve Verilerin Yüklenmesi
FirebaseClient kullanarak verileri yüklerken, her veri parçasının Firebase key'ini alalım ve Flashcard nesnesine ekleyelim.
csharp
private async Task LoadDataAsync()
{
var data = await firebaseClient
.Child("flashcards")
.OnceAsync<Flashcard>();
// DataGridView'i temizleyin
dataGridView.Rows.Clear();
foreach (var item in data)
{
var flashcard = item.Object;
flashcard.Key = item.Key; // Firebase key’i ayarla
dataGridView.Rows.Add(
flashcard.Key, // Yeni bir sütun olarak key ekleyelim
flashcard.title,
flashcard.category,
flashcard.content,
flashcard.favorite,
flashcard.pinned,
flashcard.timestamp,
flashcard.backgroundColor
);
}
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {data.Count}";
}
Not: DataGridView'de Firebase key’ini saklamak için bir sütun ekleyebilirsiniz veya satırın Tag özelliğini kullanabilirsiniz.
4. DataGridView Tasarımı
DataGridView’in sütunlarını sadece bir kez tanımlayalım ve her veri değiştiğinde sadece ilgili satırı güncelleyelim.
csharp
private void InitializeUI()
{
// DataGridView
dataGridView = new DataGridView
{
Dock = DockStyle.Top,
Height = 250,
AllowUserToAddRows = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
MultiSelect = false
};
Controls.Add(dataGridView);
// Sütunları oluşturma (JSON yapısına göre)
dataGridView.Columns.Add("key", "Key"); // Firebase key sütunu
dataGridView.Columns["key"].Visible = false; // Kullanıcıya göstermeye gerek yok
dataGridView.Columns.Add("title", "Title");
dataGridView.Columns.Add("category", "Category");
dataGridView.Columns.Add("content", "Content");
dataGridView.Columns.Add("favorite", "Favorite");
dataGridView.Columns.Add("pinned", "Pinned");
dataGridView.Columns.Add("timestamp", "Timestamp");
dataGridView.Columns.Add("backgroundColor", "Background Color");
// Diğer UI elemanları (TextBoxes, CheckBoxes, Buttons, StatusBar)
// TextBoxes (PlaceholderText yerine geçici çözüm)
txtTitle = CreateTextBox("Title", 10, 260);
txtCategory = CreateTextBox("Category", 130, 260);
txtContent = CreateTextBox("Content", 250, 260);
txtBackgroundColor = CreateTextBox("Background Color", 370, 260);
// CheckBoxes
chkFavorite = new CheckBox { Text = "Favorite", Top = 260, Left = 490 };
chkPinned = new CheckBox { Text = "Pinned", Top = 260, Left = 560 };
Controls.Add(chkFavorite);
Controls.Add(chkPinned);
// Buttons
btnAdd = new Button { Text = "Add", Top = 300, Left = 10 };
btnUpdate = new Button { Text = "Update", Top = 300, Left = 80 };
btnDelete = new Button { Text = "Delete", Top = 300, Left = 150 };
btnAdd.Click += new EventHandler(btnAdd_Click);
btnUpdate.Click += new EventHandler(btnUpdate_Click);
btnDelete.Click += new EventHandler(btnDelete_Click);
Controls.Add(btnAdd);
Controls.Add(btnUpdate);
Controls.Add(btnDelete);
// StatusBar
statusBar = new StatusBar();
Controls.Add(statusBar);
// DataGridView Selection Changed Event
dataGridView.SelectionChanged += DataGridView_SelectionChanged;
}
5. Ekleme İşlemi
Veri eklerken Firebase'den otomatik oluşturulan key'i alabilir ve Flashcard nesnesine ekleyebiliriz.
csharp
private async void AddData(Flashcard newFlashcard)
{
var result = await firebaseClient
.Child("flashcards")
.PostAsync(newFlashcard);
newFlashcard.Key = result.Key; // Firebase tarafından oluşturulan key
// DataGridView'e yeni satır ekleme
dataGridView.Rows.Add(
newFlashcard.Key,
newFlashcard.title,
newFlashcard.category,
newFlashcard.content,
newFlashcard.favorite,
newFlashcard.pinned,
newFlashcard.timestamp,
newFlashcard.backgroundColor
);
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {dataGridView.Rows.Count}";
}
Aşağıda, yukarıdaki tüm düzeltmeleri içeren tamamlanmış bir kod örneği bulabilirsiniz:
csharp
Açıklamalar ve İpuçları
Firebase Key Yönetimi:
Flashcard nesnesine Key özelliği ekledik ve bunu Firebase'den gelen key ile dolduruyoruz. Bu sayede her işlemi doğru veri üzerine uygulayabiliriz.
DataGridView Performansı:
DataGridView’in sütunları sadece bir kez oluşturuluyor. Veri yüklenirken tüm satırlar temizlenip tekrar ekleniyor. Gerçek zamanlı dinleyici ile sadece ilgili satırları güncelleyerek performansı artırıyoruz.
Olayların Doğru Bağlanması:
this.Load += MainForm_Load; ile form yüklendiğinde MainForm_Load olayını tetikliyoruz.
Gerçek Zamanlı Dinleyici:
Firebase'de herhangi bir veri değişikliği olduğunda (ekleme, güncelleme, silme) sadece ilgili satırı güncelliyoruz veya ekliyoruz/siliyoruz. Bu sayede tüm DataGridView’i yeniden yüklemek yerine sadece gerektiği kadarını güncelliyoruz.
UI Elemanlarının Senkronizasyonu:
Kullanıcı bir satır seçtiğinde, ilgili veriler TextBox ve CheckBox’lara yansıtılıyor. Bu sayede kullanıcı güncelleme yaparken mevcut veriyi görebiliyor.
Hataların Giderilmesi:
Kodda olası null referans hatalarının önüne geçmek için bazı kontroller ekledik (örneğin, selectedRow.Cells["id"]?.Value?.ToString()).
Sonuç
Yukarıdaki düzeltmeler ve iyileştirmeler ile uygulamanızın Firebase ile olan etkileşimini daha stabil ve performanslı hale getirebilirsiniz. Firebase key'lerini doğru yönetmek ve DataGridView'i efektif bir şekilde güncellemek, uygulamanızın performansını büyük ölçüde artıracaktır.
Herhangi bir adımda takıldığınız veya daha fazla yardıma ihtiyaç duyduğunuzda çekinmeden sorabilirsiniz!
Not yet included in a published release.
using System;
using System.Windows.Forms;
using Firebase.Database;
using Firebase.Database.Query;
using Google.Apis.Auth.OAuth2;
using FirebaseAdmin;
using FirebaseAdmin.Messaging;
using System.Threading.Tasks;
namespace Flashcard
{
public partial class MainForm : Form
{
private FirebaseClient firebaseClient;
private DataGridView dataGridView;
private TextBox txtTitle, txtCategory, txtContent, txtBackgroundColor;
private CheckBox chkFavorite, chkPinned;
private Button btnAdd, btnUpdate, btnDelete;
private StatusBar statusBar;
public MainForm()
{
InitializeComponent(); // Form tasarımı yüklenecek
InitializeFirebase(); // Firebase Admin SDK başlatma
InitializeUI(); // Arayüz elemanlarını programatik olarak oluşturma
this.Load += MainForm_Load; // Load olayını bağlama
}
// Firebase Client başlatma
private void InitializeFirebase()
{
// Firebase Admin SDK genellikle sunucu tarafında kullanılır. Burada sadece FirebaseClient kullanacağız.
firebaseClient = new FirebaseClient("https://flashcard-e7f47-default-rtdb.europe-west1.firebasedatabase.app/");
}
// Arayüz elemanlarını programatik olarak oluşturma
private void InitializeUI()
{
// DataGridView
dataGridView = new DataGridView
{
Dock = DockStyle.Top,
Height = 250,
AllowUserToAddRows = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
MultiSelect = false
};
Controls.Add(dataGridView);
// Sütunları oluşturma
dataGridView.Columns.Add("key", "Key");
dataGridView.Columns["key"].Visible = false; // Kullanıcıya göstermeye gerek yok
dataGridView.Columns.Add("title", "Title");
dataGridView.Columns.Add("category", "Category");
dataGridView.Columns.Add("content", "Content");
dataGridView.Columns.Add("favorite", "Favorite");
dataGridView.Columns.Add("pinned", "Pinned");
dataGridView.Columns.Add("timestamp", "Timestamp");
dataGridView.Columns.Add("backgroundColor", "Background Color");
// TextBoxes (PlaceholderText yerine geçici çözüm)
txtTitle = CreateTextBox("Title", 10, 260);
txtCategory = CreateTextBox("Category", 130, 260);
txtContent = CreateTextBox("Content", 250, 260);
txtBackgroundColor = CreateTextBox("Background Color", 370, 260);
// CheckBoxes
chkFavorite = new CheckBox { Text = "Favorite", Top = 260, Left = 490 };
chkPinned = new CheckBox { Text = "Pinned", Top = 260, Left = 560 };
Controls.Add(chkFavorite);
Controls.Add(chkPinned);
// Buttons
btnAdd = new Button { Text = "Add", Top = 300, Left = 10 };
btnUpdate = new Button { Text = "Update", Top = 300, Left = 80 };
btnDelete = new Button { Text = "Delete", Top = 300, Left = 150 };
btnAdd.Click += new EventHandler(btnAdd_Click);
btnUpdate.Click += new EventHandler(btnUpdate_Click);
btnDelete.Click += new EventHandler(btnDelete_Click);
Controls.Add(btnAdd);
Controls.Add(btnUpdate);
Controls.Add(btnDelete);
// StatusBar
statusBar = new StatusBar();
Controls.Add(statusBar);
// DataGridView Selection Changed Event
dataGridView.SelectionChanged += DataGridView_SelectionChanged;
}
// Placeholder metni simüle eden TextBox oluşturucu
private TextBox CreateTextBox(string placeholder, int left, int top)
{
TextBox textBox = new TextBox { Left = left, Top = top, Width = 100, Text = placeholder };
textBox.GotFocus += (sender, e) =>
{
if (textBox.Text == placeholder) textBox.Text = "";
};
textBox.LostFocus += (sender, e) =>
{
if (string.IsNullOrWhiteSpace(textBox.Text)) textBox.Text = placeholder;
};
Controls.Add(textBox);
return textBox;
}
// Form yüklendiğinde çalışacak işlemler
private async void MainForm_Load(object sender, EventArgs e)
{
await LoadDataAsync(); // Verileri Firebase'den yükleyin
RealTimeListener(); // Firebase Realtime Database ile dinleyici başlatın
}
// Verileri DataGridView'de yükleme
private async Task LoadDataAsync()
{
var data = await firebaseClient
.Child("flashcards")
.OnceAsync<Flashcard>();
// DataGridView'i temizleyin
dataGridView.Rows.Clear();
foreach (var item in data)
{
var flashcard = item.Object;
flashcard.Key = item.Key; // Firebase key’i ayarla
dataGridView.Rows.Add(
flashcard.Key,
flashcard.title,
flashcard.category,
flashcard.content,
flashcard.favorite,
flashcard.pinned,
flashcard.timestamp,
flashcard.backgroundColor
);
}
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {data.Count}";
}
// Veri ekleme
private async void AddData(Flashcard newFlashcard)
{
var result = await firebaseClient
.Child("flashcards")
.PostAsync(newFlashcard);
newFlashcard.Key = result.Key; // Firebase tarafından oluşturulan key
// DataGridView'e yeni satır ekleme
dataGridView.Rows.Add(
newFlashcard.Key,
newFlashcard.title,
newFlashcard.category,
newFlashcard.content,
newFlashcard.favorite,
newFlashcard.pinned,
newFlashcard.timestamp,
newFlashcard.backgroundColor
);
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {dataGridView.Rows.Count}";
}
// Veri güncelleme
private async void UpdateData(string key, Flashcard updatedFlashcard)
{
await firebaseClient
.Child("flashcards")
.Child(key)
.PutAsync(updatedFlashcard);
// DataGridView'de satırı bul ve güncelle
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["key"].Value.ToString() == key)
{
row.Cells["title"].Value = updatedFlashcard.title;
row.Cells["category"].Value = updatedFlashcard.category;
row.Cells["content"].Value = updatedFlashcard.content;
row.Cells["favorite"].Value = updatedFlashcard.favorite;
row.Cells["pinned"].Value = updatedFlashcard.pinned;
row.Cells["timestamp"].Value = updatedFlashcard.timestamp;
row.Cells["backgroundColor"].Value = updatedFlashcard.backgroundColor;
break;
}
}
// StatusBar güncellemesi (isteğe bağlı)
}
// Veri silme
private async void DeleteData(string key)
{
await firebaseClient
.Child("flashcards")
.Child(key)
.DeleteAsync();
// DataGridView'den sil
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["key"].Value.ToString() == key)
{
dataGridView.Rows.Remove(row);
break;
}
}
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {dataGridView.Rows.Count}";
}
// Gerçek zamanlı dinleyici
private void RealTimeListener()
{
firebaseClient
.Child("flashcards")
.AsObservable<Flashcard>()
.Subscribe(d =>
{
if (d.EventType == Firebase.Database.Streaming.FirebaseEventType.Put ||
d.EventType == Firebase.Database.Streaming.FirebaseEventType.Post)
{
var flashcard = d.Object;
flashcard.Key = d.Key;
// DataGridView'de satırı bul veya ekle
bool exists = false;
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["key"].Value.ToString() == d.Key)
{
// Satır güncelle
row.Cells["title"].Value = flashcard.title;
row.Cells["category"].Value = flashcard.category;
row.Cells["content"].Value = flashcard.content;
row.Cells["favorite"].Value = flashcard.favorite;
row.Cells["pinned"].Value = flashcard.pinned;
row.Cells["timestamp"].Value = flashcard.timestamp;
row.Cells["backgroundColor"].Value = flashcard.backgroundColor;
exists = true;
break;
}
}
if (!exists)
{
// Yeni satır ekle
dataGridView.Rows.Add(
flashcard.Key,
flashcard.title,
flashcard.category,
flashcard.content,
flashcard.favorite,
flashcard.pinned,
flashcard.timestamp,
flashcard.backgroundColor
);
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {dataGridView.Rows.Count}";
}
}
else if (d.EventType == Firebase.Database.Streaming.FirebaseEventType.Delete)
{
// DataGridView'den sil
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["key"].Value.ToString() == d.Key)
{
dataGridView.Rows.Remove(row);
break;
}
}
// StatusBar güncellemesi
statusBar.Text = $"Total Items: {dataGridView.Rows.Count}";
}
});
}
// Ekle butonu click olayı
private void btnAdd_Click(object sender, EventArgs e)
{
var newFlashcard = new Flashcard
{
id = Guid.NewGuid().ToString(),
title = txtTitle.Text,
category = txtCategory.Text,
content = txtContent.Text,
favorite = chkFavorite.Checked,
pinned = chkPinned.Checked,
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
backgroundColor = txtBackgroundColor.Text
};
AddData(newFlashcard);
}
// Güncelle butonu click olayı
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
var selectedRow = dataGridView.SelectedRows[0];
var key = selectedRow.Cells["key"].Value.ToString(); // Firebase'deki anahtarı kullanın
var updatedFlashcard = new Flashcard
{
Key = key,
id = selectedRow.Cells["id"]?.Value?.ToString() ?? Guid.NewGuid().ToString(),
title = txtTitle.Text,
category = txtCategory.Text,
content = txtContent.Text,
favorite = chkFavorite.Checked,
pinned = chkPinned.Checked,
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
backgroundColor = txtBackgroundColor.Text
};
UpdateData(key, updatedFlashcard);
}
}
// Silme butonu click olayı
private void btnDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
var selectedRow = dataGridView.SelectedRows[0];
var key = selectedRow.Cells["key"].Value.ToString(); // Firebase'deki anahtarı kullanın
DeleteData(key);
}
}
// DataGridView'de seçim değiştiğinde form elemanlarını güncelle
private void DataGridView_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
var selectedRow = dataGridView.SelectedRows[0];
txtTitle.Text = selectedRow.Cells["title"].Value.ToString();
txtCategory.Text = selectedRow.Cells["category"].Value.ToString();
txtContent.Text = selectedRow.Cells["content"].Value.ToString();
txtBackgroundColor.Text = selectedRow.Cells["backgroundColor"].Value.ToString();
chkFavorite.Checked = Convert.ToBoolean(selectedRow.Cells["favorite"].Value);
chkPinned.Checked = Convert.ToBoolean(selectedRow.Cells["pinned"].Value);
}
}
}
// Flashcard modeli
public class Flashcard
{
public string Key { get; set; } // Firebase tarafından oluşturulan key
public string id { get; set; }
public string title { get; set; }
public string category { get; set; }
public string content { get; set; }
public bool favorite { get; set; }
public bool pinned { get; set; }
public long timestamp { get; set; }
public string backgroundColor { get; set; }
}
}