C# 尽管 radiobutton 的 checked 属性设置为 false,但在运行时,它显示为选中。

huangapple go评论69阅读模式
英文:

C# Although radiobuton checked=false, when I run it, it comes as ticked

问题

In your code, you are trying to set the Checked property of radioButton1 to false in several places, such as the Form1 constructor and the YeniOyun method. However, it seems that the radio button is still checked when you run the program.

One possible reason for this behavior could be that the radio button's state is being affected by other code or user interactions. To ensure that radioButton1 is unchecked, you can follow these steps:

  1. Double-check in the Form Designer: Open the Windows Forms designer, select radioButton1, and make sure that its Checked property is set to false in the Properties window.

  2. Remove unnecessary code: You have multiple places where you are trying to set radioButton1.Checked to false. To avoid potential conflicts, you can remove some of these redundant calls. Keep only one place where you set it to false, which should be sufficient. For example, you can remove it from the Form1constructor and only leave it in theYeniOyun` method.

  3. Debugging: You can add breakpoints and debug your code to see if there are any unexpected changes to the Checked property during program execution. This will help you identify any unwanted side effects.

If, after following these steps, the issue still persists, there may be other factors in your code or your development environment that are causing the problem. You should carefully review your code and verify the design of your form in the Windows Forms designer to ensure that the radio button's Checked property is set correctly to false.

英文:

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

答案1

得分: 0

如果您将RadioButtons放在表单上,它将根据设计自动选择具有最低Tab索引的选项(因为1 "应始终被选中")。

然而,如果您将它们放置在容器控件中,如Panel或Group,您需要手动设置应该选择哪一个。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月24日 16:44:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定