I am just starting out with Visual basic .Net.
I can't seem to figure what's the scope of button properties like button.text. Can they be used outside the button_click event sub? And if so, how?
How can I modify button properties from a module in real time when a certain condition is met?
I'd surely appreciate some guidance and an example, if possible. Thanks.
Just as quick sample, I don't suggest doing something like this
I have 2 forms open, Form2 and Form3. Each form has a button on it.
I also have a Module, called MyModule
Public Class Form2
Public Sub ChangeButtonText(ByVal s As String)
Button1.Text = s
End Sub
End Class
.
Public Module MyModule
Sub ChangeForm2Btn()
Form2.ChangeButtonText("LOL")
End Sub
End Module
From my Form3 I click the button and call the module function to change Form2 button's text
Public Class Form3
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyModule.ChangeForm2Btn()
End Sub
End Class
You could pass a reference to the button to a sub in the module, and then call that sub from the form.
i.e.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeButtonText(Me.Button1, "Changed")
End Sub
End Class
Module modButton
Public Sub ChangeButtonText(ByRef Button As Button, ByVal Text As String)
Button.Text = Text
End Sub
End Module
Related
I'm learning for vb.net step by step. I have two forms and I would like to make a code that is, when I click Button1 which is in the other form, then my main sub exit. I have already searched in google, but I didn't find anything. How can I solve that line?
Public Class Form1
Public Sub Main()
Form2.ShowDialog()
If Form2.Button1_Click = True then '**This line is what I stucked**
Exit Sub
End if
End Sub
End Class
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
An example using DialogResult.
In Form1:
If Form2.ShowDialog = DialogResult.OK Then
' ... do something in here ...
End If
In Form2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.DialogResult = DialogResult.OK
End Sub
Note that if the user cancels the Form2 dialog without hitting the button then you'll get a result of Cancel back instead.
I would like to share an event to another form via a common class with a public shared event.
My code is setup like this:
Form1 is the main form where the button.click event is generated
Form2 is a subform displayed in Form1 in a panel control, where if the event is generated executes a sub.
To share data I use a commonData class, to share the only data i need, because I want to keep private function and variables in the single forms.
Can someone help me figure out what I'd like to do?
something like this, but working
Public Class commonData
Public Shared Event event1()
End class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.Click
RaiseEvent commonData.event1()
End Sub
End class
Public Class Form2
Private Sub eventFired(sender As Object, e As EventArgs) Handles commonData.event1
MsgBox("event")
End Sub
End class
There is many techniques to do that but this one (which is one of those) I hope might help you to have an idea how can have a shared Event between Forms or other classes.
First as you want here is the common module (you can use a class instead, is your choice; but <Extension()> go only in modules).
CommonData as a Class and ExtensionUtils as a Module for extensions:
Imports System.Runtime.CompilerServices
Public Class CommonData
Public Shared Event MyGlobalEvent(eventSender As Object, otherParams As String)
Shared Sub RaiseMyGlobalEvent(eventSender As Object, otherParams As String)
RaiseEvent MyGlobalEvent(eventSender, otherParams)
End Sub
End Class
Module ExtensionUtils
<Extension()>
Public Sub ButtonClick(ByVal buttonCaller As Button, eventArgs As String)
CommonData.RaiseMyGlobalEvent(buttonCaller, eventArgs)
End Sub
End Module
And here the implementation:
In this example I use a button named “Button1” in Form2 and when I click on it show a msgbox in Form1.
Form1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
AddHandler CommonData.MyGlobalEvent, Sub(objSender As Object, message As String)
MsgBox(message & vbCrLf & " But I'm telling you that from form " & Me.Name)
End Sub
End Sub
End Class
Form2:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, Sub()
Button1.ButtonClick(("I'm clicked from form " & Me.Name))
End Sub
End Sub
End Class
This question already has answers here:
VB.Net Passing values to another form
(2 answers)
Closed 5 years ago.
I have 2 forms with text boxes and buttons and I want the first form to open the second form. I then proceed to type a value in the text box in the second form so that when I click the button on the second form it must close and pass the value of the text box to the first form text box.
I have been trying to do this with no luck so far...
I am currently using vb.net in visual studio 2015
Here is my code:
Public Class Form1
WithEvents fr2 As New Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2.Show()
End Sub
Private Sub fr2_passvalue() Handles fr2.passvalue
Me.txtform1.Text = fr2.txtform2.Text
End Sub
End Class
Public Class Form2
Event passvalue()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue()
Me.Close()
End Sub
End Class
I have tried accessing the controls directly but it does not work as vb.net now uses classes for everything. I cannot seem to figure out how to get around this.
Redefine your event to pass a string. Then use the Form2 event to pass the value to the handler.
Public Class Form2
Public Event passvalue(text As String)
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue(txtform2.Text)
Me.Close()
End Sub
End Class
Handle it in Form1, updating Form1's TextBox.
Public Class Form1
WithEvents fr2 As Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2 = New Form2()
fr2.Show()
End Sub
Private Sub fr2_passvalue(text As String) Handles fr2.passvalue
Me.txtform1.Text = text
End Sub
End Class
As you had it before, Form1 was accessing controls on Form2 directly, which defeats the purpose of the event.
You could simply define a global variable (Public), which would be accessible from both forms.
I'm wanting to use visual studio to create a form with buttons, but then have it coded elsewhere, so that I can use the events (ie; button clicks) to perform specific actions in this application.
Is it possible/practical to do this? If not/if so, what do I need to look up and learn about from here to be able to implement it? My knowledge base in programming is limitted; I've just been starting to get familiar with Classes.
(I'm working in Autodesk Inventor, and am trying to create a prompt window with buttons to control the output of another program I have. In order to save on the amount of calls/interfacing, I was hoping to just create an uncoded button form, but code it within my program/macro I have within Inventor - its to be a form with six buttons that end up rotating some graphics within the program at a certain stop point, and then the program resumes when the form is closed via the "x")
I have seen posts such as the one below, but that doesn't seem to have the capability to receive user input: How to create a custom MessageBox?
Currently I am here, which works for showing the toolbox. Can someone show me how I would handle the events please?
AddReference "C:\Users\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\bin\Release\SectionSymToolBox.dll"
Imports System.Windows.Forms
Public Class SectionSymRule
'Public dlg As New System.Windows.Forms.For
Public Shared ToolBox As New SectionSymToolBox.SectionSymToolBox
Dim WithEvents EClass As New EventClass
Sub Main()
ToolBox.Show()
End Sub
End Class
Public Class SectionSymToolBox
Private Sub Main()
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Swap Symbols
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Flip Symbol
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Flip Text
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'<
End Sub
Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'>
End Sub
End Class
You can always use Partial Class to split your code into multiple files.
So you create your Form (say Form1) the normal way. Then put the code in another class file with class declared as Partial
For example,
Partial Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(TextBox1.Text)
End Sub
End Class
The other way is to inherit your Form. The inherited form will then have everything of whatever is on the form, with whatever you want to add.
Public Class Form1Code
Inherits Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(TextBox1.Text)
End Sub
End Class
I have some forms in my application and need to switch between them. I dont want to open another form over other form, in short I want the user to get a feel that he is working on one form only. Right now I am doing something like this but this is not what I want
Public Class Form1
Dim fo As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.TopLevel = False
Me.Panel1.Controls.Add(Form2)
Form2.Show()
End Sub
End Class
I also need to send data to other form like list of some class.
Look into UserControls(Composite Controls). They will allow you create a custom layout with events and properties and add it to your panel. I have used this to swap in/out edit pages for my applications.
Here is a very simplistic example:( 1 Form, 2 Panels, 2 UserControls)
Form1
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub UserControl11_SendText(value As String) Handles UserControl11.SendText
UserControl12.SetText(value)
End Sub
Private Sub UserControl12_SendText(value As String) Handles UserControl12.SendText
UserControl11.SetText(value)
End Sub
End Class
UserControl
Public Class UserControl1
Public Event SendText(ByVal Text As String)
Public Sub SetText(value As String)
Label1.Text = value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent SendText(TextBox1.Text)
End Sub
End Class
Have you considered using a TabControl? This will let you create different screens, and you (or the user) can switch between them easily.