UserForm won't show - vb.net

I'm using office developer tools and made a ribbon to access some functions. Thing is, it looks like I can't open a userform from a button, other commands seems to run normally.
Code:
Public Class Empresa
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles btn_DBSol.Click
'Dim wnd As New frm_DBSolventes
'wnd.Show()
MsgBox("Hello World")
End Sub
End Class
This code have this result on excel ribbon:
https://s24.postimg.org/6z16l6g43/Print_1.jpg
Now using this code:
Public Class Empresa
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles btn_DBSol.Click
Dim wnd As New frm_DBSolventes
wnd.Show()
'MsgBox("Hello World")
End Sub
End Class
Results in nothing:
There are no errors on Error List window. frm_DBSolventes is a userform on a userform referenced project, there is nothing on the form right now, just made a new project of userform and trying to show it. Is there something I'm missing? Is there any other way where I can use a userform on ribbon?
As Requested the frm_DBSolventes is
https://s29.postimg.org/6w6ae15qd/Print_3.jpg
Just add a datagridview cause I need to continue working. if it makes difference I can change it. There is no code on the form:
Public Class frm_DBSolventes
End Class

Try to show it as a modal Window.

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.

Raise an event on another form

I have two separated form(usercontrol A & B) in VB.Net. When a button clicked on formB, I want to an event raised on formA. How can do that? I searched stackoverflow.com and check several related solutions but they didn't work for me. I don't know what is the problem! Please help me.
For Example I tried this code:
FormA:
Dim MyEditForm As New ucEmployeeTimeEdit
'some code for showing and preparing MyEditForm
Private Sub GetSomeClick() Handles MyEditForm.MyClick
System.Console.WriteLine("test")
End Sub
FormB:
Public Event MyClick()
Private Sub BtnDelet_Click(sender As Object, e As EventArgs) Handles btnDelet.Click
RaiseEvent MyClick()
End Sub
While Idle_Mind answer is great, here's an alternative method with different advantages. (EDIT: I just noticed that he mentions this too, so you can consider this an elaboration on this subject)
If you keep FormB as a modal variable inside FormA, you can tell VB that you want to use it's events. Here's some skeleton code to show you:
Public Class FormA
Private WithEvents myFormB As FormB
'rest of the code for FormA
'you can use FormB's events like this
Private Sub GetSomeClicks() Handles myFormB.MyClick
'code code code
End Sub
End Class
Public Class FormB
Public Event MyClick()
End Class
Have fun!
Somewhere in the some code portion, you need to use AddHandler to "wire up" that event:
Public Sub Foo()
Dim MyEditForm As New ucEmployeeTimeEdit
'some code for showing and preparing MyEditForm
AddHandler MyEditForm.MyClick, AddressOf GetSomeClick
End Sub
Private Sub GetSomeClick()
System.Console.WriteLine("test")
End Sub
Note that GetSomeClick() does not have a "Handles" clause on the end. This is because your instance of ucEmployeeTimeEdit was a local variable.
If you had declared it as WithEvents at class level, then you could use the "Handles" clause (and consequently would no longer need the AddHandler call).

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)

Create And Implement a Custom Form in VB.NET

