我正在为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
是FormSearch(FormB)
的对象,searchedInfo
是类的对象,具有存储数据的属性,我希望FormSearch
中的文本框显示。 FormSearch
和searchedInfo
都被公开,但是当我开始输入
“formSrchResult”。 +“ctrl + space”
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;
}
以下是我对btnClick事件的编码,其中Intellisense没有帮助
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();
}
答案 0 :(得分: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对象,因此您可以将其声明为私有字段。这样,您就可以从类中的任何方法访问对象。我希望这会有所帮助。