I have tried looking for a similar problem but I haven't found any for VB.
I have multiple forms, all of which with their own design and function. Form1 creates a StreamWriter file and will launch the other forms. Form2 and so on need to be able add to the same file, however I cannot access the file from the other forms.
I have tried declaring the file as Public in Form1, and also I have tried using:
Form1:
Dim newForm As New Form2(myFile)
newForm.show()
Form2:
Public Sub New(ByVal myFile As StreamWriter)
Me.myFile = myFile
End Sub
but this will load Form2 without any of the controls I put on it in design mode.
You are missing the InitializeComponent() method
Public Sub New(ByVal myFile As StreamWriter)
InitializeComponent()
Me.myFile = myFile
End Sub
For more info read Very Simple definition of InitializeComponent(); Method
Related
Getting the error "Event 'Load' cannot be found" referring to "Handles MyBase.Load" Please see attached code. Any help much appreciated!
I have many other applications set up the same way and they all work. However, these were in an older version of Visual Studio.
Option Explicit On
Option Strict On
Imports System, System.IO
Imports System.Text
Public Class Form1
Private Sub cleanXMLDialog_Load(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles MyBase.Load
Main()
End
End Sub
Public Sub Main()
Dim directories() As String = Directory.GetDirectories("C:\")
Dim files() As String = Directory.GetFiles("C:\", "*.dll")
DirSearch("c:\")
End Sub
Sub DirSearch(ByVal sDir As String)
Dim d As String
Dim f As String
Try
For Each d In Directory.GetDirectories(sDir)
For Each f In Directory.GetFiles(d, "*.xml")
'Dim Response As String = MsgBox(f)
Debug.Write(f)
Next
DirSearch(d)
Next
Catch excpt As System.Exception
Debug.WriteLine(excpt.Message)
End Try
End Sub
End Class
Load should happen without this error.
This is the class declaration:
Public Class Form1
It looks like you intend this to inherit from a windows Form type, but there's nothing here to make that happen.
You may want this:
Public Class Form1 Inherits System.Windows.Forms.Form
but even this is unlikely to really accomplish anything. It's not enough just to inherit from the Form type if you don't have any controls are properties set and don't ever show the form.
Did you accidentally create a Console or Class Library project when you mean to create a WinForms project?
I wrote the below-mentioned code from an amalgamation of c# tutorials about redirecting Console.Write or Console.WriteLine to a textbox text field. I realized this was bad since if I invoke any kind of .Close() method will erase the textbox which is the opposite of what I want to do.
Imports System.Text
Imports System.IO
Namespace ConsoleRedirection
Public Class TextBoxStreamWriter
Inherits TextWriter
Private _output As TextBox = Nothing
Public Sub New(ByVal output As TextBox)
_output = output
End Sub
Public Overrides Sub WriteLine(ByVal value As String)
MyBase.WriteLine(value)
_output.AppendText(String.Format("[{0}] {1}" + vbNewLine, DateTime.Now, value.ToString()))
End Sub
Public Overrides ReadOnly Property Encoding As Encoding
Get
Return System.Text.Encoding.UTF8
End Get
End Property
End Class
End Namespace
Is there anyway, besides loading a text file, that I can have all Console output redirected to either a textbox?
To answer a question from comments. (This code was originally found here: https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/... But was converted over to VB)
In a separate form, the code is invoked by declaring a TextWriter.
Private _writer As TextWriter = New TextBoxStreamWriter(frmDebugLog.txtDebugLog)
Then as part of the .Load of a parent form:
Console.SetOut(_writer)
On a later portion of the form, the frmDebugLog is invoked by using frmDebugLog.Show()
When that window, frmDebugLog, is closed... that form and its textbox contents are discarded.
It appears that you're using the default instance and so only ever one instance at a time. In that case, this will do the job:
Private Shared txtDebugLogText As String
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
txtDebugLog.Text = txtDebugLogText
End Sub
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
txtDebugLogText = txtDebugLog.Text
MyBase.OnFormClosed(e)
End Sub
By using a Shared field, you keep everything within the one class. The current value of that field is loaded into the TextBox first whenever a new instance is created and the text in the current TextBox is persisted to that field whenever an instance is destroyed.
In my application I have to show lot of forms when particular button/panels are clicked. so instead of writing
Frm = New formname
Frm.MdiParent = MDIParent
Frm.Show()
i want to have public function through which i can pass the form name.
for that i have written a function
Public Sub showForm(ByVal formname As Form)
Frm = New formname
Frm.MdiParent = MDIParent1
Frm.Show()
End Sub
Call showForm(myformname)
but problem with this is, it says formname is not defined
EDIT:
I updated my answer to reflect your comment that a form should only be opened once.
I want to have public function through which i can pass the form name.
for that i have written a function
Public Sub showForm(ByVal formname As Form)
You don´t pass the name of a form to your function but an object of type Form instead.
Here is one possible solution with a generic version of showForm:
Public Class FormManager
Private _formByName As New Dictionary(Of String, Form)
Public Sub showForm(Of T As {Form, New})(name As String, parent As Form)
Dim frm As Form = Nothing
If Not _formByName.TryGetValue(name, frm) OrElse _formByName(name).IsDisposed Then
frm = New T()
_formByName(name) = frm
End If
frm.MdiParent = parent
frm.Show()
End Sub
End Class
The FormManager holds a dictionary cache for all opened forms with Key=form name. This is to make sure that a form is only opened once. The check form.IsDisposed makes sure that you can close the form and reopen it.
Usage from the parent form:
Public Class Form1
Private fm = New FormManager()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
fm.showForm(Of MyForm)("MyForm", Me)
End Sub
End Class
The first parameter is to identify the form name. The real magic is in the Type T which we made sure it is 1) of type or subtype Form and 2) it has a parameterless constructor (MyForm is just a placeholder for this example put in the type of your real form you want to show).
The parent parameter will bring you additional flexibility if it is not always MDIParent1. Remove it if you don´t neet the extra flexibility.
For sure you can also drop the FormManager class and put the showForm to another place.
Quick question about a windows service, I've added a setup project for my Windows service, and ive added a custom dialog to it, with 4 text fields, but my question is how to i get these informations/variables?
Ive also added an installer for the windows service also, and afterwards the setup project, with the custom dialog.
The informations is something like database connection strings, and so on - so just string values.
This is my code for the "project installer" . the installer item ive added for the windows serive if you wanted to see it.
Imports System
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.Runtime.InteropServices
Public Class ProjectInstaller
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
My.Settings.TestSetting = Context.Parameters.Item("PathValue")
#If DEBUG Then
Dim ServicesToRun As ServiceBase()
ServicesToRun = New ServiceBase() {New tdsCheckService()}
ServiceBase.Run(ServicesToRun)
#Else
Dim listener As New tdsCheckService()
listener.Start()
#End If
End Sub
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
Dim regsrv As New RegistrationServices
regsrv.RegisterAssembly(MyBase.GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase)
End Sub
Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
MyBase.Uninstall(savedState)
Dim regsrv As New RegistrationServices
regsrv.UnregisterAssembly(MyBase.GetType().Assembly)
End Sub
Private Sub ServiceProcessInstaller1_AfterInstall(sender As Object, e As InstallEventArgs) Handles ServiceProcessInstaller1.AfterInstall
End Sub
Private Sub ServiceInstaller1_AfterInstall(sender As Object, e As InstallEventArgs) Handles ServiceInstaller1.AfterInstall
End Sub
End Class
You should eb able to access them using the Context object.
'Get Protected Configuration Provider name from custom action parameter
Dim variableName As String = Context.Parameters.Item("dialogSettingName")
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
I have a form with a tabcontrol and 4 tabs. I want to open a form with showdialog in a predetermined tab.
I've tried
OptionsForm.OPTS_TabControl1.SelectTab(1)
OptionsForm.OPTS_TabControl1.ShowDialog()
but it didn't work.
Any help? thanks
First Kudos for using Stackoverflow. It shows you paid attention to class =D
regarding your question, that piece of code you showed should be working. You should provide the actual error so we can try to figure out.
Does OptionsForm refer to the class or an object of a class you created?
Anyways, try to create an object of the form and then set the starting tab, like this:
Dim OptionsObject As New OptionsForm
OptionsObject.OPTS_TabControl1.SelectTab(1)
OptionsObject.OPTS_TabControl1.ShowDialog()
Another solution might be Overloading the Showdialog method, although it seems kind of an overshot.
Here's how:
Inside your OptionsForm Code:
Public Overloads Sub Showdialog(ByRef TabNumber As Integer)
OPTS_TabControl1.SelectTab(TabNumber)
Return MyBase.ShowDialog()
then call the form using
optionsform.showdialog(1)
Note: Overloading is basically creating another instance of a subrotine that accepts different arguments. read the pages 342-358 of the manual if you wish to know more.
Since you are getting a NullReferenceException, you should separate out the call to see where the null object is. Is the tab control null?.
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private tabControl1 As TabControl
Private tabPage1 As TabPage
Private tabPage2 As TabPage
Private Sub MyTabs()
Me.tabControl1 = New TabControl()
Me.tabPage1 = New TabPage()
Me.tabPage2 = New TabPage()
Me.tabControl1.Controls.AddRange(New Control() {Me.tabPage1, Me.tabPage2})
Me.tabControl1.Padding = New Point(15, 10)
Me.tabControl1.Location = New Point(35, 25)
Me.tabControl1.Size = New Size(220, 220)
' Selects tabPage2 using SelectedTab.
Me.tabControl1.SelectedTab = tabPage2
Me.tabPage1.Text = "tabPage1"
Me.tabPage2.Text = "tabPage2"
Me.Size = New Size(300, 300)
Me.Controls.AddRange(New Control() {Me.tabControl1})
End Sub
Public Sub New()
MyTabs()
End Sub
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class