8 Replies - 18271 Views - Last Post: 09 May 2012 - 03:22 PM Rate Topic: -----

#1 vnewbie   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 28-April 12

How to declare textbox?

Posted 09 May 2012 - 01:51 PM

Hi,

I have created a new class, but I cannot use the textbox in the class as it is in the form1 class how would i declare it or use it in both classes?

i have tried form1.textbox1.text which workings in coding but when running my system it does nothing.
Is This A Good Question/Topic? 0
  • +

Replies To: How to declare textbox?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15331
  • View blog
  • Posts: 61,451
  • Joined: 12-June 08

Re: How to declare textbox?

Posted 09 May 2012 - 01:55 PM

Where do you want this textbox to show up?
Was This Post Helpful? 0
  • +
  • -

#3 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: How to declare textbox?

Posted 09 May 2012 - 01:59 PM

I don't know why using form1.textbox1.text does not work but what to you mean when you say : "when running my system it does nothing."

the other way would be to declare a global variable and then:

globalVar = textbox1.text

Then use the global variable in the class
Was This Post Helpful? 0
  • +
  • -

#4 pradeep1210   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: How to declare textbox?

Posted 09 May 2012 - 01:59 PM

You can put the full qualifer to access the same object (must be Public) from another class.

e.g.

YourClass.TextBox1 = Form1.TextBox1

Was This Post Helpful? 0
  • +
  • -

#5 vnewbie   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 28-April 12

Re: How to declare textbox?

Posted 09 May 2012 - 02:34 PM

Mod123, I have created a class which I use for process and threading to run some commands, the command reads a textbox on my form which is textbox1 for example...

sIn.Write("ping " & TextBox1.Text & " >d:\testing.txt" & System.Environment.NewLine)



Now Textbox1 in my public class is showing an error Error 1 Reference to a non-shared member requires an object reference - which is due to it not being declared however when I try to change it to Form1.textbox1.text the coding doesnt show any errors but when I run the program the output file states that it failed as no value was pinged, If i hard code the ping it works so the problem is with the textbox.

M_Wylie and Pradeep many thanks I have tried both suggestions but they fail with Error 1 Declaration expected. I did put the coding at the top global level.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV   User is offline

  • vexing conundrum
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,746
  • Joined: 24-July 11

Re: How to declare textbox?

Posted 09 May 2012 - 02:39 PM

Just an idea: try using a Property.
Public ReadOnly Property valu As String
Get
Return TextBox1.Text
End Get
End Property


Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to declare textbox?

Posted 09 May 2012 - 02:40 PM

What does the constructor of the class look like?

Can you just create one and pass that value from your main form to the constructor to be used in the class?
Was This Post Helpful? 0
  • +
  • -

#8 vnewbie   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 28-April 12

Re: How to declare textbox?

Posted 09 May 2012 - 02:58 PM

Dimitriv nope still same issue with the error on textbox1.

Charlie do you mean the following by constructor?

Public Class Form1
 


    Public Class Mytestclass



        Private Sub RunCommand
	'My process coding goes here where textbox1 is

        End Sub

        Public Sub Cancel()
	'My cancel coding
        End Sub

    End Class

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Run the command
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Cancel the command
    End Sub
End Class






Do you mean have two textboxes and pass the value over?
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: How to declare textbox?

Posted 09 May 2012 - 03:22 PM

I guess something like:
Public Class Form1
    Public Class MyTestClass
        Private MyTextFromAnotherForm As String
        Public Sub New(sometext As String)
            MyTextFromAnotherForm = sometext
            RunCommand()
        End Sub
        Private Sub RunCommand()
            MessageBox.Show(MyTextFromAnotherForm)
        End Sub
    End Class
...

It would be instantiated by passing the textbox.text
Dim test as New MyTestClass(TextBox1.Text)
NOTE: I put the call to RunCommand in the Constructor but I would suggest you create a method that you can use to initiate the runcommand Or just make RunCommand available so that you could use something like:

Dim test as New MyTestClass(TextBox1.Text)
test.RunCommand


Is there a reason you're creating this class inside the Form1 instead of a separate class?

This post has been edited by CharlieMay: 09 May 2012 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1