How do I make it so that when a variable is true in "Form1," it does something in "Form2?" - vb.net

So, I'm trying to make it so when the user clicks the login button on the login form, it sets a variable to true, then it makes something activate on the main form.
Some pseudo code that would be placed in Form 2:
If Form1.button = clicked Then
do something
end if
Thanks for any help!
By the way, I'm a complete noob with vb.net. Sorry.

On button click event write below
If Form1.button = clicked Then
Dim myForm As New Form2
if LoggedIn = True then
myForm.UserGroupBox.visible = true
me.close
myForm.Show()
end if
End if

Double click your Application name in the solution explorer then go to settings and create a new one with the name: clicked and the value on : 0 and write on the button1 in the form1
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
My.Settings.clicked = "1"
form1.show
me.hide
End Sub
and on the form were you want the something
if my.settings.clicked = "1" then
'Do Something what you want here
my.settings.clicked = "0"
end if
end sub
what this code does is:
when you click then button if gives the my.settings.clickde an value 1 = true
and when the other form loads its gonna check if the value = 1 if the value = 1 then it clicks the button and then changes the clicked value back to 0
so its not gonna repeat forever
hope this helps you and dont forget if it helps click button_up on my post

Related

Listview Checkbox - vb.net

Ok, i have a listview with checkboxes and a button, how it works is that i have to check the items i want to change the values, then press the button to change the value of those checked item, here's my code on the button.
Try
Dim I As Integer
If lv_id.CheckedItems.Count = 0 Then
For I = 0 To lv_id.Items.Count - 1
lv_id.Items(I).SubItems(1).Text = "Pending"
Next
Else
For I = 0 To lv_id.CheckedItems.Count - 1
lv_id.CheckedItems(I).SubItems(1).Text = "Submitted"
Next
End If
Proc_Items.BackColor = Color.Green
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Now, what i want to do is, to remove the button and then when i'll check the item i want to the code above will do the process without pressing the button, i tried "ItemCheck, ItemChecked" event, but with no luck.
You should be able to use the ItemChecked event for this. With the ItemCheckedEventArgs giving you everything you need.
This example would set up to toggle the 3rd column depending on the checkbox state
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
If e.Item.Checked Then
e.Item.SubItems(2).Text = "Submitted"
Else
e.Item.SubItems(2).Text = "Pending"
End If
End Sub
If I understood you right, you want to run your code whenever a ckecbox in your ListView gets checked or unchecked. This can be done using
Public Class Form1
'The ListView_SelectedIndexChanged event triggers when a checkbox of the listview gets checked or unchecked
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
'Put your code here, access all checked items with "ListBox1.CheckedItems"
End Sub
End Class
This worked for me, and I think it works for you as well. If not, please tell me in the comments what went wrong.

Labels & Text wont update accordingly

I developed an application which is like a digital comprehension sheet activity.
The order of which this works is:
A user double clicks an underscored text area on a text box
A form opens which asks them to place an answer
The answer is validated, if true it goes back to the original form
The issue here is that for some odd reason my labels don't update on step 3, as- well as the text. I have a label named lbAnswerStatus which notifies the user if they have a correct answer, the text does not update, and the 'SelectedText' on Form 1 should be replaced with the answer (if correct)
Here is my code:
Form 1 (when textbox is clicked):
Form2.Answer(answers(counter))
answers(counter) represents the correct answer being passed on, to compare with users answer in form 2
Form 2:
If tbAnswer.Text = theanswer Then
Form1.answerStatus(True, theanswer)
Form 1:
Public Sub answerStatus(status, answer)
If status = true Then
Form2.Close()
rtb.SelectedText = answer
lbAnswerStatus.ForeColor = Color.Green
End If
End Sub
My first assumption was that the Rich text box's SelectedText wasn't changing because it didn't have focus on it however, the lbAnswerStatus color didn't change either, so I figured that there was issues with making modifications to the form.
I used a message box to test whether lbAnswerStatus.Text would pop up and it did, so it can read but not write.
I also attempted to change the text of the label and selectedtext of the textbox in step 1 and it worked fine.
Any ideas what may be causing this issue?
Thanks.
I guess that you want your Form2 (AnswerForm) presented as a modal dialog. Doing that, you can return a result. You didn't say what you want to happen to the answer status label or the answer form if the supplied answer is incorrect.
Just as an example of how it can be done, create a new Windows Forms project. On Form1, place a button (Button1) and a label ("lblAnswerStatus"). Add a new form named "AnswerForm" to the project, and add a TextBox ("TextBox1") and a button ("bnDone").
As code for AnswerForm:
Public Class AnswerForm
Private statusLabel As Label
Private answerText As String
Private Sub bnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnDone.Click
If TextBox1.Text.Equals(answerText, StringComparison.CurrentCultureIgnoreCase) Then
' Alternative 1: set the color here if you want to
statusLabel.ForeColor = Color.Green
' return a value indicating success
Me.DialogResult = Windows.Forms.DialogResult.Yes
Me.Close()
Else
' indicate error
statusLabel.ForeColor = Color.Red
End If
End Sub
Public Sub New(ByVal statusLabel As Label, ByVal answerText As String)
InitializeComponent()
Me.statusLabel = statusLabel
Me.answerText = answerText
End Sub
End Class
and as code for Form1:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
lblAnswerStatus.ForeColor = Color.Blue
Using answerFrm As New AnswerForm(lblAnswerStatus, "X")
Dim dlgResult = answerFrm.ShowDialog()
' Alternative 2: use this if you want to change the color in this handler
If dlgResult = DialogResult.Yes Then
lblAnswerStatus.ForeColor = Color.Purple
End If
End Using
End Sub
End Class
If you click Button1 on Form1, a dialog box appears. Type into its TextBox1 and click bnDone. Because the instance of AnswerForm has a reference to lblAnswerStatus (supplied in its constructor), it is easy to update the latter, e.g. it turns red if you enter the wrong answer.

