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

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

Related

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

Access Master Page Variable From Content Page VB

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

Sharing variables across classes and forms

I have a class called DataStorage that stores all of the publicly declared variables that I use in my program. My main class calls a public array from DataStorage and fills it with values. The main class then calls up a windows form and populates the textboxes on it with the values in the array. However, when I run the program, the form comes up with blank textboxes. I don't understand why this would be the case. I also tried populating the textboxes from the load function of the form rather than in the main class. I debugged and stepped through my code, and the main class filled the array with values fine. But when the form was called, and I started stepping through the code to populate the textboxes, it said the array was equal to nothing. If the array is stored in a separate class that both the main class and the form call, why are the values getting set to null between the main class and the form?
Here is what my code looks like:
Public Class Main
Private ds As New DataStorage
Public Sub Test()
For i = 0 to 2
ds.Info(i) = "Hello"
Next
Form1.ShowDialog()
End Sub
End Class
Public Class DataStorage
Public Info() As String
End Class
Public Class Form1
Private ds As New DataStorage
Private Sub Form1_Load(sender As Object, e As EventAgrs) Handles MyBase.Load
Textbox1.Text = ds.Info(0)
Textbox2.Text = ds.Info(1)
Textbox3.Text = ds.Info(2)
End Sub
I just called it Form1; the program starts with the sub Test in the main class.
I just need a way to have variables/arrays that are accessible by any class or form and don't change value or get set to null unless I tell it to.
You must give your array a size before you try to fill it.
Public Class TestArray
Private ds As New DataStorage
Private Sub TestArray_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillArray()
TextBox1.Text = ds.Info(0)
TextBox2.Text = ds.Info(1)
TextBox3.Text = ds.Info(2)
End Sub
Public Sub FillArray()
For i = 0 To 2
ds.Info(i) = "Hello"
Next
End Sub
End Class
Public Class DataStorage
Public Info(2) As String
End Class
If you don't want to give a size then use a List(Of T)
Public Class TestArray
Private ds As New DataStorage
Private Sub TestArray_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillList()
TextBox1.Text = ds.Info(0)
TextBox2.Text = ds.Info(1)
TextBox3.Text = ds.Info(2)
End Sub
Public Sub FillList()
For i = 0 To 2
ds.Info.Add("Hello")
Next
End Sub
End Class
Public Class DataStorage
Public Info As New List(Of String)
End Class

referencing form button from another form

I have two forms both with the same buttons on, and I want to have it so that if I click the button both buttons will do the same thing i.e. they are referencing each other on different forms. the way i found was:
Public Class Form2
Dim form1 As New form1
Private Sub Button2_Click
form1.backcolor=black
form2.backcolor=black
end sub
end class
then
Public Class Form1
Dim form2 As New form2
Private Sub Button1_Click
form1.backcolor=black
form2.backcolor=black
end sub
end class
only this doesn't work as there is an error:An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll as far as i can see there is no infinite loop or stack over flow.
any help would be greatly appreciated.
You have an infinite loop, because each time one of the forms is instantiated, it is instantiating the other. Creating a Form1 will create a Form2, then Form2 immediately creates another Form1 and so on and so on...
Change your code to this:
Public Class Form2
Private Sub Button2_Click
Dim form1 As New Form1
form1.backcolor=black
form2.backcolor=black
End sub
End class
Public Class Form1
Private Sub Button1_Click
form1.backcolor=black
Dim form2 As New Form2
form2.backcolor=black
End sub
End class
Now it will only create the other class instances when you click a button.
Like Karl Anderson said, there is a infinite loop in your code. His solution will create a new form every time you click the button. If you don't want this behavior, I think that the best approach is to use the mediator pattern. And it will be much more easy if you want to add new actions and new forms.
The code will look something like this:
Public Class Mediator
Private forms As New List(Of BaseForm)
Public Sub RegisterForm(form As BaseForm)
forms.Add(form)
End Sub
Public Sub ChangeAllFormsBackColorToBlack()
For Each form In forms
form.ChangeBackColorToBlack()
Next
End Sub
End Class
Public Class BaseForm
Private med As Mediator
Public Sub New(med As Mediator)
Me.med = med
Me.med.RegisterForm(Me)
End Sub
Public Sub ChangeBackColorToBlack()
backcolor = black
End Sub
Public Sub OnButtonClick()
Me.med.ChangeAllFormsBackColorToBlack()
End Sub
End Class
Public Class Form2
Inherits BaseForm
Public Sub New(med As Mediator)
MyBase.New(med)
End Sub
Private Sub Button2_Click()
Me.OnButtonClick()
End Sub
End Class
Public Class Form1
Inherits BaseForm
Public Sub New(med As Mediator)
MyBase.New(med)
End Sub
Private Sub Button1_Click()
Me.OnButtonClick()
End Sub
End Class
Module MediatorDemo
Sub Main()
Dim med As New Mediator
Dim f1 As New Form1(med)
Dim f2 As New Form2(med)
f1.OnButtonClick()
End Sub
End Module

How do I render the inner properties of my User Control on the page?

I am designing a user control that attempts to create a filter bar with various TextBox or DropDownList elements on the page according to the sample markup below:
<gf:GridFilterBar runat="server">
<filters>
<filter Label="Field1" Type="TextBox" />
<filter Label="Field2" Type="DropDownList" />
</filters>
</gf:GridFilterBar>
Using inspiration from another post, I have created code behind that properly parses this markup and reads in the properties of each intended child control. The issue I am having is when it comes time to actually render this information on the screen. Every control I initialize from within the "New" sub of the "Filter" class never appears on the screen. When I place a breakpoint in the "New" sub and follow what is happening, I can see the Filter.New sub being traversed twice and the values being read in, but nothing else I initialize from within that sub has any effect on the page even though, as far as I can tell, it is all being created successfully. Here is a sample of the code with just the Label property being read:
Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class GridFilterBar
Inherits System.Web.UI.UserControl
Private _Filters As New FiltersClass(Me)
<PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property Filters() As FiltersClass
Get
Return _Filters
End Get
End Property
Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
DDL.Visible = True
End Sub
End Class
Public Class FiltersClass
Inherits ControlCollection
Public Sub New(ByVal owner As Control)
MyBase.New(owner)
End Sub
Public Overrides Sub Add(ByVal child As System.Web.UI.Control)
MyBase.Add(New Filter(child))
End Sub
End Class
Public Class Filter
Inherits HtmlGenericControl
Public Sub New(ByVal GenericControl As HtmlGenericControl)
Label = GenericControl.Attributes("Label")
Dim lit As New Literal
lit.Text = Label.ToString
Me.Controls.Add(lit)
End Sub
Public Property Label As String = String.Empty
Public Overrides Function ToString() As String
Return Me.Label
End Function
End Class
Can anyone spot what I'm doing wrong?
I was able to answer my question. I added an override sub for CreateChildControls in my main class and used a For Each loop to grab the properties set from each newly initialized "Filter"
Protected Overrides Sub CreateChildControls()
For Each filter In Filters
Dim lit As New Literal
lit.Text = filter.Label
Controls.Add(lit)
Next filter
End Sub
This relegated the Filter.New sub to simply grabbing the properties:
Public Sub New(ByVal GenericControl As HtmlGenericControl)
Label = GenericControl.Attributes("Label")
End Sub