Declaring string and condition in vb - vb.net

I have a String variable in this page named "a".
I wanted the scenario to be.
When the page is started "a" will be null.
But when the user selected an entry from the DetailView Control "a" will become "have".
The following is my code. But i keep getting "a" = null even though i have selected an entry from the detailView control.
Dim a As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
a = "a"
End Sub
Why is this so? How should i go about doing it?

When ever you do any operation on you page with server side controls and your page postback, all the variables which are declared globally are again reset and go at their initial stage, so that's why you are getting a = null every time.

The code is ok, but you are changing the value when the selected index changes on a gridview, not a detailsview, also values are not stored through postbacks. If you assign the gridview selected value to a label for example, and viewstate is active, then it will be maintained on that control. But variables on the VB are reset on each postback.
Anyway for doing that you can update the string on the Page_Load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
a = "a" 'or whatever value you need. i.e. the gridview selected data key, etc...
End Sub
Or if its based on what the user does, add the page.ispostback
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
a = "a" 'or whatever value you need. i.e. the gridview selected data key, etc...
'or for example...
a = Me.aDropDown.SelectedItem.Text
Else
a = String.Empty
End If
End Sub
If not, "a" will allways be equal to string.empty each time the page loads.

Related

variable object name error

I have the following:
a listbox called ListBox1 with a couple of random values in it
a couple of rich textboxs called Rit11 up to Rit 154
a button
When I click a rich textbox, I want to set the a variable to the number that the box is called. After this I select a value from the listbox, and when I press the button, I want this value to move to the selected textbox, and be removed from the listview. I made the following code, but it gives an error.
Private Sub Rit12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rit12.Click
SelBox = "12"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim BoxName
BoxName = ("Rit" & SelBox)
Me.Controls(BoxName).Text = ListBox1.SelectedItem
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
It worked fine, till I tried using the variable name. This rule seems to give the problem:
Me.Controls(BoxName).Text = ListBox1.SelectedItem

How to Transfer Datagrid Value of one form to TextBoxes of another form?

I cant somehow figure why it can't transfer the data with no errors
Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim pinfo As New Employee_Personal_Info()
pinfo.ShowDialog()
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
Employee_Personal_Info.TextBox1.Text = row.Cells("emp_firstname").Value.ToString
End If
End Sub
Apart from the fact that you're displaying the form before passing it the data, the issue is that you are setting the Text of a TextBox on the default instance of that form, not the instance you created. This:
Employee_Personal_Info.TextBox1.Text = row.Cells("emp_firstname").Value.ToString
should be this:
pinfo.TextBox1.Text = row.Cells("emp_firstname").Value.ToString
you can pass via globle variable like
public text as string
Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
text=row.Cells("emp_firstname").Value.ToString
End Sub

How to pass value of a textbox from one form to another form