Changing a button's text after an event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Member = False Then
LoginForm1.Show()
Else <<<<(-after logged in successfully-)
Button1.Text = "Logged In" <<<(-I want to change this button text to Logged In-)
Button1.Enabled = False
End If
End Sub
So my problem is, the Button1.Text changes only works when I click on the button. If I did not click on the button, the text is still same as default "Login". I want to make the button text change instantly after the loginform1 closes. Help please and thank you
You can use LoginForm1.ShowDialog() instead. This shows the login as a modal form. This means you can not access the underlying form and the code execution in your sub stops until the modal form is closed.
So you could just use:
LoginForm1.ShowDialog()
If Member = True Then 'I guess this is how you check if the login was successful
Button1.Text = "Logged in"
Button1.Enabled = false
End if
instead of the whole If-Else-EndIf.
Somewhere in LoginForm1 when the Member has logged in you should include this code:
FORM1.Button1.Text = "Logged In"
FORM1.Button1.Enabled = False
Replace FORM1 with the name of your first form. (Form containing Button1_Click)

Edit Update DatagridView VB.Net (No Database)

Good day everyone.
I need your help in this project I am into (a Visual Basic program with no database.) It just contains a Datagridview, a Textbox, and three buttons (an "Add" Button, a "Edit" and an "Update" Button).
1 . Is there any way (like using "for loop") to automatically assign DataGridView1.Item("item location") to the one edited and be updated?
2 . Or is it possible to just click an item in the Datagridview then it will be edited at that without passing it to a Textbox, and to be updated at that.
The DataGridViewCellEventArgs variable (e in the method stub the designer will generate for you) of the double click event of the cell has RowIndex and ColumnIndex properties which refer to the position of the cell you clicked.
Save those (in a class variable possibly or a local one if that's all you need) and then refer to them when you update the cell in your DataGridView, possibly like this MyDataGridView.Item(e.ColumnIndex, e.RowIndex) or MyDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex) where e is the variable from the double click event handler.
For you cell double click event you could have something like this:
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Using myEditor As New frmCellEditor(Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value)
If myEditor.ShowDialog() = DialogResult.OK Then
Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value = myEditor.NewCellValue
End If
End Using
End Sub
This will call a new instance of your editor and get a value from you. For the purpose of this demo I have made a form like this:
Public Class frmCellEditor
Public NewCellValue As Integer
Public Sub New(ByVal CurrentCellValue As Object)
InitializeComponent()
Me.TextBox1.Text = CStr(CurrentCellValue)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.NewCellValue = CInt(Me.TextBox1.Text)
Me.DialogResult = DialogResult.OK
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Call Me.Close()
End Sub
End Class
Which just has two buttons (Button1 = OK, Button2 = Cancel). When you click OK, it just returns the value 1 which then gets set as the value of the cell.
This is a VERY simplistic example, but it should provide you the basics of what you are trying to do.
UPDATE:
I updated the code for the editor interface so it will include handling for passing the value back and forth from the form with your datagridview.
In your project, make a new form called frmCellEditor. This forms has to have two buttons and a textbox (Make sure that the programmatic names match!). Replace the code with the code listed above. You will have to add Imports System.Windows.Forms above the class as well.
Amend the event handler for the cell double click event of your datagrid to pass the cell value when frmCellEditor is constructed (the line going ... New frmCellEditor(...).
How many columns does your DataGridView has?
Based on how you populate your DataGridView, I'll assume only 1.
Declare this on top of your form
Dim i as Integer
On your btnUpdate_Click Event (Just combine your Edit and Update button into One)
SELECT CASE btnUpdate.Text
Case "Update"
With DataGridView1
'Check if there is a selected row
If .SelectedRows.Count = 0 Then
Msgbox "No Row Selected for Update"
Exit Sub
End If
i = .CurrentRow.Index 'Remember the Row Position
Textbox1.Text = .item(0 ,i).value 'Pass the Value to the textbox
.Enabled = False 'Disable DataGridView to prevent users from clicking other row while updating.
btnUpdate.Text = "Save"
End With
Case Else 'Save
DatagridView1.Item(0,i).Value = Textbox1.Text
btnUpdate.Text = "Update"
END SELECT
Thanks for those who contributed to finding answers for this thread. I have not used your solutions for now (maybe some other time). After some research, I've found an answer for problem 2 (more user friendly at that):
2 . Or is it possible to just click an item in the Datagridview then
it will be edited at that without passing it to a Textbox, and to be
updated at that.
Here's what i did:
in Private Sub Form1_Load, just add:
yourDataGridView.EditMode = DataGridViewEditMode.EditOnEnter
in Private Sub yourDataGridView_(whatever event here: DoubleCellClick, CellContentClick, etc.) add:
DataGridView1(e.ColumnIndex, e.RowIndex).[ReadOnly] = False
DataGridView1.BeginEdit(False)

VB.NET Form Hiding Issue

I have a custom form, B. B is created by A, which has a handle to B.VisibleChanged.
B only has an OK and Cancel button on it, and I want to do some logic when OK is hit.
B's OK button is dealt with like this:
Me.Result = Windows.Forms.DialogResult.OK
Me.Hide()
The code in A is properly hit and run, but it never hides B. When I check the values of the properties on B, it shows me Visible = False.
Does anyone have any suggestions as to the possible cause of this issue?
Edit
This form was shown using the Show() command, as I'm making a later call to have the form flash using FlashWindow().
Not exactly sure about your question.
why not use me.Close() instead of me.Hide?
Is it OK to have multiple instances of B at a time? If not, go for ShowDialog.
If you can rephrase the question, someone can probably resolve your problem.
I suppose you want to display a messagebox with an ok & cancel button. Instead of using a form use a mesagebox.
eg:
DialogResult dgResult = MessageBox.Show("Click Yes or No", "Test App", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (DialogResult.OK == dgResult)
{
//Do what you want.
}
else
{
//Do nothing.
}
If you are going to use a form, to do that & wanted to modify the parent's form, it would be advisable to use delegates to prevent form B from modifying form A's variables.
Else: (Not recommended)
Declare form B as a member variable of form A.
when required instantiate form B.
do B.ShowDialog();
internally in OK & cancel do this.dispose();
Again when you need form B just instantiate. re - instantiation will not be too much of an overhead if you dont call it very often.
But if you only need OK Cancel, use message box instead.
The show/hide approach works for me:
Public Class frmViewChild ' your form A
Private WithEvents _edit As frmEdit
'code
Private Sub editCell()
Dim PKVal As String
Dim PKVal2 As String
Dim fieldOrdinalPos As Integer
Dim isLastField As Boolean
If _edit Is Nothing Then
_edit = New frmEdit
_edit.MdiParent = Me.MdiParent
End If
'code
_edit.init(<params>)
If Not _edit.isNothing Then
_edit.Show()
_edit.WindowState = FormWindowState.Maximized
_edit.BringToFront()
End If
End Sub
'code
Private Sub _edit_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _edit.VisibleChanged
If Not _edit.Visible Then
WindowState = FormWindowState.Maximized ' revert after closing edit form
End If
End Sub
Public Class frmEdit ' your form B
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim ret As Integer
doOK(ret)
If ret > -1 Then ' no error
Me.Hide() ' close form, but didn't cancel
End If
End Sub
HTH