[英]Validating event with TextBox in C#
因此,我有2種形式。 最主要的是一個TextBox
,如果我按F1
,它將根據TextBox
上插入的值使用DataGridView
打開一個新窗體。 在第二個表單中雙擊該行后,它將再次進入主表單,並用選定的行填充TextBox
。
然后,在主窗體中,我在TextBox
上有一個Validating事件,它可以根據該值在Main Form DataGridView
顯示它。
不幸的是,它不起作用,我認為問題出自其他組件的CauseValidation
。 我使用例如禁用它: dataGridView1.CauseValidation = false;
,但仍然相同。
這是TextBox
事件上的代碼:
private void txtCargs_Validating(object sender, CancelEventArgs e)
{
e.Cancel = false;
try
{
SqlConnection con = new SqlConnection(cs.DBConnP);
con.Open();
string querySelect = @"SELECT RTRIM(CL.Cargs) AS 'Cargs', RTRIM(S.Abvs) AS 'Abss', RTRIM(CL.Linha) AS 'Linha', RTRIM(CL.Qtd) AS 'Quantity'
FROM CargaCab CC (NOLOCK)
INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs
INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code
INNER JOIN Marks M (NOLOCK) ON S.Marks = M.Marks
WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.TypeEmb = 'P'
AND CC.TypeD = 'OCS' AND CL.Cargs = '" + txtCargs.Text.Trim() + "' ORDER BY CL.Carga, S.Marks DESC, S.Abvs";
cmd = new SqlCommand(querySelect);
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "CargaCab");
dataGridView1.DataSource = ds.Tables["CargaCabee"].DefaultView;
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[2].ReadOnly = true;
dataGridView1.Columns[3].ReadOnly = false;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我該怎么辦?
而不是將所有邏輯放在txtCargs_Validating
事件中,而是創建一種在第二個窗體關閉時調用的方法。 您可以通過將第一個表單的實例傳遞給第二個表單來執行此操作,例如,
public class SecondForm : Form {
private Form _1stForm;
public SecondForm(Form 1stForm)
{
_1stForm = 1stForm;
}
...
public SecondForm_Closing(object sender, EventArgs e)
{
_1stForm.SetTheTextBox(theRowValueSelected);
}
從主要形式調用代碼:
var frm = new SecondForm(this);
frm.Show();
更好的解決方案是將所有代碼都放在業務邏輯層中(例如MVC中的Controller或MVVM中的ModelView),並將所有UI控件綁定到業務邏輯層中的數據結構。
兩種方式都保存用於驗證的驗證事件,例如:
MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
我建議將此事件設置為您的自定義函數,而不要使用bool返回類型。 在文本框中使用text_changed事件,然后調用Validate函數。 我嘗試了示例Textbox_Validating事件及其在表單關閉時的調用。
private void txtCargs_TextChanged(object sender, EventArgs e)
{
if (ValidateText())
{
//Then do this
}
}
private bool ValidateText()
{
bool Isvalidated = false;
try
{
SqlConnection con = new SqlConnection(cs.DBConnP);
con.Open();
string querySelect = @"SELECT RTRIM(CL.Cargs) AS 'Cargs', RTRIM(S.Abvs) AS 'Abss', RTRIM(CL.Linha) AS 'Linha', RTRIM(CL.Qtd) AS 'Quantity'
FROM CargaCab CC (NOLOCK)
INNER JOIN CargsLin CL (NOLOCK) ON CC.Cargs = CL.Cargs
INNER JOIN Stock S (NOLOCK) ON CL.Code = S.Code
INNER JOIN Marks M (NOLOCK) ON S.Marks = M.Marks
WHERE CC.Date >= GETDATE() - 120 AND CL.State NOT IN ('F', 'A') AND S.TypeEmb = 'P'
AND CC.TypeD = 'OCS' AND CL.Cargs = '" + txtCargs.Text.Trim() + "' ORDER BY CL.Carga, S.Marks DESC, S.Abvs";
cmd = new SqlCommand(querySelect);
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "CargaCab");
dataGridView1.DataSource = ds.Tables["CargaCabee"].DefaultView;
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[2].ReadOnly = true;
dataGridView1.Columns[3].ReadOnly = false;
con.Close();
Isvalidated = true;
}
catch (Exception ex)
{
MessageBox.Show("Error\nDetalhes: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Isvalidated = false;
}
return Isvalidated;
}
我認為這里的問題是您想要解決方案,而不是解決問題。
這是我認為如何實現的簡要概述:
public class Form1 : Form
{
public TextBox textBox1 { get; set; }
public Button button1 { get; set; }
private void button1_Click(object sender, EventArgs e)
{
var form = new Form2();
form.Show();
textBox1.Text = form.val;
//do your sql stuff here
}
}
public class Form2 : Form
{
public DataGridView datagriview1 { get; set; }
public string val { get; set; }
private void datagriview1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1)
val = datagriview1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
Close();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.