If I have a value stored into a textbox of form1 and I have to pass that value into an another textbox of another form2. What is the method to do this passing values from one form to another?
There are a no. of ways.
1.Using TextChanged event.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Using Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TextBox1.Text = TextBox1.Text
End Sub
Using LostFocus Event:
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
Form2.TextBox1.Text = TextBox1.Text
End Sub
Similarly you can work with every events.
In order to retrieve a control's value (e.g. TextBox.Text) from another form, the best way is to create a module and create a property for the private variable.
An example of a property to hold a customer's first name:
Module modPrivateVariables
Private strCustomerFirstNameSTR As String
Public Property getCustomerFirstNameSTR() As String
Get
Return strCustomerFirstNameSTR
End Get
Set(ByVal strCustomerFirstName As String)
strCustomerFirstNameSTR = strCustomerFirstName
End Set
End Property
End Module
Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text. For example, if you had a textbox named (txtCustomerFirstName), under its TextChanged event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text.
The textbox's text will now be assigned to getCustomerFirstNameSTR property. Now you'll be able to access this property's value from anywhere and from any form in your application. For example if you had a textbox in another form say Form2 called "txtBoxInForm2" you can call txtBoxInForm2.Text = getCustomerFirstNameSTR.
If you wanted to clear the value of the property then just type getCustomerFirstNameSTR = String.Empty. The main thing to understand is that when you create a variable in one form (class) and try to access its value from another form (another class) then the variable has to be re-instantiated once.
This happens then the variable is reset to its default value which is an empty string. This will cause you to keep getting nothing (an empty textbox) every time you call it from another form. Properties don't need to be re-instantiated because they are accessed through public methods within the property itself (the get and set methods).
if both the forms are running, then you can use
form2.TextBox1.Text=form1.TextBox1.Text
Else you can declare a Public String variable in Form2, on any event,
dim Obj as new Form2
Obj.StrVariable=Me.TextBox1.Text
Obj.Show
and on Form2 Load,
Me.TextBox1.Text=StrVariable
In Form1.vb make sure you use an event such as Button.Click and inside that
Dim obb As New Form2
obb.val = Me.TextBox1.Text()
obb.Show()
Me.Hide()
In Form2.vb use a property called "val"
Public Property val As String
And on an event like MyBase.Load
TextBox1.Text = val
You could use the button1_click and state there:
Dim obj as new form2
Obj.pass=me.textbox1.text
Obj.show()
Then in your form2 before your form2 main class you state:
Public property pass as string
On the load state
Textbox1.text=pass
Now, when you click on the button on form1, form2 will show and the textbox1 on form2 will have the same text as the one in form1. Provided you use this only with text box, labels or other kind of STRING or will work.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' timer1 must be set to true
Timer1.Start() Form1.Show() Me.Hide()
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Form1.TextBox13.Text = TextBox1.Text

Making a link from GridView in VB.NET 2012 (Windows Form)

I have project to develop an application for employees and schedule management on my campus. My plan is to develop with VB.NET and use SQL Server for the database.
How can I use a grid view to make a link going to another window? The window will show all information by schedule.
You are looking for DataGridViewLinkColumn. Also check this link.
Once CellContentClick event fires, you can create another form and pass desired values to that form to display details and show them up.
You can also use DataGridViewButtonColumn too.
EDIT:
I assume that I have two forms frmMaster and frmDetails. DataGridView lies on frmMaster and user keeps record ID in TAG property of first cell.
then, FrmMaster code would be:
Private Sub frmMaster_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Added a LinkColumn from code (you can do it at design time too)
DataGridView1.Columns.Add(New DataGridViewLinkColumn() With {.Text = "Show details"})
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim detail As New frmDetail() ' create an instance of detail form
detail.ID = DataGridView1.Rows(e.RowIndex).Cells(0).Tag'set property of detail form
detail.ShowDialog() ' Show form
End Sub
and frmdetail code would be:
Private _Id As String
Public Property ID() As String
Get
Return _Id
End Get
Set(ByVal value As String)
_Id = value
End Set
End Property
Private Sub frmDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData(_Id)
End Sub
Sub LoadData(ByVal _id As Integer)
'' use '_id' variable to extract data from dtaabse via query
End Sub

Datagridview combobox column's value

I have a datagridview combo box with a few values in there. I am trying to capture what user clicked. I tried CellValueChanged, CellContentClicked etc. but nothing works.
I want to store this value to a variable (important) and then shift the cursor to column after user has selected value.
Please help. Please also advise what event to fire.
Thanks
Private Sub dg_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellValueChanged
If flgLD = 1 Then
Dim retreivedValue As Object = dg.Rows(dg.CurrentRow.Index).Cells(0).Value
dg.Rows(dg.CurrentRow.Index).Cells(1).Value = retreivedValue
dg.CurrentCell = dg.Rows(dg.CurrentRow.Index).Cells(1)
dg.BeginEdit(True)
End If
End Sub
Use CellValueChanged Event, then check what Column is chosen by the user, then you can retreive the value. Here an example:
Private Sub yourGrid_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles yourGrid.CellValueChanged
If e.ColumnIndex = yourComboColumn.Index Then
Dim retreivedValue As Object = yourGrid(e.ColumnIndex, e.RowIndex).Value
End If
End Sub