C#で、プロジェクト1つ1つを登録するシステムの改修をしています。
作業漏れを防ぐため、タイトルのような、サブフォームで保存ボタンを押していないときに、メインフォームの右上の閉じるボタンを押したときに、
警告メッセージを出したいと思っています。
サブフォームのクラスの保存ボタンのイベントハンドラにフラグを設置し、それが1となっているか確認し、なってなければ閉じる処理で
警告をだせばいいだろうと考え、コードを書いてみたのですが…
今、困っているところが、変数の受け渡しがうまくいかないことです。
ソースを一部書きます。
メインフォームで、BaseStationRegisterContentViewクラスをインスタンス化して、そのFlgを見に行けばいいかなと思ったんですが、
「uintは無効です。」とパラメータに問題ありです。引数の_Id、house_id
はサブフォームのBaseStationRegisterContentViewクラスだけが持っています。
どう修正すればよいでしょうか?
//MainForm.cs using SmartFoundation.Views.ContentsView; //他のusingは省略 namespace SmartFoundation { public partial class MainForm : Form { BaseStationRegisterContentView bsrc; public MainForm() { InitializeComponent(); ConfigInstance.Load(); //uint golfhouseid = BaseStationRegisterContentView.golfhouseid; this.Text = Resources.TITLE; var signinAction = new Action<LoginInfo>((loginInfo) => { DBController.SetConnection(loginInfo.Server, loginInfo.DBName, loginInfo.Account, loginInfo.Password); if (DBController.TestConnection()) { ConfigInstance.Config.Server = loginInfo.Server; ConfigInstance.Config.DBName = loginInfo.DBName; ConfigInstance.Config.Account = loginInfo.Account; ConfigInstance.Save(); this.Controls.Remove(_body); _body = new MainView(); this.Controls.Add(_body); } else { MessageBox.Show(Resources.ERROR_CONNECTION, Resources.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error); } }); _body = new LoginView(signinAction); this.Controls.Add(_body); } private UserControl _body; // フォームのFormClosingイベントハンドラ private void MainForm_FormClosing_1(object sender, FormClosingEventArgs e) { bsrc = new BaseStationRegisterContentView(uint id); if (bsrc.Flg == 0) { //閉じるときの処理 DialogResult ret; ret = MessageBox.Show("[設定]タブで、【保存】ボタンを押していない可能性があります。\r\n本当に終了しますか?" , "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); if (ret == DialogResult.Cancel) e.Cancel = true; } } } }
// BaseStationRegisterContentView.cs // usingステートメント省略 namespace SmartFoundation.Views.ContentsView { public partial class BaseStationRegisterContentView : UserControl { BaseStation baseStation; uint _Id; // 保存ボタン押下確認フラグ private int SaveBaseSettingFlg; public int Flg { get { return SaveBaseSettingFlg; } } public uint id { get { return _Id; } } public BaseStationRegisterContentView(uint house_id) { InitializeComponent(); this.Dock = DockStyle.Fill; _Id = house_id; // 保存ボタン押下確認フラグを初期化 SaveBaseSettingFlg = 0; try { GetBaseStation(); } catch (Exception ex) { MessageBox.Show(Resources.EXCEPTION_ERROR_TITLE + "\n" + ex.Message + "\n\n" + Resources.EXCEPTION_ERROR_STACK_TRACE + "\n" + ex.StackTrace); } } public void GetBaseStation() { var list = DBController.BaseStationManager.Select(_Id); baseStation = list.FirstOrDefault(); if (baseStation != null) { //省略 } } bool isDouble(string text) { double value = 0; if (double.TryParse(text, out value)) { return true; } return false; } private void buttonUpdate_Click(object sender, EventArgs e) { if ( //省略) { MessageBox.Show(Resources.ERROR_DATA_EMPTY, Resources.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (//省略) { MessageBox.Show(Resources.ERROR_DATA_OUTSIDE_NUMERIC, Resources.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var updateDate = DateTime.Now; var data = new BaseStation() { id = _Id, //省略 create_user_id = 0, create_date = updateDate, update_user_id = 0, update_date = updateDate, delete_flg = false, }; if (baseStation == null) { DBController.BaseStationManager.Insert(data); } else { DBController.BaseStationManager.Update(data); } GetBaseStation(); // 保存ボタン押下確認フラグを1に設定 SaveBaseSettingFlg = 1; // 保存完了メッセージ MessageBox.Show("保存しました。" , "保存完了", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(Resources.EXCEPTION_ERROR_TITLE + "\n" + ex.Message + "\n\n" + Resources.EXCEPTION_ERROR_STACK_TRACE + "\n" + ex.StackTrace); } } private void numericKeyPress(object sender, KeyPressEventArgs e) { if ('0' <= e.KeyChar && e.KeyChar <= '9') { } else if (e.KeyChar == 22) { } else if (e.KeyChar == '\b') { } else if (e.KeyChar == '.') { if ((sender as TextBox).Text.Contains('.')) { e.Handled = true; } } else { e.Handled = true; } } } }
こんにちは。
BaseStationRegisterContentView がよくわからないのですが、
そもそもサブフォームというものはUserControlですか?
であればMainFormCloseで呼び出しているコンストラクタは生成処理ですよね。
それともMainFormのコンストラクタでMainView、LoginViewというのを生成していますがこれがサブフォームでしょうか?
メインフォームとサブフォームの関連がイマイチ掴めませんのでその辺りを明確にして頂けると。
説明不足ですみません。メインフォームの中にサブフォームが複数、タブとして存在しています。その中の一つのタブに入力欄と保存ボタンがある状態です。MainViewというのは、MainFormの右ペインの背景部分のようなViewですね。左ペインにプロジェクトの階層が表示されます。こんな説明でよろしかったでしょうか?
すみません。追記ですが、ビルドしたメインフォームには、サブフォームがタブとして埋め込まれていますが、ビルド前の画面は、それぞれ独立している状態です。
コードを見ると、メインフォームにサブフォームをAddメソッドで追加しているだけのようですが…
回答2件
あなたの回答
tips
プレビュー