• 締切済み

C#について・・・

次のようなブログラムなのですが、ラジオボタンが上手く切り替わりません。 どこを修正すると上手くいくでしょうか? <<文字数オーバーのため省略>> public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); if(isJapanStyle) { label5.Text=string.Format("0時0分0秒"); label6.Text=string.Format("0時0分0秒"); } else { label5.Text=string.Format("00:00:00"); label6.Text=string.Format("00:00:00"); } // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // <<文字数オーバーのため 省略>> static void Main() { Application.Run(new Form1()); } protected void button1_Click(object sender, System.EventArgs e) { if(isJapanStyle) { label5.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label5.Text=DateTime.Now.ToString("T"); } recTime=dt; timer2.Stop(); timer3.Start(); } private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, System.EventArgs e) { dt=DateTime.Now; if(isJapanStyle) { label4.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label4.Text=DateTime.Now.ToString("T"); } } private void timer3_Tick(object sender, System.EventArgs e) { keika=dt-recTime; if(isJapanStyle) { label6.Text=string.Format("{0}時間{1}分{2}秒",keika.Hours,keika.Minutes,keika.Seconds); } else { label6.Text=string.Format("{0}:{1}:{2}",keika.Hours,keika.Minutes,keika.Seconds); } } private void timer2_Tick(object sender, System.EventArgs e) { timer3.Stop(); } private void radioButton2_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=false; } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=true; } } }

みんなの回答

  • kuroooooo
  • ベストアンサー率47% (8/17)
回答No.1

質問の意図が読み取れないのでなんともいえませんが、 問題がラジオボタンを押してもisJapanStyleの値が正しくセット されない ということであれば private void radioButton2_CheckedChanged(object sender, System.EventArgs e) { isJanpanStyle=radioButton1.Checked; } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { isJanpanStyle=radioButton1.Checked; } でいいのでは?

tyamoro
質問者

お礼

ご質問しておきながら(補足まで送り)大変申し訳ないことですが、一晩寝て自己解決してしまいました。ありがとうポイントを付けさせていただきます。 完成したプログラムです <<文字オーバーのため一部省略> private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, System.EventArgs e) { dt=DateTime.Now; if(isJapanStyle) { label4.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label4.Text=DateTime.Now.ToString("T"); } } private void timer3_Tick(object sender, System.EventArgs e) { keika=dt-recTime; if(isJapanStyle) { label6.Text=string.Format("{0}時間{1}分{2}秒",keika.Hours,keika.Minutes,keika.Seconds); } else { label6.Text=string.Format("{0}:{1}:{2}",keika.Hours,keika.Minutes,keika.Seconds); } } private void timer2_Tick(object sender, System.EventArgs e) { timer3.Stop(); } private void radioButton2_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=false; ; } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=true; } private void timer4_Tick(object sender, System.EventArgs e) { if(isJapanStyle) { label5.Text=string.Format("{0}時{1}分{2}秒",recTime.Hour,recTime.Minute,recTime.Second); } else { label5.Text=DateTime.Now.ToString("T"); } } private void timer5_Tick(object sender, System.EventArgs e) { if(isJapanStyle) { label6.Text=string.Format("{0}時間{1}分{2}秒",keika.Hours,keika.Minutes,keika.Seconds); } else { label6.Text=string.Format("{0}:{1}:{2}",keika.Hours,keika.Minutes,keika.Seconds); } } }

tyamoro
質問者

補足

ラジオボタンの切り替え時にすべてのラベルが変更されません。 ラジオボタンを変更しボタン1をクリックすると ラベル4,5,6が変更できますが、ボタン1を押さず ラジオボタンの変更時に ラベル4,5,6を変更できないでしょうか?

