С# Windows forms, багов нет но форма пустая, в чем проблема, как исправить?
Код везде показывается корректным, формы запускаются однако пустые.
form1
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace QuizApp
{
public partial class Form1 : Form
{
private string connectionString = "Data Source=BUCK-TICK;Initial Catalog=QuizDB;Integrated Security=True";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadQuestions();
}
private void LoadQuestions()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Questions", connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
questionsGridView.DataSource = dataTable;
}
}
private void addButton_Click(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(
"INSERT INTO Questions (Question, Option1, Option2, Option3, Option4, CorrectOption) VALUES (@Question, @Option1, @Option2, @Option3, @Option4, @CorrectOption)",
connection);
command.Parameters.AddWithValue("@Question", questionTextBox.Text);
command.Parameters.AddWithValue("@Option1", option1TextBox.Text);
command.Parameters.AddWithValue("@Option2", option2TextBox.Text);
command.Parameters.AddWithValue("@Option3", option3TextBox.Text);
command.Parameters.AddWithValue("@Option4", option4TextBox.Text);
command.Parameters.AddWithValue("@CorrectOption", correctOptionComboBox.SelectedIndex + 1);
command.ExecuteNonQuery();
MessageBox.Show("Вопрос добавлен!");
LoadQuestions();
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (questionsGridView.SelectedRows.Count > 0)
{
int id = Convert.ToInt32(questionsGridView.SelectedRows[0].Cells["Id"].Value);
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("DELETE FROM Questions WHERE Id = @Id", connection);
command.Parameters.AddWithValue("@Id", id);
command.ExecuteNonQuery();
}
MessageBox.Show("Вопрос удален!");
LoadQuestions();
}
else
{
MessageBox.Show("Выберите вопрос для удаления.");
}
}
}
}
form1.Designer
namespace QuizApp
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.DataGridView questionsGridView;
private System.Windows.Forms.TextBox questionTextBox;
private System.Windows.Forms.TextBox option1TextBox;
private System.Windows.Forms.TextBox option2TextBox;
private System.Windows.Forms.TextBox option3TextBox;
private System.Windows.Forms.TextBox option4TextBox;
private System.Windows.Forms.ComboBox correctOptionComboBox;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button deleteButton;
private void InitializeComponent()
{
this.questionsGridView = new System.Windows.Forms.DataGridView();
this.questionTextBox = new System.Windows.Forms.TextBox();
this.option1TextBox = new System.Windows.Forms.TextBox();
this.option2TextBox = new System.Windows.Forms.TextBox();
this.option3TextBox = new System.Windows.Forms.TextBox();
this.option4TextBox = new System.Windows.Forms.TextBox();
this.correctOptionComboBox = new System.Windows.Forms.ComboBox();
this.addButton = new System.Windows.Forms.Button();
this.deleteButton = new System.Windows.Forms.Button();
// Add Button
this.addButton.Text = "Добавить";
this.addButton.Click += new System.EventHandler(this.addButton_Click);
// Delete Button
this.deleteButton.Text = "Удалить";
this.deleteButton.Click += new System.EventHandler(this.deleteButton_Click);
// Arrange Controls
// Add controls to form
this.Controls.Add(this.questionsGridView);
this.Controls.Add(this.questionTextBox);
this.Controls.Add(this.option1TextBox);
this.Controls.Add(this.option2TextBox);
this.Controls.Add(this.option3TextBox);
this.Controls.Add(this.option4TextBox);
this.Controls.Add(this.correctOptionComboBox);
this.Controls.Add(this.addButton);
this.Controls.Add(this.deleteButton);
}
}
}
QuizForm
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace QuizApp
{
public partial class QuizForm : Form
{
private string connectionString = "Data Source=BUCK-TICK;Initial Catalog=QuizDB;Integrated Security=True";
private int currentQuestionIndex = 0;
private DataTable questions;
public QuizForm()
{
InitializeComponent();
}
private void QuizForm_Load(object sender, EventArgs e)
{
LoadQuestions();
ShowQuestion();
}
private void LoadQuestions()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Questions", connection);
questions = new DataTable();
adapter.Fill(questions);
}
}
private void ShowQuestion()
{
if (currentQuestionIndex < questions.Rows.Count)
{
DataRow question = questions.Rows[currentQuestionIndex];
questionLabel.Text = question["Question"].ToString();
option1RadioButton.Text = question["Option1"].ToString();
option2RadioButton.Text = question["Option2"].ToString();
option3RadioButton.Text = question["Option3"].ToString();
option4RadioButton.Text = question["Option4"].ToString();
}
else
{
MessageBox.Show("Викторина окончена!");
this.Close();
}
}
private void nextButton_Click(object sender, EventArgs e)
{
currentQuestionIndex++;
ShowQuestion();
}
}
}
QuizForm.Designer
using System;
using System.Windows.Forms;
namespace QuizApp
{
partial class QuizForm
{
private Label questionLabel;
private RadioButton option1RadioButton;
private RadioButton option2RadioButton;
private RadioButton option3RadioButton;
private RadioButton option4RadioButton;
private Button nextButton;
private void InitializeComponent()
{
this.questionLabel = new Label();
this.option1RadioButton = new RadioButton();
this.option2RadioButton = new RadioButton();
this.option3RadioButton = new RadioButton();
this.option4RadioButton = new RadioButton();
this.nextButton = new Button();
// Question Label
this.questionLabel.AutoSize = true;
this.questionLabel.Location = new System.Drawing.Point(20, 20);
this.questionLabel.Size = new System.Drawing.Size(300, 20);
this.questionLabel.Text = "Вопрос";
// Option 1
this.option1RadioButton.Location = new System.Drawing.Point(20, 60);
this.option1RadioButton.Size = new System.Drawing.Size(300, 20);
// Option 2
this.option2RadioButton.Location = new System.Drawing.Point(20, 100);
this.option2RadioButton.Size = new System.Drawing.Size(300, 20);
// Option 3
this.option3RadioButton.Location = new System.Drawing.Point(20, 140);
this.option3RadioButton.Size = new System.Drawing.Size(300, 20);
// Option 4
this.option4RadioButton.Location = new System.Drawing.Point(20, 180);
this.option4RadioButton.Size = new System.Drawing.Size(300, 20);
// Next Button
this.nextButton.Location = new System.Drawing.Point(20, 220);
this.nextButton.Size = new System.Drawing.Size(100, 30);
this.nextButton.Text = "Далее";
this.nextButton.Click += new EventHandler(nextButton_Click);
// QuizForm
this.ClientSize = new System.Drawing.Size(400, 300);
this.Controls.Add(this.questionLabel);
this.Controls.Add(this.option1RadioButton);
this.Controls.Add(this.option2RadioButton);
this.Controls.Add(this.option3RadioButton);
this.Controls.Add(this.option4RadioButton);
this.Controls.Add(this.nextButton);
this.Text = "Викторина";
this.Load += new EventHandler(QuizForm_Load);
}
}
}