I am trying to create a customized form class (CustomBorderlessForm) in VB.NET.
My Progress So Far
I created a new Class and named it CustomBorderlessForm.vb
I then proceeded to write the following code:
CustomBorderlessForm.vb
Public Class CustomBorderlessForm
Inherits Form
Dim _form As Form = Nothing
Public Sub New(form As Form)
_form = form
MsgBox("Testing: New()")
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
MsgBox("Testing OnMouseMove()")
End Sub
End Class
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
End Sub
End Class
Results of progress
A message box displays "Testing: New()" on load
Nothing shows on mouse move
As you can see, my problem lies with the events
Questions
Is it possible to create a form object and use that instead of the pre-populating form?
If so, can I give this form custom properties, such as, a border and some boolean values (shadow...etc), just like any other custom object/class?
What am I doing wrong in my current approach?
Why isn't the OnMouseMove being overridden?
Am I initialising the class wrong?
Can it even be done this way?
After creating a form you also need to show it. Change your logic to:
Dim form As New CustomBorderlessForm(Me)
form.Show()
Before you do that, I'd recommend changing from MsgBox to Console.WriteLine(), otherwise you can run into a fun/frustrating little cat and mouse game.
EDIT
Based on the comments, if, from VS you did a "Add New, Windows Form" you can just right-click the project, select property and on the Application tab change the Startup object to your new form. VS only allows you to do this with forms it creates for you (by default, more on this later).
If you wrote that file by hand (which is absolutely fine) you can perform the Show() like I did above and call Me.Hide() to hide the "parent" form. Unfortunately the Load event is fired before the Show event so if you place this in Form1_Load() it won't work. Instead you can use the Shown event like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
form.Show()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Hide()
End Sub
Another option has to do with "Application framework". You can read about it here however it basically handles application events that other languages have to manually implement. If you go into your project properties you can uncheck the "Enable application framework" checkbox. This will give you more option in the "Startup object" dropdown. If you add the following code to your project one of the items in the Startup object dropdown menu should now be "Loader"
Public Module Loader
<STAThread()>
Public Sub Main()
Dim form As New CustomBorderlessForm(Nothing)
form.ShowDialog()
End Sub
End Module
You'll notice that the above bypasses Form1 completely. Also, instead of Show() I'm using ShowDialog() because otherwise the form shows and then the program ends.

VB.NET Multithreading. Calling invoke on a UI Control from a class in a separate class file

I've been trying to understand this for a few days now and wonder if it's something simple I'm missing or doing completely wrong.
Example:
Two files - TestClass.vb, myForm.vb
TestClass.vb looks as follows:
Imports System.Threading
Public Class TestClass
Private myClassThread As New Thread(AddressOf StartMyClassThread)
Public Sub Start()
myClassThread.Start()
End Sub
Private Sub StartMyClassThread()
myForm.Msg("Testing Message")
End Sub
End Class
myForm.vb is a basic form with a listbox control and a button control named respectively Output and StartButton.
The code behind the form is as follows:
Public Class myForm
Private classEntity As New TestClass
Private Sub StartButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles StartButton.Click
Msg("Start Button Pressed")
classEntity.Start()
End Sub
Delegate Sub MsgCallBack(ByVal mesg As String)
Public Sub Msg(ByVal mesg As String)
If Output.InvokeRequired Then
MsgBox("Invoked")
Dim d As New MsgCallBack(AddressOf Msg)
Invoke(d, New Object() {mesg})
Else
MsgBox("Not Invoked")
mesg.Trim()
Output.Items.Add(mesg)
End If
End Sub
End Class
The result:
Application runs, no errors or exceptions.
Displayed is the listbox and the Start button.
I press the start button and a msgbox says "Not Invoked" as expected and upon clicking OK to that msgbox "Start Button Pressed" is added to the Output listbox control.
Immediately following that the msgbox pops up again and says "Not Invoked". I was expecting "Invoked" as a separate thread is trying to use the Output listbox control.
Of course this results in the Output.Items.Add being attempted which results in no visible result as the thread is not allowed to directly update the UI control.
I must've read a small books worth of different pages trying to thoroughly understand pit falls and methods but I feel I may have fallen into a trap alot of people may do.
With my current understanding and knowledge I'm unable to get out of that trap and would appreciate any input or advice.
Kind regards,
Lex
The problem here is that you are calling the function Msg not on an instance of myForm, but as a shared function of the class myForm.
Change your code in TestClass to add
Public FormInstance as myForm
and then replace
myForm.Msg("Testing Message")
with
FormInstance.Msg("Testing Message")
Then in StartButton_Click add the line
classEntity.FormInstance = Me
and you will get the expected result.