I'm trying to make simple race game in c#. I have 4 radio buttons and their checked
property is false. When I look to my code, I tried something like radioButton1.checked=true
but when I run the program, the radiobutton1 is checked. How can I set it to false?
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 at_yarisi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
}
int uzaklik1, uzaklik2, uzaklik3, uzaklik4, tahmin, skor = 0;
Random x = new Random();
private void YeniOyun()
{
timer1.Enabled= false;
timer2.Enabled= false;
pictureBox1.Left = 0;
pictureBox2.Left = 0;
pictureBox3.Left = 0;
pictureBox4.Left = 0;
label41.Text = "--";
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
int genis1 = pictureBox1.Width;
int genis2 = pictureBox2.Width;
int genis3 = pictureBox3.Width;
int genis4 = pictureBox4.Width;
pictureBox1.Left += x.Next(15, 20);
pictureBox2.Left += x.Next(15, 20);
pictureBox3.Left += x.Next(15, 20);
pictureBox4.Left += x.Next(15, 20);
int bitis = label30.Left;
// Yarıştırılan arabalar arasındaki en büyük uzaklığı hesapla
int enBuyukUzaklik = Math.Max(Math.Max(pictureBox1.Left, pictureBox2.Left), Math.Max(pictureBox3.Left, pictureBox4.Left));
// Hangi arabaların en önde olduğunu kontrol et
List<int> ondekiArabalar = new List<int>();
if (pictureBox1.Left == enBuyukUzaklik) ondekiArabalar.Add(1);
if (pictureBox2.Left == enBuyukUzaklik) ondekiArabalar.Add(2);
if (pictureBox3.Left == enBuyukUzaklik) ondekiArabalar.Add(3);
if (pictureBox4.Left == enBuyukUzaklik) ondekiArabalar.Add(4);
// Label'ı güncelle
label41.Text = string.Join(", ", ondekiArabalar);
if (genis1 + pictureBox1.Left >= bitis && genis2 + pictureBox2.Left != bitis && genis3 + pictureBox3.Left != bitis && genis4 + pictureBox4.Left != bitis)
{
timer1.Enabled = false;
MessageBox.Show("The Cheetah won the race");
if (tahmin == 1)
{
skor += 1;
label40.Text = skor.ToString();
}
}
else if (genis1 + pictureBox1.Left != bitis && genis2 + pictureBox2.Left >= bitis && genis3 + pictureBox3.Left != bitis && genis4 + pictureBox4.Left != bitis)
{
timer1.Enabled = false;
MessageBox.Show("The Car won the race");
if (tahmin == 2)
{
skor += 1;
label40.Text = skor.ToString();
}
}
else if (genis1 + pictureBox1.Left != bitis && genis2 + pictureBox2.Left != bitis && genis3 + pictureBox3.Left >= bitis && genis4 + pictureBox4.Left != bitis)
{
timer1.Enabled = false;
MessageBox.Show("The Racer won the race");
if (tahmin == 3)
{
skor += 1;
label40.Text = skor.ToString();
}
}
else if (genis1 + pictureBox1.Left != bitis && genis2 + pictureBox2.Left != bitis && genis3 + pictureBox3.Left != bitis && genis4 + pictureBox4.Left >= bitis)
{
timer1.Enabled = false;
MessageBox.Show("The Jet won the race");
if (tahmin == 4)
{
skor += 1;
label40.Text = skor.ToString();
}
}
if (pictureBox1.Left > pictureBox2.Left + 10 && pictureBox1.Left > pictureBox3.Left + 10 && pictureBox1.Left > pictureBox4.Left + 10)
{
label41.Text = "1";
}
if (pictureBox2.Left > pictureBox1.Left + 10 && pictureBox2.Left > pictureBox3.Left + 10 && pictureBox2.Left > pictureBox4.Left + 10)
{
label41.Text = "2";
}
if (pictureBox3.Left > pictureBox1.Left + 10 && pictureBox3.Left > pictureBox2.Left + 10 && pictureBox3.Left > pictureBox4.Left + 10)
{
label41.Text = "3";
}
if (pictureBox4.Left > pictureBox1.Left + 10 && pictureBox4.Left > pictureBox2.Left + 10 && pictureBox4.Left > pictureBox3.Left + 10)
{
label41.Text = "4";
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
tahmin = 2;
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked)
{
tahmin = 3;
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
if (radioButton4.Checked)
{
tahmin = 4;
}
}
private void button6_Click(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
}
private void button8_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
YeniOyun();
}
private void button1_Click_2(object sender, EventArgs e)
{
// Trafik ışığı kodu başlangıcı
if (timer2.Enabled)
{
MessageBox.Show("You cannot start a new transaction until the previous transaction is completed.");
return;
}
label1.Text = "Kırmızı";
label1.BackColor = Color.Red;
timer2.Enabled = true;
// Trafik ışığı kodu sonu
}
private void button2_Click_1(object sender, EventArgs e)
{
YeniOyun();
}
private void label30_Click(object sender, EventArgs e)
{
}
private void pictureBox4_Click(object sender, EventArgs e)
{
}
private void label25_Click(object sender, EventArgs e)
{
}
private void timer2_Tick(object sender, EventArgs e)
{
label1.BackColor = Color.Transparent;
if (label1.Text == "Kırmızı")
{
label1.Text = "Yeşil";
label1.BackColor = Color.Green;
timer1.Enabled = true;
timer2.Enabled = false;
}
else
{
label1.Text = "Kırmızı";
label1.BackColor = Color.Red;
}
}
private void button5_Click(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
tahmin = 1;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
timer1.Enabled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.Transparent;
pictureBox2.BackColor = Color.Transparent;
pictureBox3.BackColor = Color.Transparent;
pictureBox4.BackColor = Color.Transparent;
uzaklik1 = pictureBox1.Left;
uzaklik2 = pictureBox2.Left;
uzaklik3 = pictureBox3.Left;
uzaklik4 = pictureBox4.Left;
button1.BackgroundImageLayout = ImageLayout.Stretch;
button1.BackColor = Color.Transparent;
button1.FlatStyle = FlatStyle.Flat;
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackColor = Color.Transparent;
button2.FlatStyle = FlatStyle.Flat;
button1.FlatAppearance.MouseDownBackColor = Color.Transparent;
button1.FlatAppearance.MouseOverBackColor = Color.Transparent;
button2.FlatAppearance.MouseDownBackColor = Color.Transparent;
button2.FlatAppearance.MouseOverBackColor = Color.Transparent;
button1.FlatAppearance.BorderSize = 0;
button2.FlatAppearance.BorderSize = 0;
}
private void label65_Click(object sender, EventArgs e)
{
}
}
}
I tried to write radiobutton1.checked=false
in form load event and in properties, i verified if checked
property is false. but both didn't work
If you place the RadioButtons on the form it will autoselect the one with lowest Tab-index by design (because 1 "should always be selected")
However if you place them as child items inside a container control like the Panel or Group you need to manually set which one that should be selected.