How to save the last Position/Value of a trackbar when closing a form? - vb.net

I have a Form1 that sends a line of text to another open application using AppActivate.
This line of text is created from dropdowns and textboxes, and is sent to the other app using sendkeys and System.Threading.Thread.Sleep(label1.text).
I have a trackbar on a second form hooked to label1 to control the time amount for sleep value. I have the trackbar value set at 100 so that the user has a default value. When the timer is not long enough, the user can go to the second form and increase the slider value.
My problem is that because I have the trackbar value set at 100 everytime the second form opens it resets to the default 100 setting. How can I have a default 100 setting on the trackbar, but once the value is changed it will be there next time the second form is opened?
Also I want it to go back to 100 when the application is close completely (form 1 closed).

I don't understand exactly what controls each form has or what it's job is, but it sounds like you can just pass the current trackbar value from one form to another via the constructor:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set the default value
TrackBar.Value = 100
End Sub
Private Sub Button1_Clicked(sender As Object, e As EventArgs) Handles Button1.Click
'Pass the current value of trackbar into Form2 constructor
Dim form2 As New Form2(TrackBar.Value)
form2.Show()
End Sub
End Class
Public Class Form2
'You can this wherever you need it now
Dim _TrackBarValue As Integer
Public Sub New(ByVal TrackBarValue As Integer)
_TrackBarValue = TrackBarValue
End Sub
End Class
Obviously, you need to use the right forms for how your controls are layed out.

Related

parse value from datagrid to button name

I'm building a form that has many buttons, all buttons do the same thing: add 1 every time they are clicked. Every pressed button is sent to a datagridview along with the time they are pressed. Datagrid values look like this:
a_1_serv (button name), 18:05:00(time).
Sometimes I want to delete the last row. Everything works fine so far.
When I delete the last row, I want to change the text of the button (a_1_serv).
I can parse the dgv value (a_1_serv) to a variable but I can't bind it to the appropriate button name so I can control it.
Is there a way to do it?
Don't store your program state in your UI
Create a data structure to hold the information, and let the DataGridView be a "view", not treating it as a variable. You will save yourself headaches vs using the UI as a variable.
That said, create a class to represent your information
Public Class Data
Public Sub New(button As Button, time As DateTime)
Me.Button = button
Me.Time = time
End Sub
<System.ComponentModel.Browsable(False)>
Public Property Button As Button
Public ReadOnly Property Text As String
Get
Return Button.Name
End Get
End Property
Public Property Time As DateTime
End Class
And your code can manipulate the data in a variable off the UI. Bind the data to the DataGridView for display.
Private datas As New List(Of Data)()
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
addButton(DirectCast(sender, Button))
End Sub
Private Sub RemoveLastButton_Click(sender As Object, e As EventArgs) Handles RemoveLastButton.Click
removeLast()
End Sub
Private Sub addButton(b As Button)
datas.Add(New Data(b, DateTime.Now))
bindData()
End Sub
Private Sub removeLast()
Dim b = datas.Last.Button
b.Text = "new text" ' change to whatever
datas.RemoveAt(datas.Count - 1)
bindData()
End Sub
Private Sub bindData()
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = datas
End Sub
This does exactly what you stated but there may be inconsistency in these two bits of information you provided: a_1_serv (button name) and I want to change the text of the button .... This changes the button text but not the name. The name is displayed in the grid. You can change the data class to display the text or whatever. But the point is this approach will keep your data off the UI and you won't need to look up the control by name anymore.

How Show a Form on top of other?

I have a first Form that is always displayed of way:
Maximized and as content a printscreen of atual screen
Always on top
without borders
and now I want show a second Form on top this first Form, but I haven't success until this moment, in another words, this second Form don't is displayed on top. So how I can do it? All suggestions here are welcome.
Here is how I'm making for show the first Form:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each s As Screen In Screen.AllScreens
Dim Locker As New Form2(s, 0.3)
Locker.Show()
Next
End Sub
End Class
=========================================================================
Public Class Form2
Public Sub New(ByVal scrn As Screen, ByVal FrmOpacity As Double)
InitializeComponent()
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.StartPosition = FormStartPosition.Manual
Me.Bounds = scrn.Bounds
Me.TopMost = True
Me.Opacity = FrmOpacity
Me.ShowInTaskbar = False
Me.BackgroundImageLayout = ImageLayout.None
CaptureScreen(scrn)
End Sub
Private Sub CaptureScreen(ByVal s As Screen)
Using ScreenImg As New Bitmap(s.Bounds.Width, s.Bounds.Height)
Using g As Graphics = Graphics.FromImage(ScreenImg)
g.CopyFromScreen(s.Bounds.Location, Point.Empty, ScreenImg.Size, CopyPixelOperation.SourceCopy)
End Using
Me.BackgroundImage = New Bitmap(ScreenImg)
End Using
End Sub
End Class
You could simply set the Owner of the form2 to be form1
Public Class Form1
.....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each s As Screen In Screen.AllScreens
Dim Locker As New Form2(s, 0.3)
Locker.Show(Me)
Next
End Sub
.....
End Class
Passing the instance of Form1 to the Show method of Form2 will set the passed instance of the Form1 as the Owner of all the Locker forms that your create in your loop. In this way the Form2 instance will be always above the Form1 instance. Of course, you could remove the setting of the TopMost=True property that could be assigned only to one form at a time (Only one form could be the TopMost)
From MSDN on Owner form property
When a form is owned by another form, it is closed or hidden with the
owner form. For example, consider a form named Form2 that is owned by
a form named Form1. If Form1 is closed or minimized, Form2 is also
closed or hidden. Owned forms are also never displayed behind their
owner form. You can use owned forms for windows such as find and
replace windows, which should not disappear when the owner form is
selected. To determine the forms that are owned by a parent form, use
the OwnedForms property.
What you want is a Modal Dialog. I don't know vb.Net, but in C# .Net if you say
NameOfFormThatShouldBeOnTop.Show();
The form will just show but not necessarily be on top. However, if you go
NameOfFormThatShouldBeOnTop.ShowDialog();
It will be forced on top. From the documentation, it looks like doing this in vb is pretty similar. I would guess it would be something like
NameOfFormThatShouldBeOnTop.ShowDialog()
If I got that syntax wrong, feel free to edit :)
You may try to load a Sub after the LOAD or SHOWN events, that will contain the below code:
private sub LeaveMeAtTop()
Me.Topmost = True
Me.TopLevel = true
Me.Activate()
Me.ResizeRedraw() = true
Me.ResumeLayout()
Me.Focus()
end sub
It will force the form to be displayed at top of all

