Solved

clipboard

Posted on 2008-10-24
3
331 Views
Last Modified: 2010-04-23
i want duplicate class that consist of  string and class. I try copy the class  and paste  it to form by using clipboard ,but fail .how to do?thanks.my Code is below.
<Serializable()> _
Public Class Student
    Public FirstName As String
     Public mysubstudent As subStudent
     End Sub
End Class
Public Class subStudent
    Public width As Integer
    Public height As Integer
    Public left As Integer
    Public right As Integer
    Public objectname As String
End Class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim emp As Student
        Dim mysub As New subStudent
        emp = New Student
        emp.FirstName = TextBox1.Text
        mysub.width = Button3.Width
        mysub.height = Button3.Height
        mysub.objectname = "Button"
        emp.mysubstudent = mysub
        Dim data_object As New DataObject
        data_object.SetData("Student1", emp)
        Clipboard.SetDataObject(data_object)
   End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    
           Button2.Click
        Dim emp As Student
        Dim stu As subStudent
        Dim data_object As IDataObject = Clipboard.GetDataObject()
        If data_object.GetDataPresent("Student1") Then
             emp = DirectCast(data_object.GetData("Student1"), Student)
            ' fault here
            T3.Text = emp1.FirstName
            stu.height = emp.mysubstudent.height
            stu.width = emp.mysubstudent.width
            stu.objectname = emp.mysubstudent.objectname
            If stu.objectname = "Button" Then
                Dim mybut As New Button
                mybut.Width = stu.height
                mybut.Height = stu.width
                mybut.Text = "myb"
                mybut.Left = 100
                mybut.Top = 200
            End If
        End If
    End Sub
End Class
how to do it?thanks .
 
0
Comment
Question by:Cheng_sam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Assisted Solution

by:omegaomega
omegaomega earned 70 total points
ID: 22797751
Hello, Cheng sam,

There seem to be a couple of problems in your code.  (Maybe it hasn't been copied correctly in your post.  Otherwise I don't know how you could get it to compile.)
1. There is an extraneous End Sub in your Student class.
2. Your Button2 Click event handler refers to a non-existent variable "emp1" at the point of your error.

I assume that neither of these faults appear in your actual code.  

I think that your actual problem is that your class subStudent is not marked as serializable.  Just add:

<Serializable()> _

in front of the line "Public Class subStudent" and I think you'll be able to move on to the next problem.  (That will be that you haven't yet assigned the variable "stu".)

Cheers,
Randy

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 180 total points
ID: 22798140
I agree with omegaomega...marking subStudent as serializable fixes the main problem.

Here I have done some MINOR refactoring of your code:
Public Class Form1
 
    <Serializable()> _
    Public Class Student
        Public FirstName As String
        Public mysubstudent As New subStudent
    End Class
 
    <Serializable()> _
    Public Class subStudent
        Public width As Integer
        Public height As Integer
        Public left As Integer
        Public right As Integer
        Public objectname As String
    End Class
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim emp As New Student
        emp.FirstName = TextBox1.Text
        emp.mysubstudent.width = Button3.Width
        emp.mysubstudent.height = Button3.Height
        emp.mysubstudent.objectname = "Button"
        Clipboard.SetDataObject(New DataObject("Student1", emp))
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Clipboard.ContainsData("Student1") Then
            Dim emp As Student = DirectCast(Clipboard.GetData("Student1"), Student)
            T3.Text = emp.FirstName
            If emp.mysubstudent.objectname = "Button" Then
                Dim mybut As New Button
                mybut.Width = emp.mysubstudent.width
                mybut.Height = emp.mysubstudent.height
                mybut.Text = "myb"
                mybut.Left = 100
                mybut.Top = 200
                Me.Controls.Add(mybut)
            End If
        End If
    End Sub
 
End Class

Open in new window

0
 

Author Closing Comment

by:Cheng_sam
ID: 31509564
thank for omegaomega and Idle_Mind.It works good.Idle_Mind give the total code ,so he get more score.Thanks both.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 48
"lblTime is not declared" 3 65
Broken .resx file generating errors 18 44
MessageBox Appear behind a form in Runtime 1 32
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question