Access Master Page Variable From Content Page VB - vb.net

I am having some difficulty accessing a variable on on master page this is what I have got.
Site.Master.vb
Public Class Site
Inherits System.Web.UI.MasterPage
Public VarToSee As String = "TESTING THIS"
End Class
ContentPage.vb
Public Class ContentPage
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Msgbox(VarToSee)
End Sub
End Class
The error I get "variable used before it has being assigned a value"
Basically it can't see it.
Please help..

the class for your content page does not inherit the class for your master page.
If you want to get the master page from your content page you have can get it from the Page.Master property
If you want to reference a field or property specific to that MasterPage class you have to cast it first. Like this: CType(Me.Master, Site)
so it would end up looking like this:
Public Class ContentPage
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim FooVar As String = CType(Me.Master, Site).VarToSee
Msgbox(FooVar)
End Sub
End Class

Related

Share code between two different child classes

In my project I use a 3rd party library (for which I don't have the
source code) that provides ribbon-style forms.
There are two different types of forms actually used in the project, Office2007Form and Office2007RibbonForm.
This is the hierarchy tree of the provided form classes:
System.Windows.Forms.Form
+-RibbonForm
+-OfficeForm
| +-Office2007Form <---
+-Office2007RibbonForm <---
In my forms' form.Designer.vb I replace Inherits System.Windows.Forms.Form with either Inherits Library.Office2007Form or Inherits Library.Office2007RibbonForm.
Now I want to add some default behaviour to my project's forms.
To do this I could create a base form class, insert there my code and then inherit from my base class.
The problem here is that I have two different types of form, Office2007Form and Office2007RibbonForm, so I should implement my code twice:
Public Class MyBaseForm
Inherits Library.Office2007Form
Public Property MyProp As String = ""
Public Sub New()
MyBase.New
Me.Icon = My.Resources.appicon
End Sub
Private Sub Form_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F12 Then Suggestions(Me.Text)
End Sub
End Class
Public Class MyBaseRibbonForm
Inherits Library.Office2007RibbonForm
Public Property MyProp As String = ""
Public Sub New()
MyBase.New
Me.Icon = My.Resources.appicon
End Sub
Private Sub Form_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F12 Then Suggestions(Me.Text)
End Sub
End Class
Since vb.net/c# don't support multiple inharitance, I can't find a way to implement this code only once.

Access a base class property in inheritance class

I'm using the base class Button in VB.net (VS2017) to create a new class called CDeviceButton. The CDeviceButton then forms as a base for other classes such as CMotorButton, CValveButton.
I want to set the Tag property in the child class CMotorButton but access it in the constructor in CDeviceButton. Doesn't work for me. It turns up being empty.
The Tag is set in the standard property when inserting the CMotorButtom instance into a form.
I've also tried to ensure teh the parent classes' constructors are run by setting mybase.New() as the first action in each constructor but that didn't change anything.
Any ideas for improvements?
Public Class CDeviceButton
Inherits Button
Public MMIControl As String = "MMIC"
Public Sub New()
MMIControl = "MMIC" & Tag
End Sub
End class
Public Class CMotorButton
Inherits CDeviceButton
Sub New()
'Do Something
end Sub
End Class
When you try to concatenate Tag with a string, you are trying to add an object that is probably nothing. I set the Tag property first and used .ToString and it seems to work.
Public Class MyButton
Inherits Button
Public Property MyCustomTag As String
Public Sub New()
'Using an existing Property of Button
Tag = "My Message"
'Using a property you have added to the class
MyCustomTag = "Message from MyCustomTag property : " & Tag.ToString
End Sub
End Class
Public Class MyInheritedButton
Inherits MyButton
Public Sub New()
If CStr(Tag) = "My Message" Then
Debug.Print("Accessed Tag property from MyInheritedButton")
Debug.Print(MyCustomTag)
End If
End Sub
End Class
And then in the Form
Private Sub Test()
Dim aButton As New MyInheritedButton
MessageBox.Show(aButton.Tag.ToString)
MessageBox.Show(aButton.MyCustomTag)
End Sub
Below is my solution I came up with that works. Basically I make sure that all initialization has taken place before reading the Tag property. What I experienced is that the Tag property is empty until the New() in CMotorButton has completed, even though the Tag property has been set when creating the instance of CMotorButton in the Form. TimerInitate has a Tick Time of 500 ms.
Not the most professional solution but works for what I need at the moment.
Another option could be multi threading but that I haven't tried and leave that for future tryouts.
Public Class CDeviceButton
Inherits Button
Public MMIControl As String = "MMIC"
Public Sub New()
TimerInitiate = New Timer(Me)
End Sub
Private Sub TimerInitiate_Tick(sender As Object, e As EventArgs) Handles TimerInitiate.Tick
If Tag <> Nothing Then
TimerInitiate.Stop()
MMIControl = "MMIC" & Tag
End If
End Sub
End class
Public Class CMotorButton
Inherits CDeviceButton
Sub New()
'Do Some stuff
TimerInitiate.Start()
End Sub
Private Sub CMotorButton_Click(sender As Object, e As EventArgs) Handles Me.Click
End Class

