Accessing text box on Form1 from Form2 - vb.net

I am new to vb.net so forgive me if this is an easy question.
I have a created a class library project that houses two windows forms, Form1 and Form2. The main class library has the event to open Form1. A button on Form1 launches Form2. The bulk of the code is in Form1, which I don't want to change if I can help it.
What I am trying to do, is access a sub that is on Form1 from Form2. That sub is changing the value of a text box on Form 1. I don't get any errors when I compile the project, however, nothing happens.
Here is an example
Form1:
Public Sub test()
Me.Panel1.Controls("Textbox1").Text = "Test"
End Sub
Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MainForm As New Form1
Me.Close()
MainForm.test()
End Sub
Don't get too caught up on how I built it out, I have tried about 20 different things and this is where I am at now.
I have tried defining Form1 in the sub test(). I have tried setting sub test() to shared. I have tried closing Form2 and activating Form1. I have tried changing the modifiers property on the text box to public. I have tried making Form1 the parent and Form2 a child (I honestly don't understand MDI very much). All these results end up in a project that will compile but wont give me any results. My code accesses the sub just fine, it wont access the text box's text property.
Any suggestions will help. I am trying to access the text boxes in a way that I can loop through all of them. For example: Me.Panel.Controls("Textbox" & i).Text = "Something". Also I would like to keep the sub in the class for Form1 if I can.
Any suggestions would be great!

You are creating a brand new Form1 in Form2, that is the problem.
Just use :
Call Form1.test()
By the way, I think this code in the sub is an easier way to set the text:
Panel1.TextBox1.Text = "Test"

Related

Filling RichTextBox works from click-Event only

I have a RichTextBox in a Form which I try to fill from a Module; I tried already a few different ways to get it to work, but I can't figure out what the actual problem is?
In my module I have an instance::
Dim Protokoll_UI As New Form1
With this I fill the RichTextBox directly.
Protokoll_UI.RichTextBox.Text = File.ReadAllText(filename)
I already tried to call a method in class Form1 from the module, too, but it didn't have any impact on it:
Module Module1
Public Sub get_protokoll()
Protokoll_UI.Protokoll()
End sub
End Module
Class Form1
Public Sub Protokoll()
Protokoll_UI.Text = File.ReadAllText(Dateiname)
End Sub
End Class
The funny thing is that I have a ToolStripMenuItem.Click Event in the Class as well in which I update the RichTextBox, and it doesn't matter how I fill the RichTextBox, I can call a sub or fill it directly, it works perfectly:
Private Sub UpdateToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AktualisierenToolStripMenuItem.Click
Protokoll()
End Sub
Even changing the WordWrap property of the RichTextBox did not help. At the moment I have absolutely no idea where I could look to solve this problem.
Btw. this is the result when I search for the RichTextBox in the whole project.
In your module, just do:
Form1.RichTextBox.Text = File.ReadAllText(filename)
and remove the Dim Protokoll_UI As New Form1 from it.
You can call the Protokoll() function in the module from Form1 because it's in a Module; modules are global, and usable by all forms/classes in the project.

ShowDialog() in vb.net not working properly

I'm not sure if this question has been already posted or not but since I couldn't find the answer I'm looking for so I'll ask anyways.
When I use ShowDialog() to call another form from the current form it just displays the name of the controls the first time it opens, if I close it and open it again then it displays at it should have. For example if I write a simple code as follows
Form1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Display("Message")
End Sub
End Class
Form2
Public Class Form2
Public Sub Display(ByVal msg As String)
Me.ShowDialog(Parent)
Label1.Text = msg
End Sub
End Class
When I click the button the first time all I get is "Label1" instead of "Message" displayed on the called form but when I click on the button a second time I get the desired output.
I tried the same thing with Show() and didn't have any such problem. But I need the called form to be modal (like a MessageBox), so is there any other way to do it or can someone tell me the right way to use ShowDialog() ?
I'm new to vb so please keep it simple.
Thanks
ShowDialog is a blocking call. You need to set everything before you call it.

VB Adding an item to listbox on a different form giving null reference exception

I am getting a an Null reference exception when trying to add an item to a listbox in a different form.
This is my error at run time.
An unhandled exception of type 'System.NullReferenceException'
occurred in ... Additional information: Object reference not set to an
instance of an object.
I am trying to connect the Mainform by initializing it at the top of the class of the secondForm. after I have my data i want to add it to a listbox it the mainform.
Public Class FormHairdresser //The second form
Dim varMainForm As FormMain //connecting the forms ?
Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click
hairdresser = HairdresserChoices(HairdresserID) // get the data
varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.
All i had to to was write the form name instead of ininalised variable.
FormMain.lstListbox.Items.Add("item")
Instead of
Dim varMainForm As FormMain
varMainForm.ListBox.Items.Add("item")
You cannot simply create a new instance of your main form (as has been suggested and expect that to work, you need an actual reference to the mainform that you have created. To help you see the logic involved;
Create a new Winforms project. In the default Form1 add a textbox and a button.
Now add a new form to this application (you can leave it with its default name of Form2. To this form add a TextBox (call it myTextBox) and a button.
Now go back to your first form and doubleclick the button to access the click handler in code. Add the following:
Dim frm as New Form2
frm.Show
Press f5 and click the button and you'll see a new form 2. So far so good.
Now open up the code for Form2 and add the following code so that it ends up looking like this:
Public Class Form2
Private frm As Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frm.TextBox1.Text = myTextBox.text
End Sub
End Class
build, press f5 and click the button on form1, in the new form2 enter some text in the text box and click the button, you get your null reference exception. The reason you get this is because at the moment the private field frm inForm2 refers to Nothing.
Now open up the code in Form2 and add a constructor and the following code so that it ends up looking like this:
Public Class Form2
Private frm As Form1
Public Sub New(byval frm1 As Form1)
'first we should make sure that we have a parameter to play with
If Not IsNothing(frm1) Then
frm = DirectCast(frm1,Form1)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frm.TextBox1.Text = myTextBox.text
End Sub
End Class
Finally go back to your first forms buttonclick handler and change the code slightly so that it looks like this;
Dim frm as New Form2(Me)
frm.Show
Build and run your application, now when you enter text into the textbox in form2 and click the button it will appear in the textbox in Form1.
The reason why this happens is because you have passed an actual reference to the form1 that was originally created when the application started to form2. By casting that reference to your private field used to represent form1 in form2 you can then use it to properly refer to things on form1. This is a very simple concept but one which you need to learn before you will make progress programming.
the Problem is with your initialization of the formmain.with out proper initialization the object you are creating is nothing other than Null.To avoid this we use New Operator.The New operator can often be used to create the instance when you declare it.
So the initialization will look like
Dim varMainForm As New FormMain
Hope this Helps.For more Reference Object Initialization Errors
update:
Dim varMainForm As FormMain //connecting the forms ?
Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click
hairdresser = HairdresserChoices(HairdresserID) // get the data
varMainForm = New FormMain
varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.
Try This.
Public Class FormHairdresser //The second form
Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click
Dim varMainForm As FormMain
hairdresser = HairdresserChoices(HairdresserID)
varMainForm.lstListBox.Items.Add(hairdresser)

vb .net access form controls in a task

i am trying to access data from textboxes and checkboxes placed on form1 in a task running on form2.
When i access the textboxes and checkboxes within a task started in a sub of form1 everything works fine!
But if i try to use the data from the controls in a task of form2 i only get the default text (empty) of the textbox and the default checked status
The following testsub works on form1 and the right text is shown.
Public Sub testsub()
Dim testTask As New Task(Sub() MsgBox(TextBox1.Text))
testTask.Start()
End Sub
On form2 i tried this
Public Sub testsub()
Dim testTask As New Task(Sub() MsgBox(Form1.TextBox1.Text))
testTask.Start()
End Sub
This doesn't work and only an empty textbox is shown.
It seems that the standard instance of the form1 is not available in the task of form2?! Is that right?
So how can i access the control data of form1 in the task of form2?
You need your instance of Form1 declared in a place Form2 can access it.
Try adding a module :
Module Mod1
Public f1 as Form1
End Module
Then in the Form1 Load event, set f1 to the instance of Form1
f1 = Me
After Form1 has been loaded, then in Form2 you can use your sub, replacing the general Form1 with the specific f1
Public Sub testsub()
Dim testTask As New Task(Sub() MsgBox(f1.TextBox1.Text))
testTask.Start()
End Sub

Using Variables Across Forms - VB

I've got a fairly simple question (I think) on passing variables between forms using Visual Basic.
I've got a program with 2 forms (Form1 and Form2). Form1 has 3 radio buttons, which the user has to select one of and then loads Form2.
Now I've made it so that if radiobutton1 is picked, the Public Variable "radio_select" will equal "radiobutton1", if radiobutton2 is picked, "radio_select" will equal "radiobutton2".
But whenever I try call "radio_select" in my second form, it comes up blank. Why could this be? And how can I fix it.
I've tried using if form1.radiobutton1.checked = true but I keep getting the first radiobutton, regardless of the radio button I've selected.
I think the form is being unloaded, or there is an issue somewhere there, as it appears none of the variables get passed to the second form, once it has been initialized. Also note, the first form is hidden Me.Hide() when the second form is called.
Have you considered a slight re-design whereby you create a property on Form2 called RadioSelect and then set this from Form1 before showing Form2:
Class Form2
Public Property RadioSelect As String
...
End Class
...
Dim f2 as new Form2()
f2.RadioSelect = "radiobutton2"
f2.Show() ' Or f2.ShowDialog()
This gets you away from an unnecessary public variable and should also ensure Form2 can see what it needs from Form1, or whoever calls it.
Edit:
The following works for me:
Public Class Form1
Public Test As String
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Test = "I'm Here"
Me.Hide()
Form2.ShowDialog()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Text = Form1.Test
End Sub
End Class