从按钮调用功能 [英] Calling a function from a button

查看:96
本文介绍了从按钮调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序。我有一个函数使用Linq to SQL来查找我想要的记录,但我不知道如何在我的button.click事件中调用此函数,以便click函数将启动我的其他函数。



另外,在我的button.click事件中,我需要将textBox1.Text值设置为sql语句但是怎么做呢?



 命名空间 InsertParameter 
{
public partial class Form1:Form
{
LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
// 如何在这里调用以下函数???
}

public 异常searchCity(学生,< span class =code-keyword> string searchCity)
{
try
{
< span class =code-keyword> if ( string .IsNullOrEmpty(searchCity))
{
MessageBox.Show(< span class =code-string> 请输入值);
}
其他
{
var City = 来自 stud linqStud.Students
其中 stud.City == searchCity
选择 stud;

dataGridView1.DataSource = City;

}
return null ;
}
catch (例外情况)
{
return ex;
}
}
}
}





谢谢



尼克

解决方案

这取决于你如何获得学生参数 - 但由于你的方法实际上并没有使用它...目前,你可以使用 null

< pre lang =cs> private void button1_Click( object sender,EventArgs e)
{
异常ex = searchCity( null 纽约);
if (ex!= null
{
(前);
}
}


更新 - 这是完成的解决方案:

  public   partial   class  Form1:Form 
{
LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
Student student = new Student();
string city = textBox1.Text;

searchCity(学生,城市);
}

public 异常searchCity(学生, string searchCity )
{
尝试
{
如果 string .IsNullOrEmpty(searchCity))
{
MessageBox.Show( < span class =code-string>请输入值);
}
其他
{
var City = 来自 stud linqStud.Students
其中 stud.City == searchCity
选择 stud;

dataGridView1.DataSource = City;

}
return null ;
}
catch (例外情况)
{
return ex;
}


Hi, I have a Windows Form Application. I have a function which uses Linq to SQL to find the records I want but I don't know how to call this function in my button.click event so that the click function will launch my other function.

Also, in my button.click event I will need to set the textBox1.Text value to the sql statement but how would that be done?

namespace InsertParameter
{
public partial class Form1 : Form
{
    LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // how do I call the below function here???
    }

    public Exception searchCity(Student student, string searchCity)
    {
        try
        {
            if (string.IsNullOrEmpty(searchCity))
            {
                MessageBox.Show("Please enter a value");
            }
            else
            {
                var City = from stud in linqStud.Students
                           where stud.City == searchCity
                           select stud;

                dataGridView1.DataSource = City;

            }
            return null;
        }
        catch (Exception ex)
        {
            return ex;
        }
    }
}
}



Thanks

Nick

解决方案

It depends on how you are going to get the student parameter - but since your method doesn't actually use it...for the moment, you can just use a null:

private void button1_Click(object sender, EventArgs e)
{
    Exception ex = searchCity(null, "New York");
    if (ex != null)
    {
        throw(ex);
    }
}


update - here is the finished solution:

public partial class Form1 : Form
    {
        LinqtoSqlDataContext linqStud = new LinqtoSqlDataContext();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            string city = textBox1.Text;

            searchCity(student, city);
        }

        public Exception searchCity(Student student, string searchCity)
        {
            try
            {
                if (string.IsNullOrEmpty(searchCity))
                {
                    MessageBox.Show("Please enter a value");
                }
                else
                {
                    var City = from stud in linqStud.Students
                               where stud.City == searchCity
                               select stud;

                    dataGridView1.DataSource = City;

                }
                return null;
            }
            catch (Exception ex)
            {
                return ex;
            }


这篇关于从按钮调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