Passing values of an object between forms in VB.NET

Here's my problem.
I'm making a project in VB.NET that (currently) exists out of 1 class (let's call it User.vb here) and 2 WinForms (frmDisplay & frmMain).
Let's say User.vb is currently looking like this:
Public Class User
Private mName As String
Public Sub New(ByVal name As String)
Me.Name = name
End Sub
Public Property Name As String
Get
Return mName
End Get
Set(value As String)
mName = value
End Set
End Property
End Class
Let's also say the form frmDisplay is just a form with a textfield txtString and a button btnSend.
Public Class frmDisplay
Dim usr As New User()
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
usr.Name = txtString.Text
frmMain.Show()
Me.Hide()
End Sub
End Class
On the form frmMain I want to reach the value in the property Name that I stored in the class User on the first form.
The basic idea is (I know it doesn't work):
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblStoredString.Text = usr.Name << This is where I'm stuck
End Sub
End Class
I googled my problem and read many posts, but I just can't seem to understand it. Maybe you guys can help me. I am new to VB.NET and WinForm-stuff (about 3 months of exp.), but I have done some programming in the past in C# with webapplications.
Every bit of help is greatly appreciated.
Thanks a lot in advance!
Will there only ever be one User.Name that you are interested in throughout the app?
If yes, then change the class to:
Public Class User
Public Shared Name As String
End Class
Then you can use User.Name from any form (or anywhere in the application) to get/set that value.
Note that you can still wrap the field in a property if you like:
Public Class User
Private Shared _Name As String
Public Shared Property Name As String
Get
Return _Name
End Get
Set(value As String)
If (value.Trim <> "") Then
_Name = value.Trim
End If
End Set
End Property
End Class
My focus is ASP.NET, and I prefer C#, but I'll chime in. There are numerous ways of providing data between the forms. The first one that comes to mind is to use a cache of some kind. The idea is that once the cache is made available to your program, you can add the value to the cache when the button is clicked, and then safely read the value whenever you need it. This can be a static class with a Dictionary, or you can look into using the functionality provided by the System.Web.Caching namespace. http://www.codeproject.com/Articles/8977/Using-Cache-in-Your-WinForms-Applications has an example.
Another way would be to use a shared data source. The concept is similar to the caching, but this would allow you to pass more complex relational data between your forms, assuming your real goal is more complicated than you describe. Here is a walkthrough for that: https://msdn.microsoft.com/en-us/library/ms171925.aspx.
You could be quick and dirty, and write the values to a text file at some location, and then read the values from the second form.
The simplest way is probably to define a custom constructor for the second form, and pass the values you need when you instantiate the second form. This is best suited if the values from the first form can be considered "parameters" to the instance of the second form. Passing a textbox value from one form to another in windows application
Declare the usr variable Friend
Public Class frmDisplay
Friend usr As New User()
It will then be available from the other form
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblStoredString.Text = frmDisplay.usr.Name
End Sub
End Class
It's a quirk of VB.NET that forms are automatically created with a public variable name the same as the class name. That's why you are able to use frmMain without having to create it (e.g. Dim frmMain as New frmMain). You can turn off this behaviour, but it isn't relevant to your problem.
On the other hand, if you want to do it "properly"...
Public Class frmDisplay
Private usr As User
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
usr = New User(txtString.Text)
Dim f As New frmMain(Me, usr)
f.Show()
Me.Hide()
End Sub
End Class
and frmMain...
Public Class frmMain
Private myParent As Form
Private usr As User
Sub New(parent As Form, _usr As User)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
usr = _usr
myParent = parent
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = usr.Name
End Sub
Private Sub frmMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
myParent.Show()
End Sub
End Class
Here we instantiate frmMain and pass the User object to its constructor. We also pass the calling form so we can display it again when frmMain is closed.

How can I access form.aspx from codefile.aspx.vb?

I have a formview on a page called form.aspx and it of course has a code-behind page called form.aspx.vb
The form.aspx.vb file is huge! So I'd like to take the functions out of the form.aspx.vb page and into functions.vb.
My problems is everything goes out of scope.
example....
form.aspx.vb has this...
dim box1, box2, box3 as Textbox
Public Sub initialiseControls()
box1 = Me.Formview1.FindControl("box1")
box2 = Me.Formview1.FindControl("box2")
box3 = Me.Formview1.FindControl("box3")
End Sub
I'd like to take this sub and put it into functions.vb codefile, but everything is out of scope then.
Can someone tell me if this can be done?
Thanks.
Two options:
1) Pass a reference to the Page into every method that needs to use it:
In code behind:
ExTest.ModifyControl(Me.Page)
New class with various methods in:
Public Class ExTest
Public Shared Sub ModifyControl(aPage As System.Web.UI.Page)
Dim tb As TextBox = CType(aPage.FindControl("txthelloWorld"), TextBox)
tb.Text = "Hello World"
End Sub
End Class
2) Extend the code behind as a partial class:
Current code behind (add the Partial keyword):
Partial Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ModifyControl()
End Sub
End Class
Add a New class:
Partial Public Class WebForm1
Private Sub ModifyControl()
txtGoodbyeWorld.Text = "Goodbye"
End Sub
End Class

Form data erased after Windows form is closed

I have a subsidiary form where I can enter data and then save it before closing the form and going back to using the main form.
When I re-open the subsidiary form, I cannot see the changes in the data that I had entered earlier.
Can anyone tell me where I'm wrong ?
MainForm.vb
Public Class Maincls
oTestObj as New Testcls
oTestObj.XYZ = "XYZ"
Private Sub SoftwareSettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SoftwareSettingsToolStripMenuItem.Click
Testcls.tbXYZ.Text = oTestObj.m_XYZ
Testcls.Show()
End Sub
End Class
Form_Testcls.vb
Public Class Testcls
Structure Params
Dim m_XYZ as String
End Structure
Dim oParams as Params
Public Sub New ()
InitializeComponent()
End Sub
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
XYZ = tbXYZ.Text
Me.Hide()
End Sub
Public Property XYZ() As String
Get
Return Me.oparams.m_XYZ
End Get
Set(ByVal value As String)
Me.oparams.m_XYZ = value
End Set
End Property
End Class
I think in windows forms the work around for this is to create a static class and add properties according to your requirement. Then populate these static properties on closing of your form. Now you can use the value set in the static data members, unless otherwise you change them on any other event.
Edit: In vb.net the Static is actually NonInheritable