问题描述
我有一个名称为placeofstudent1
的富文本框。我想将富文本框中写的任何内容存储在名为Place
的变量中,以便可以将该信息传递到数据库中。
try
{
string name = nameofstudent.Text;
string father = fatherofstudent.Text;
string mother = motherofstudent.Text;
string id = stud_id.Text;
string gender;
if (male.IsChecked == true)
{
gender = "M";
}
else
gender = "F";
string studentAge = ageofstudent.Text;
DateTime dt = Convert.ToDateTime(dob.Text);
int Age = Convert.ToInt16(ageofstudent.Text);
string Place = placeofstudent1.Text;
connect();
con.open();
string saved = "insert into student_details (student_id,student_name,father_name,mother_name,gender,age,dob,place,class)values('" + id + "','" + name + "','" + father + "','" + mother + "','" + gender + "','" + Age + "','" + dt + "','" + Place + "','" + x + "')";
sqlCommand cmd = new sqlCommand(saved,con);
cmd.ExecuteReader();
con.Close();
如果我尝试使用Place
填充变量.Text
,则无法正常工作。我可以使用什么方法使之成为可能?谢谢您的帮助。
解决方法
您使用了错误的控件。当前,您正在使用WinForms控件,而不是WPF控件。
这是正确的System.Windows.Controls.RichTextBox
(请注意其他名称空间)。您会注意到WPF实现没有Text
属性,而是Document
属性。
您必须手动将内容(RichTextBox.Document
,它是FlowDocument
)转换为字符串。
<Window>
<RichTextBox x:Name="MyRichTextBox" />
</Window>
var richTextBoxContentRange = new TextRange(
this.MyRichTextBox.Document.ContentStart,this.MyRichTextBox.Document.ContentEnd);
string richTextBoxText = richTextBoxContentRange.Text;