![](/img/trans.png)
[英]VB 2010 Unable to cast object of type 'System.String' to type 'System.Windows.Forms.TextBox'
[英]Value of type 'String' cannot be converted to 'System.Windows.Forms.Textbox'?
我的名為form2.vb的表單具有此代碼。
Private Sub ADDRESS_TICKETDataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles ADDRESS_TICKETDataGridView.CellDoubleClick
Dim value As String = ADDRESS_TICKETDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
If e.ColumnIndex = e.ColumnIndex Then
Search.Show()
Search.TextBox1 = value
End If
End Sub
End Class
但是關於錯誤,我無法將'String'類型的Value轉換為'System.Windows.Forms.TextBox'。 我想本質上解決此問題,我想要的是從datagridview獲取值並將其輸入到另一個具有文本框的窗體上。 是可以做到的還是我做錯了什么。 請幫忙?
Search.TextBox1 = value
您只是試圖分配TextBox1
變量以容納字符串而不是文本框。
那沒有任何意義。
相反,您想要通過設置文本框中的Text
屬性來設置Text
框中顯示的文本。
只是為了提供信息(並增加我對Slacks答案的評論),有一種方法可以通過使用運算符重載來解決此問題。 (代碼在C#中,但是我想它在VB.Net中很容易翻譯)
只需創建一個從TextBox
繼承的類,如下所示:
public class MyTextBox : TextBox
{
public static implicit operator string(MyTextBox t)
{
return t.Text;
}
public static implicit operator MyTextBox(string s)
{
MyTextBox tb = new MyTextBox();
tb.Text = s;
return tb;
}
public static MyTextBox operator +(MyTextBox tb1, MyTextBox tb2)
{
tb1.Text += tb2.Text;
return tb1;
}
}
然后,您將可以執行以下操作:
MyTextBox tb = new MyTextBox();
tb.Text = "Hello ";
tb += "World";
您的文本框的內容將是Hello World
我嘗試使其與tb = "test"
,但沒有成功。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.