Solved

Form2-Textbox, to, Form1 -Listbox (vb.net 2003)

Posted on 2006-11-14
16
2,543 Views
Last Modified: 2012-08-14
Hi Experts

I have a Two Forms. Form1 has a Listbox & a 'users' Button. When button clicked, it opens Form2.

Form2 has a Listview at the top of the page followed by button1... when I select the row in the list view, I click button1 and it imports the selected item into Textbox1 & 2 below.

*Form2 diagram*
-------------------------------------------
|                     column A  |  column B                      |
|------------------------------------------|
|                          data A    |  data B                        |
|                                                                            |
--------------------------------------------
           ---------
           |Button1|
           ---------
--------------------------------------------

 ------------    -------------
 |  Textbox1  |    |   Textbox2    |
 ------------    -------------
--------------------------------------------
           ---------
           |Button2|
           ---------

So Far so good. (I've got this far)

What I want to do is click Button2 to transfer data from Textbox 1 & 2 and populate the Listbox in Form1 whilst closing form 2. I'be tried a few things, but get errors.

Any ideas?

Cheers,
Roberto
0
Comment
Question by:RobertoFreemano
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 1

Expert Comment

by:allos
ID: 17945555
Hi!

It is possible to solve this in many ways, but I suggest that this one as the simpler:

Step 1 : Go to the constructor of Form2 and declare a parameter of type Form1 e.g.
            You should also declare a private Form1 field in your Form2
             Private myParent as Form1
             Public Sub New(ByVal parentForm As Form1)

Step 2 : Declare a public Sub in Form1 with two arguments. This sub should add to the
            listbox the two arguments as an item. e.g.
            Public Sub AddAnItem(first as String, second as String)
                 ... add your code here to add the new item ...
            EndSub

Step 3 : In the Button2 click event write:
            myParent.AddAnItem(TextBox1.Text, TextBox2.Text)

Step 4 : Finally change the code in Form1 that opens the Form2 from something like new Form2() that you have now
            to new Form2(Me)

This should do the job.

Alternatively you can go to the graphical editor of Form1 and change the Modifiers property from Private to Public for the
listbox in Form1, so you can access it from within Form2, BUT that would only merge steps 2 & 3 from the above procedure
which is not much, and is a bad programming practice.

If you have any question or need more (specific) help, please do not hesitate to come back with more...

Sincerely,

Pan
___
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 17945841
A simple way would be, for example if you were using C# instead, is to save the contents from Form2 into a text file.  And then have code in Form1 to read that text file and put the contents into the listbox.  

Not the most pretty way of doing it, but it's a solution if stuck.
0
 
LVL 1

Expert Comment

by:allos
ID: 17945915
Regarding angus_young_acdc solution my objection is
that in this case you would have to terminate the
Form2 in order to execute the code in Form1. In addition
this method has many problems regarding the syncronization
of the access to the file in case a problem with the program
comes up (e.g. a program crash).

Pan
___
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 17945975
I did say it wasn't a pretty way of doing it and that I was merely offering a solution if Roberto was stuck.   It does however offer a little backup should the program crash for another reason.  The data would be saved, could be opened again whereas if it was just stored in variables etc then it would be gone.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 17947909
In my opinion, the simplest way would be to open Form2 via ShowDialog()...

In Form1:

    Dim f2 As New Form2
    ' setup "f2" as needed before display...
    f2.ShowDialog() ' code STOPS here until "f2" is hidden/closed
    ' now get some info DIRECTLY from "f2"...
    Me.ListBox1.Items.Add(f2.TextBox1.Text)

In Button2 on Form2:

    Me.DialogResult = DialogResult.Ok ' this causes the form to HIDE (not Dispose) and code in Form1 continues
0
 
LVL 1

Expert Comment

by:allos
ID: 17948283
I don't mean to be a pain but..

Again the same problem arises as in case that angus_young_acdc proposes.
Each time you open Form2 you can add only one entry in the listview of Form1.
However using the Button2 you can add many entries with one "session" of
Form2.

This is the only difference. If this is not a requirement, then Idle_Mind's method
is equivalent (with less code) to the one I suggested. However I think that Roberto
had such a requirement in mind when he added the Button2 in Form2.

Pan
___
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17948323
The author said:

    "What I want to do is click Button2 to transfer data from Textbox 1 & 2 and populate the Listbox in Form1 whilst closing form 2."

He says "whilst closing form 2"...thus I interpreted this as Form2 being used only ONCE per "session".

But you never know if what was said is actually what was intended.  =)

0
 

Author Comment

by:RobertoFreemano
ID: 17948971
Thanks All,

Cheers Idle Mind.... 1 more question that'll complete my project.

Q) if I have another button on Form 1, if clicked opens form 5 and displays the contents of Listbox1 on Form 1 with inserted text... is that possible?

e.g.

Form 5
Textbox1.text = "The current user is? " & Form1(Listbox1.selecteditem(1) & "text continued..." & VbCtrl & "Location? " &  Form1(Listbox1.selecteditem(2) & "text continued..."...

That's the idea?

Cheers
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17950741
With regard to your last question, you don't need any special coding to do that as this can all be accomplished from within Form1 when you create your instance of Form5:

    Dim f5 As New Form5

    ' setup "f5" as necessary using info from THIS form...
    f5.TextBox1.Text = "The current user is? " & Me.ListBox1.SelectedItem(1) & "text continued..." & VbCrLf & ...

    f5.Show() ' or f5.ShowDialog()...
0
 

Author Comment

by:RobertoFreemano
ID: 17951437
Hi Idle Mind

I get an error when I run the above script...

Error Message: 'An unhandled exception of type 'System.IndexOutofRangeException' occurred in system.windows.forms.dll' Addition information: Index was outside the bounds of the array.

Any ideas?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17951801
That was not meant to be exact code silly goose...

I thought you were asking how to pass data from Form1 to Form5 for display.  That is the intent of the code.

The gist of it is:

    Dim f5 As New Form5
    f5.TextBox1.Text = Me.something.value
    f5.Show()

Do you need specific help getting something in particular off of Form1?  If so, then what?
0
 

Author Comment

by:RobertoFreemano
ID: 17951900
Hi Idle_Mind,

Silly me...

In a nutshell... I have a Listbox on form1. If I click Button 1=Form2 & I select options that add items back to Listbox=Form1 and continuous with other Buttons & Forms.

I want to output the final collection of items from Listbox1 on Form1 to... Final Form with a Textbox on it so that I can Copy all the data and Paste it somewhere else.

:)
0
 

Author Comment

by:RobertoFreemano
ID: 17951907
I tried adding -
'textBox1.Text = ListBox1.GetItemText(ListBox1.SelectedItem' & it does display text in the final textbox on the Final Form... but it looks like code rather than the contents of Listbox1 from Form1.

:(
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17951911
So basically you need ALL the items from your ListBox in a String...with each item seperated by vbCrLf?
0
 

Author Comment

by:RobertoFreemano
ID: 17954034
Yep
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17956914
You can do something like...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As String = GetListBoxItems(ListBox1)
        MsgBox(msg)
    End Sub

    Private Function GetListBoxItems(ByVal lb As ListBox) As String
        Dim sb As New System.Text.StringBuilder
        For Each item As String In lb.Items
            sb.Append(item & vbCrLf)
        Next
        Return sb.ToString
    End Function
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Batch file output 20 77
Delphi Mdi application Child forms get behind control 7 111
count8 challlenge 13 85
mapAB Challlenge 35 85
A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now