passing data from textbox in form1 to textbox in opened form2 in visual basic

i have form1 to enter movies details
in this form1 i have a textbox called NVideosGenres
through this textbox i can open the form2 with a space
the form2 contains 5 combobox to let the users choose the genres if there is more then one
when user choose the genres they will be applied to a textbox in the same form like this way
for example i choose from three combobox
action - war - western
so now i have a problem because i know that to pass value i should do this
Videos.NVideosGenres.text = me.FinalGenres.text
me.close()
when i click on the button the form2 will close but the data don't pass to NVideosGenres in the form1
any help??
There are many, many ways to do this. One of the easiest ways would be to create a property on Form2 that holds a reference to your main calling form. Then you can set the values of the textboxes (or whatever) directly from Form2. This is definitely not the best way to do it, but it certainly is quick and easy.
Private Sub btnShowForm2_Click(sender As System.Object, e As System.EventArgs) Handles btnShowForm2.Click
'Create a new instance of Form2 (called frmDetail in this example).
Dim frm2 As New frmDetail()
'Set the MyParentForm property of Form2 to this form.
frm2.MyParentForm = Me
'Show the form.
frm2.ShowDialog()
End Sub
Now in Form2, when you click the "OK" button to close the form, you can use the property that references your parent form to set the values directly.
' Property to hold a reference to the calling form.
Private mParentForm As frmMain
Public Property MyParentForm() As frmMain
Get
Return mParentForm
End Get
Set(ByVal value As frmMain)
mParentForm = value
End Set
End Property
' When I click the OK button, I will store the value of my various textboxes to properties.
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
'Set the value of the Genre textbox on the parent form to the value
'of the textbox on my current form.
MyParentForm.txtGenre.Text = txtGenre.Text
Me.Hide()
End Sub

Label won't update from separate form

I have 2 forms in my program. Form1 has a tab control on it, and on one of these tabs there are a load of labels. Form2 has a few textboxes and dropdown lists on it. Form1 has a button on it that opens Form2 on top of it as a normal form, not as a dialog.
There is code in Form1 that on loading populates the labels on it from a MySQL database. This code is in a separate public sub in the form that is called when the form loads.
What I am trying to do is fill in some of the boxes on Form2, when this Form closes it updates the database with these values (Works fine) and then those values are displayed on Form1. I can get Form2 to run the code in Form1 that populates the labels, but the problem is that the labels on Form1 never actually change. I have tried multiple things, including calling refresh on the labels, trying to change the labels from within Form2 instead of Form1, but they just never update. The .text value of the labels is being updated to the new value, I have checked this via debugging and stopping at the correct points to see what the value is.
Any ideas anyone?? I think it might be to do with the fact that it's Form2 calling the code and for some reason it doesn't have access to change the labels on Form1.
Code:
Form1 (AECSurveyForm)
Private Sub AECSurvey_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadMeterData()
End Sub
Public Sub LoadMeterData()
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
'utility
Meter1UtilityLabel.Text = AECMeteringData(0)("Utility1")
End Sub
Private Sub MeterButton1_Click(sender As Object, e As EventArgs) Handles Meter1Button.Click
If Not Application.OpenForms().OfType(Of AECMeteringDataForm).Any Then
AECMeteringDataForm.GetData(AECGlobalValues.CurrentSurveyUniqueIdentifier, 1)
AECMeteringDataForm.Show()
End If
End Sub
Form2 (AECMeteringDataForm)
Private Sub AECMeteringDataForm_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Closing
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
AECMeteringData(0)("Utility1") = UtilityComboBox.SelectedItem.ToString
AECMeteringDataAdapter.Update(AECMeteringData)
AECSurveyForm.LoadMeterData()
End Sub

Stop label from resetting when closing a form visual basic

In visual basic I have a label set to increment every time a button is clicked. However when I close and reopen the form the label goes back to default value. Is there a way to keep this label the same it was before I closed the form? Thanks
When you close the form instance every local object kept by the form is disposed (destroyed). When you show again the form a new instance of the form class is created but the objects are all initialized to their default values. So you have lost your current value.
A possible workaround is to have a Shared variable inside the form class that keeps the count.
Shared variables are not destroyed with the class instance but are available for every instance of the class with their current value
You use this value to initialize your label in the form constructor with the current value of your clicks
Public Class Form1
Private Shared clickCount As Integer
Public Sub New()
InitializeComponent()
myLabel.Text = Convert.ToString(clickCount)
End Sub
' you could also use Form1_Load event if you prefer
' Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Label1.Text = Convert.ToString(clicksCount)
' End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
clicksCount += 1
myLabel.Text = Convert.ToString(clicksCount)
End Sub
End Class
Keep in mind that this approach use the same variable (clickCount) for every instance of Form1 you create.