Pass a string in a form

Ksilenio

Member
Joined
Jan 7, 2021
Messages
9
Programming Experience
3-5
Hi to everyone.I am built a addin aplication for solidworks. I use the VB.net for that . I have create a form with a TextBox and in this control i want to pass some information . Here is the code which i use
Dim ex As New FEditDox
ex.TBEditBoxDepth.Text = "200"
The "FEditBox " is a form
Can anyone tell me why i can't pass the value into the " TBEditBoxDepth.Text" control?
Regards Kostas
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,376
Location
Sydney, Australia
Programming Experience
10+
@JohnH, is likely correct because Friend is the default access level for fields when you add a control or component in the designer. There are two solutions: a good one and a bad one.

The bad one is the easier option so it is very tempting. That is to simply change the access level of the control from Friend to Public in the designer. If you do that, you code will work.

The problem with that is that setting the Text of the control is not the only thing you can then do. You can access every member of the TextBox, e.g. Size, Location and Dispose, and you can also replace the TextBox itself with a different one. That's all bad. You should not be able to do anything that you don't need to do from outside a class for that reason, you should use the good solution.

What you should do is actually make the control - ALL controls and components - Private and then provide pass-through members for only the functionality you specifically need outside the form. I call them "pass-through" members because they are like little holes in the object that you can pass data through. In this case, you only need to access the Text property so that's the only one you should allow to pass through. You would do this is the form:
VB.NET:
Public Property TBEditBoxDepthText
    Get
        Return TBEditBoxDepth.Text
    End Get
    Set
        TBEditBoxDepth.Text = value
    End Set
End Property
and then you would change your existing code to this:
VB.NET:
Dim ex As New FEditDox

ex.TBEditBoxDepthText = "200"
 

Ksilenio

Member
Joined
Jan 7, 2021
Messages
9
Programming Experience
3-5
Hi @jmcilhinney Thank you for helping me. I wrote your code to my form but still i don't have the value in the textbox .I don't understand , from where I do all the controls "private" and what do you mean "components". I send you screenshot
to see my form and the code
scr7.jpg

Regards
Kostas
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,376
Location
Sydney, Australia
Programming Experience
10+
Firstly, I made a small mistake in my code, although it won't actually affect your application. This:
VB.NET:
Public Property TBEditBoxDepthText
should be this:
VB.NET:
Public Property TBEditBoxDepthText As String
As for your issue, you haven't provided enough information for us to diagnose it reliably but I think I can guess what the issue is. If you made the change I suggested, you have this code:
VB.NET:
Dim ex As New FEditDox

ex.TBEditBoxDepthText = "200"
That is creating a new instance of that form type and setting the Text of a TextBox in it. Are you actually displaying that form that you created? Are you perhaps expecting that code to change an existing form that has already been displayed? If the latter, that's obviously the problem. Let's say that you had the plans to build a house. That's your class. Let's say that you then build a house off that plan. That's your first instance. Now let's say that you build a new house off that same plan and put some furniture in one of the rooms. Would you expect that furniture to magically appear in the corresponding room of the first house? Of course you wouldn't, so why would you expect that to happen here? If you push data into one object, that data won't magically appear in another object, just because the two objects are the same type.

If that's not what you're doing then you need to do your own due diligence first, i.e. debug your code. If you don't know how to debug, stop what you're doing and learn, because it is an essential skill for all developers. There are lots of tutorials and at least one of our regular contributors has a link to one in their signature. Once you debug, you will likely find the issue but, if you don't, you can provide us with more relevant information.
 

Ksilenio

Member
Joined
Jan 7, 2021
Messages
9
Programming Experience
3-5
Hi @jmcilhinney . I have change the code and i added the " Public Property ...." code into designer ,but it does not appear the value into the text box .
The form is already displayed .
What I want to achieve is (after the form displayed) to pass some values into textboxes but i can't .
The program run "ok" without problems .
I made and debugging too but the value still does not appear.
I don't know if it's need to fired some event from the form but i don't know which.
If you need further informations please tell me
Regards kostas
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,376
Location
Sydney, Australia
Programming Experience
10+
I have change the code and i added the " Public Property ...." code into designer
The designer is where you see the visual representation of the controls. You can't have added a property there.
The form is already displayed .
Then I have already explained what the issue is. You need to set the property on the form you already displayed, not a new one that you haven't displayed. The code is working exactly as it should but you are putting the data in the wrong object. Put it in the right object, i.e. the object that you're actually looking at, and you'll see it. I suggest that you follow the Blog link in my signature below and check out all three parts of my post on Data Among Multiple Forms. Passing data between forms is as easy as passing data between any other objects because forms are objects like any other, but people try to make it hard by thinking about forms as somehow different.
 

Ksilenio

Member
Joined
Jan 7, 2021
Messages
9
Programming Experience
3-5
The designer is where you see the visual representation of the controls. You can't have added a property there.

Then I have already explained what the issue is. You need to set the property on the form you already displayed, not a new one that you haven't displayed. The code is working exactly as it should but you are putting the data in the wrong object. Put it in the right object, i.e. the object that you're actually looking at, and you'll see it. I suggest that you follow the Blog link in my signature below and check out all three parts of my post on Data Among Multiple Forms. Passing data between forms is as easy as passing data between any other objects because forms are objects like any other, but people try to make it hard by thinking about forms as somehow different.
OK Thank you very much for your help @jmcilhinney
 
Top Bottom