大家好,我在尝试继承tabcontrol时遇到以下错误:基类'System.Windows.Forms.TabControl'为类'Form1'指定不能与基类'System.Windows.Forms.Form'不同其他部分类型之一!
这是一张图片:http://prntscr.com/3dqzd6
我想要制作的是一个动画标签控件。我发现了一个代码,但它只适用于visual basic(我在c#中编码,但这段代码只适用于vb) 我的代码:
Public Class Form1
Inherits TabControl//Here i got the error
Dim OldIndex As Integer
Private _Speed As Integer = 9
Property Speed As Integer
Get
Return _Speed
End Get
Set(ByVal value As Integer)
If value > 20 Or value < -20 Then
MsgBox("Speed needs to be in between -20 and 20.")
Else
_Speed = value
End If
End Set
End Property
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw, True)
End Sub
Sub DoAnimationScrollLeft(ByVal Control1 As Control, ByVal Control2 As Control)
Dim G As Graphics = Control1.CreateGraphics()
Dim P1 As New Bitmap(Control1.Width, Control1.Height)
Dim P2 As New Bitmap(Control2.Width, Control2.Height)
Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
For Each c As Control In Control1.Controls
c.Hide()
Next
Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
Dim a As Integer
For a = 0 To Slide Step _Speed
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
Next
a = Control1.Width
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a - Control2.Width, 0, Control2.Width, Control2.Height))
SelectedTab = Control2
For Each c As Control In Control2.Controls
c.Show()
Next
For Each c As Control In Control1.Controls
c.Show()
Next
End Sub
Protected Overrides Sub OnSelecting(ByVal e As System.Windows.Forms.TabControlCancelEventArgs)
If OldIndex < e.TabPageIndex Then
DoAnimationScrollRight(TabPages(OldIndex), TabPages(e.TabPageIndex))
Else
DoAnimationScrollLeft(TabPages(OldIndex), TabPages(e.TabPageIndex))
End If
End Sub
Protected Overrides Sub OnDeselecting(ByVal e As System.Windows.Forms.TabControlCancelEventArgs)
OldIndex = e.TabPageIndex
End Sub
Sub DoAnimationScrollRight(ByVal Control1 As Control, ByVal Control2 As Control)
Dim G As Graphics = Control1.CreateGraphics()
Dim P1 As New Bitmap(Control1.Width, Control1.Height)
Dim P2 As New Bitmap(Control2.Width, Control2.Height)
Control1.DrawToBitmap(P1, New Rectangle(0, 0, Control1.Width, Control1.Height))
Control2.DrawToBitmap(P2, New Rectangle(0, 0, Control2.Width, Control2.Height))
For Each c As Control In Control1.Controls
c.Hide()
Next
Dim Slide As Integer = Control1.Width - (Control1.Width Mod _Speed)
Dim a As Integer
For a = 0 To -Slide Step -_Speed
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
Next
a = Control1.Width
G.DrawImage(P1, New Rectangle(a, 0, Control1.Width, Control1.Height))
G.DrawImage(P2, New Rectangle(a + Control2.Width, 0, Control2.Width, Control2.Height))
SelectedTab = Control2
For Each c As Control In Control2.Controls
c.Show()
Next
For Each c As Control In Control1.Controls
c.Show()
Next
End Sub
请帮助我!
-Christos
答案 0 :(得分:0)
当您创建表单时,设计人员会创建一个名为Form1
的表单,并且其名称为Partial Class
- 这是生成的代码维护的地方,不应该被您触及。此Form1
继承自System.Windows.Forms.Form
,因此无法从TabControl
继承。如果您只是更改上面所有类的名称,那么该名称将成为Sub-Classed
的{{1}}版本,那么一切都会很好。
TabControl
您的代码应该像:
Public Class Form1
...
End Class
Partial Class Form1
'same class as above just allowed to be seperate
'they share the internal code
End Class