関連するQ&A

  • C#のタイマーについて

    タイマーのプログラムを作っています。 namespace WindowsApplication3 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; protected System.Windows.Forms.Label label5; protected System.Windows.Forms.Label label6; protected System.Windows.Forms.Button button1; protected System.Windows.Forms.Button button2; protected DateTime dt; protected DateTime recTime; private System.Windows.Forms.GroupBox groupBox1; protected System.Windows.Forms.RadioButton radioButton1; protected System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.Timer timer1; private System.ComponentModel.IContainer components; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); DateTime dt; dt=DateTime.Now; string a="00"; string b=a; string c=a; label4.Text = string.Format("{0}時{1}分{2}秒", dt.Hour, dt.Minute,dt.Second); label5.Text = string.Format("{0}時{1}分{2}秒",a,b,c); label6.Text = string.Format("{0}時間{1}分{2}秒",a,b,c); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // dt=DateTime.Now; } /// <summary> /// 使用されているリソースに後処理を実行します。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows フォーム デザイナで生成されたコード /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 <<  文字数オーバーのため 省略>> /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } protected void timer1_Tick(object sender, System.EventArgs e) { DateTime dt; dt=DateTime.Now; label4.Text = string.Format("{0}時{1}分{2}秒", dt.Hour, dt.Minute,dt.Second); } protected void button1_Click(object sender, System.EventArgs e) { DateTime recTime; recTime=DateTime.Now; TimeSpan k; k=dt-recTime; label5.Text=string.Format("{0}時{1}分{2}秒",recTime.Hour,recTime.Minute,recTime.Second); label6.Text=string.Format("{0}秒",k.Seconds); } } } このプログラムでは ボタン1をクリックするとlabel6に経過時間 label5に記録時間を出力させたいのですが、TimeSpanクラスが上手く使えず、経過時間がマイナスになってしまいます。アドバイスお願いします。

  • C# 簡単なシューティング 自機移動について

    かなりの初心者で困っています。 簡単なシューティングを作ろうとおもっています。 使用ソフトはVisualC#2005です。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //自機(右移動) private void button3_Click(object sender, EventArgs e) { if (timer1.Enabled == false) { timer2.Enabled = false; timer1.Enabled = true; } else { timer1.Enabled = false; timer3.Enabled = false; timer4.Enabled = false; } } private void timer1_Tick(object sender, EventArgs e) { pictureBox1.Left = pictureBox1.Left + 2; } //自機(左) private void button4_Click(object sender, EventArgs e) { if (timer2.Enabled == false) { timer1.Enabled = false; timer2.Enabled = true; } else { timer2.Enabled = false; timer3.Enabled = false; timer4.Enabled = false; } } private void timer2_Tick(object sender, EventArgs e) { pictureBox1.Left = pictureBox1.Left - 2; } //自機(上) private void button1_Click(object sender, EventArgs e) { if (timer3.Enabled == false) { timer4.Enabled = false; timer3.Enabled = true; } else { timer1.Enabled = false; timer2.Enabled = false; timer3.Enabled = false; } } private void timer3_Tick(object sender, EventArgs e) { pictureBox1.Top = pictureBox1.Top - 2; } //自機(下) private void button2_Click(object sender, EventArgs e) { if (timer4.Enabled == false) { timer3.Enabled = false; timer4.Enabled = true; } else { timer1.Enabled = false; timer2.Enabled = false; timer4.Enabled = false; } } private void timer4_Tick(object sender, EventArgs e) { pictureBox1.Top = pictureBox1.Top + 2; } } } 自機は画像(PictureBox)でボタンによって上下左右に移動します。 FormのSizeは800,630です。 自機の移動がボタンなのでキー入力によって操作できるようにしたいのですが、 それと、自機の移動範囲を画面からでないようにしたいです。 あまりC#を理解できてない上でつくったのでおかしな点が多々あると思います。 教えていただける方がいると助かります。

  • listBoxとTimerについて C#

    listBoxとTimerについて C# ヤフーニュースのURLをlistboxへ入れます。 そしてlistBox1の中身を画面表示させたら、次にlistBox2の中身も表示したいと思っています。 最初listBoxが1つだったときはうまく行っていたのですが、listBoxを増やすとうまく行かなくなりました。 この状態だと、listBox2の中身だけ表示させて終わってしまいます。 Timerの使い方が怪しいと思うのですが、どうでしょうか? 些細なことでも何でもいいのでご意見頂ければ助かります。 -----以下コード抜粋------ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int urlindex = 0; private void Form1_Load(object sender, EventArgs e) { listBox1.AllowDrop = true; listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter); listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop); listBox2.AllowDrop = true; listBox2.DragEnter += new DragEventHandler(listBox2_DragEnter); listBox2.DragDrop += new DragEventHandler(listBox2_DragDrop); } private void listBox1_DragEnter(object sender, DragEventArgs e) { //URLのみ受け入れる//@ITより if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox2_DragEnter(object sender, DragEventArgs e) { //URLのみ受け入れる//@ITより if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox1_DragDrop(object sender, DragEventArgs e) { //ドロップされたリンクのURLを取得する//@ITより string url = e.Data.GetData(DataFormats.Text).ToString(); //結果を表示 listBox1.Text = url; //MessageBox.Show(url); //ドロップされたデータがstring型か調べる if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; //ドロップされたデータ(string型)を取得 string itemText = (string)e.Data.GetData(typeof(string)); //ドロップされたデータをリストボックスに追加する target.Items.Add(url); //MessageBox.Show("表示"); } } private void listBox2_DragDrop(object sender, DragEventArgs e) { //ドロップされたリンクのURLを取得する//@ITより string url = e.Data.GetData(DataFormats.Text).ToString(); //結果を表示 listBox2.Text = url; //MessageBox.Show(url); //ドロップされたデータがstring型か調べる if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; string itemText = (string)e.Data.GetData(typeof(string)); target.Items.Add(url); } } private void goButton_Click(object sender, EventArgs e) { urlindex = 0; timer1.Start(); timer2.Start(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); if (listBox1.Items.Count != 0 && urlindex < listBox1.Items.Count) { string url = (string)listBox1.Items[urlindex]; webBrowser1.Navigate(url); urlindex++; } } private void timer2_Tick_1(object sender, EventArgs e) { timer2.Stop(); if (listBox2.Items.Count != 0 && urlindex < listBox2.Items.Count) { string url = (string)listBox2.Items[urlindex]; } } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { timer1.Start(); timer2.Start(); } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { }

  • C#

    form2で入力した文字を form1で表示させたいのですが、上手くいきません。プログラムの(文字制限の為)一部だけ のせます。アドバイス よろしくお願いします。 まず、form1 public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.Label label1; private Form2 form2; /// <summary> /// 必要なデザイナ変数です。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); form2= new Form2(); label1.Text=string.Format("{0}",form2.s); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } static void Main() { Application.Run(new Form1()); } private void menuItem2_Click(object sender, System.EventArgs e) { if (form2.ShowDialog(this) == DialogResult.OK) this.Refresh(); } } 次にform2です private void button1_Click(object sender, System.EventArgs e) { string s; s=(string)textBox1.Text; } public string s { get { return s; } }

  • vc++6.0とVisual C++ 2010

    遊び程度ですが、VC++をいじったことがあります。 もう何年も触っていませんが。 lVisual C++ 2010 Expressについて調べていたら、    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {            label1->Text=L"はじめてのC++";         } というコードがありました。 vc++6.0に ”label1->Text=~”こんな1行で文字が表示できる方法ってありましたっけ。 次のサイトのような書き方だったと思いますが、コードの書き方が変わったのでしょうか? http://t-recipe.com/vc/string.html お分かりの方、教えてください。お願いします。

  • イベントハンドラ関数について

    現在VC++/CLR 2005 でプログラミングをしています。 基本的な事柄かもしれませんが、質問させてください  以下の2つのボタンのイベントハンドラ関数があった場合 //ボタン1のイベントハンドラ関数 private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) String^ str = textBox1->Text } //ボタン2のイベントハンドラ関数 private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { } ボタン2をクリックしたときの動作とボタン1の動作を同じにしたいと思っています。 その場合は private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) String^ str = textBox1->Text } //ボタン2のイベントハンドラ関数 private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { String^ str = textBox1->Text } のようにボタン2のイベントハンドラ関数をボタン1と同じように書けばいいのですが イベントハンドラ関数の処理が膨大になった場合を考えて省略する 書き方は無いのでしょうか?(以下の場合ですとエラーが起こってしまいます。) private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { button1_Click(); } 宜しくお願いします

  • C#での時間制限のプログラム

    C#初心者です。Visual studioでプログラムを書いてます。 ラベルに制限時間を表示させ、起動して30秒経つと 「もういちどやりますか?」というメッセージボックスが出て、 「はい」でもう一度開始、「いいえ」でプログラムを終了させるような 処理をさせたいのですが、「はい」を押しても同じメッセージボックスが出てループしてしまい、もう一度開始させることができません…。 解決する方法を教えていただきたいです。よろしくお願いします。 以下、書いたコードです。 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 timelimit { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DateTime StartTime; TimeSpan TimeLimit; private void Form1_Load(object sender, EventArgs e) { timer1.Enabled = true; StartTime = DateTime.Now; TimeLimit = new TimeSpan(0,0,30); } private void timer1_Tick(object sender, EventArgs e) { TimeSpan tm = DateTime.Now - StartTime; if(tm > TimeLimit) { TimeLimit = new TimeSpan(0, 0, 30); timer1.Enabled = false; DialogResult result = MessageBox.Show ("時間終了!もういちどやりますか?","終了処理",MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { timer1.Enabled = true; } else if (result == DialogResult.No) { Close(); } }else{ label1.Text = (TimeLimit - tm).ToString(); } }

  • c# イベントハンドラ 統一

    trackBarを複数配置しているのですが、そのtrackBarごとに private void trackBar1_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value,'赤'); } private void trackBar2_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value, '青'); } private void trackBar3_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value, '緑'); } private void trackBar4_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value, '透'); } こんな風に別々に記述する形になって非常にスッキリしないです。 これをジェネリクス?やデリゲート?などを使ってすっきりできないのでしょうか? visual stdio c# 2008を使ってます。 宜しくお願いします。

  • .Net Compact でのダブルバッファリング

    .Net Comapct Frameworkを利用してのC#プログラミングで、 ダブルバッファリングを利用して画面を描画しようとしていますが、 ちらつきが解消されません。 どこがおかしいかご指摘いただけると幸いです。 【環境】 Visual Studio 2005 C# .Net Compact Framework 2.0 【コード(抜粋)】 public partial class Form1 : Form { private Bitmap m_offScr; // オフスクリーンイメージ private Graphics m_offG; // オフスクリーングラフィックス private System.Windows.Forms.Timer timer1; public Form1() { this.timer1.Enabled = true; this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); ~略~ this.m_offScr = new Bitmap(400, 200); this.m_offG = Graphics.FromImage(this.m_offScr); } private void Form1_Paint(object sender, PaintEventArgs e) { m_offG.FillRectangle(new SolidBrush(Color.Red), 10, 10, 100, 100); e.Graphics.DrawImage(this.m_offScr, 0, 0); } // イベントハンドラ private void timer1_Tick(object sender, EventArgs e) { // 秒の更新 this.Refresh(); } } よろしくお願いいたします。

  • 【C#】ラベルのダブルクリックについて

    初めて質問させていただきます。C#初心者です。よろしくお願いします。 開発環境はMicrosoft Visual C# 2008です。 現在、UserControl1を作成して、label1にはテキストを入れています。 ・label1をクリックすると、パネル(panel1)の背景色を変更。 ・label1をダブルクリックすると、他のラベル(atai_text)にlabel1内のテキストを代入。 ↑上記2つの処理をしたいのですが、クリックの方は処理できているのですが、ダブルクリックは反応せず、どのように作成すればよいかがわかりません。 <UserControl1.csの一部> private void label1_Click(object sender, EventArgs e) { if (this.label1.BackColor == Color.Pink) { this.panel1.BackColor = Color.FromArgb(0xFF, 0xFF, 0x80); } else { this.panel1.BackColor = Color.Pink; } } private void label1_DoubleClick(object sender, EventArgs e) { this.atai_text.Text = this.label1.Text; } どなたかご教授よろしくお願いします。

専門家に質問してみよう