-1

我正在为 Windows 应用程序编写一些代码,除了 IntelliSense 没有在某些单击事件范围内为我提供选项外,一切都很顺利。令人惊讶的是,当我在没有它的帮助的情况下对其进行编码时,它并没有在该代码行上给我任何红色波浪,所以我假设范围是好的。

我有两个表单,FormA并且FormB,一个按钮单击事件FormA应该触发FormB它的文本框,其中填充了它在运行附加的逻辑后收到的信息。我正在做的是

formSrchResult.txtSearchFirstName.Text = searchedInfo.FirstName;
formSrchResult.txtSearchLastName.Text = searchedInfo.LastName;
formSrchResult.txtSearchUsername.Text = searchedInfo.UserName;
formSrchResult.txtSearchEmail.Text = searchedInfo.Email;

formSearchResult是 a 的对象,FormSearch(FormB)并且searchedInfo是具有存储数据的属性的类的对象,我希望在其中FormSearch显示文本框。FormSearch和都searchedInfo被宣布为公开的,但是当我开始输入时

“formSrchResult。” + "ctrl + 空格"

IntelliSense 没有提供包含文本框和属性名称的列表。所以,我想知道是否有人对可能导致这种情况的原因有任何想法,我很想知道。顺便说一句,它的 Visual Studio 2012。

谢谢。

public class UserInfo
{
    public int UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string UserName { get; set; }
}

那是存储搜索信息的类,下面是搜索方法的代码......

public UserInfo Search(string email)
{
    UserInfo searchUserInfo = new UserInfo();

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("spListEverything", conn);
        cmd.CommandText = "spListEverything";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Email", email);

        SqlDataReader rdrSearch = cmd.ExecuteReader();

        if (rdrSearch.Read())
        {
            //why do we need to convert the first name, last name etc to ToString
            //when they are defined, in the database, as a string itself?
            searchUserInfo.UserID = Convert.ToInt32(rdrSearch["UserID"]);
            searchUserInfo.FirstName = rdrSearch["FirstName"].ToString();
            searchUserInfo.LastName = rdrSearch["LastName"].ToString();
            searchUserInfo.UserName = rdrSearch["UserName"].ToString();
            searchUserInfo.Email = rdrSearch["Email"].ToString();
        }
        else 
        {
            // "Sorry, no match found";
        }

        }
    catch (Exception ex)
    {
    }
    finally
    {
       conn.Close();
    }
    return searchUserInfo;
}

下面是我对 Intellisense 没有帮助的 btnClick 事件的编码

private void btnSearch_Click(object sender, EventArgs e)
{
    string email = txtSearchEmail.Text.ToString();

    Manage myManage = new Manage(); //to call the search function
    FormSearchResult formSrchResult = new FormSearchResult();//to put the vlaues in the textboxes in that form

    UserInfo searchedInfo = myManage.Search(email);

    //the search function is not working properly. No value is returned to be stored
    //in the userInfo object

    //Here for some reason my intellisense is not providing options.

    formSrchResult.txtSearchFirstName.Text = searchedInfo.FirstName;
    formSrchResult.txtSearchLastName.Text = searchedInfo.LastName;
    formSrchResult.txtSearchUsername.Text = searchedInfo.UserName;
    formSrchResult.txtSearchEmail.Text = searchedInfo.Email;

    txtSearchFirstName.Enabled = false;
    formSrchResult.ShowDialog();
}
4

1 回答 1

0

Windows 窗体控件不是窗体的静态属性,因此您需要创建一个 formSrchResult 类型的对象,以便能够从另一个类访问它的任何控件。类需要具有要调用的静态方法或属性,而无需直接引用该类型的对象。

像这样的东西:

public partial class FormB : Form
{
    private formSrchResult formSR; // this is a private field that is accessible to any method in your FormB class - you must initialize it first before trying to do anything on the object or you will receive a null reference

    private void Search_Click(object sender, EventArgs e)
    {
        SearchResult searchedInfo = DoSearch();
        formSR = new formSrchResult();
        formSR.txtSearchFirstName.Text = searchedInfo.FirstName;
        formSR.txtSearchLastName.Text = searchedInfo.LastName;
        formSR.txtSearchUsername.Text = searchedInfo.UserName;
        formSR.txtSearchEmail.Text = searchedInfo.Email;
        formSR.Show();
    }

    private SearchResult DoSearch()
    { 
        // your code
    }
}

您可能还希望使 FormB 类可以全局访问 formSrchResult 对象,以便将其声明为私有字段。这样,您可以从类中的任何方法访问您的对象。我希望这有帮助。

于 2013-04-10T18:08:50.683 回答