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

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

Related

Vb.NET Windows Forms Focus Event

I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class

data from datagridview not displayed into textbox (Visual Studio 2010)

I've been dealing with this for hours!!! I have 2 forms: in one form (form1) I have my layout with textboxes, etc... in the other (datagrid_form2) I have a datagridview where I choose the item, with a doubleclickcell event, to be loaded in a specific textbox of the first form (form1).
I have a button next to the textbox of the form1 that call the datagrid_form2, once the element in the datagrid_form2 is chosen the textbox of the form1 is loaded with that value.
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim form1panel As New form1
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Debug.WriteLine(form1panel.txtmybox.Text )
Me.Close()
End Sub
As you can see I have the cellDoubleclick event that should load the value of the selected cell into the textbox of my form1, but it doesn't display anything in the textbox(txtmybox). in the debug the value is chosen correctly, so is not a problem of code, simply the value is not being passed at the textbox.
Any ideas? hints?
thanks in advance
p.s. I'm working with visual studio 2010 .vb project!
It seems that you are messing with the forms.
You are creating a new instance of Form1 but you don't show it.
I suggest to read this.
Also your question is similar to this
Edit:
It is not clear from your questions how you want to achieve what you ask.
You have a form with a dataGridView (I name it frmDgv) and a second form (form1) that you want to show the cell content from yours datagrid.
Is this form (form1) already opened? or you want to open a new one each time that you double click? And if you want to open it each time you want multiple instances or one in modal ?
So I'll try to include everything:
->The form will open each time
frmDgv
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim f1 as new form1
f1=DirectCast(mLinkForm1,Form1)
f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
'If you want to open a Form1 each time you double click in an cell
f1.Show
'If you want a modal style info
'f1.ShowDialog
'f1.Dispose
End Sub
->The form is already open (I wouldn't follow this)
frmDgv
Private mLinkForm1 As Form1
Public Property LinkForm1
Get
Return mLinkForm1
End Get
Set(value)
mLinkForm1 = value
End Set
End Property
'When you open this form for first time you will set:
Dim f1 as new Form1
mLinkForm1=f1
f1.Show
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim f1 as new form1
f1=DirectCast(mLinkForm1,Form1)
f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
End Sub
Edit 3 (I don't have Visual Studio right now , so my code is not tested)
Form: datagridview
Private mLinkForm1 As Form1
Public Property LinkForm1
Get
Return mLinkForm1
End Get
Set(value)
mLinkForm1 = value
End Set
End Property
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
LinkForm1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Me.Close()
End Sub
Form: Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
datagridview.LinkForm1=Me
datagridview.Show
End Sub
Try this and inform me.
Change this part
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
to
form1panel.txtmybox.Text = data.CurrentCell.Value
This is for string value ...
You need to show your child form:
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim form1panel As New form1
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Debug.WriteLine(form1panel.txtmybox.Text)
form1panel.Show();
//Me.Close()
End Sub

VB.NET - Handle many textboxes with less code

I got many textboxes (about 10) in a form. I want the text in the textbox to be higlighted whenever it gets foucs. The code for that looks like:
Private Sub txtBillNo_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBillNo.GotFocus
HoverText(txtBillNo)
End Sub
Private Sub HoverText(ByRef ctrl as TextBox)
ctrl.SelectAll()
End Sub
It works perfectly, but I though I could do some code optimization here. Since, I have about 10 textboxes (and many other forms containing several textboxes), I have to HoverText(TextBox) in every Private Sub.. Handles TextBox.GotFocus for every textbox in each form.
I look for any form event (or any other way) that is trigerred when focus is given to another control (textbox) within the form, either by MouseClick or TAB so that HoverText(TextBox) is needed to be written only once for a form.
You can list all your textboxes in the Handles clause:
Private Sub atextbox_GotFocus(ByVal sender As System.Object, ByVal e As _
System.EventArgs) _
Handles txt1.GotFocus, _
txt2.GotFocus, _
txt3.GotFocus, _
(...remaining text boxes..)
txt9.GotFocus, _
txt10.GotFocus
Or you can add handlers to all textboxes when loading the form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each t In Me.Controls.OfType(Of TextBox)()
AddHandler t.GotFocus, AddressOf HoverText
Next
End Sub
Private Sub HoverText(ByVal sender As System.Object, ByVal e As System.EventArgs)
DirectCast(sender, TextBox).SelectAll()
End Sub
The .NET way to alter the behavior of a class is to derive a new one. That's well supported by VB.NET and Winforms as well. For any class derived from Control, you can intercept an event by overriding the protected OnXxx event where Xxx is the event name. You should be using the Enter event btw. Use Project + Add Class and make the code look like this:
Public Class MyTextBox
Inherits TextBox
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
Me.Select(0, Me.Text.Length)
MyBase.OnEnter(e)
End Sub
End Class
Compile. Go back to your form and note that you now have a new control on the top of the toolbox. Drag it on the form and note how it now behaves the way you want it, without writing any code or event handler in your form at all.

TextBox1 doesn't show the listbox1 value

I'm Visual Basic beginner, yesterday i wrote a dictionary that give you the opposite of the entered word, so i designed the form to look like this
[url]http://img651.imageshack.us/img651/6115/errorbp.jpg[url]
by the way i made a two list boxes as databases so the code will compare if the textbox1.text = listbox1.text then it will command textbox2 to append the value of the listbox : textbox2.appendtext(listbox2.text) but nothing happens
my code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TnsBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = TextBox3.ToString Then
TextBox2.AppendText(ListBox2.Text)
ElseIf TextBox1.Text = TextBox4.Text Then
TextBox2.AppendText(ListBox1.ToString)
End If
End Sub
Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
ListBox2.Items.Add(TextBox4.Text)
End Sub
End Class
the point of the code is ok cuz yesterday i finished the coding and the programs works fine but i forget to save it so i coded again and every thing above happens
this is the yesterday program
http://www.mediafire.com/?tavne7xjyth7y7v
virustotal link:
https://www.virustotal.com/file/1d39429ae1498a744e1556188b7e8914526b7e2fbb2d4904c2b4ea22fb278dc7/analysis/1346676641/
Initially you are setting the textbox text to "ListBox" without choosing anything specific so it is calling ToString() on the listbox which is why you get that.
I would change the method so that you have a Dictionary variable like so:
Public Sub Translate(input As String)
TextBox2.Text = OppositeDictionaires(input)
End Sub
Public OppositeDictionary As New Dictionary(Of String, String)
'Call as Add(TextBox3.Text, TextBox4.Text)
Public Sub Add(input As String, opposite As String)
OppositeDictionary.Add(input, opposite)
End Sub
Call add from your event and then Translate from your translate event. You should then get your output as intended, still add them to the listboxes if you want to display to the user but handle the translation in the code behind through a dictionairy object.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Declaring string and condition in vb

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.