Problem!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Problem!

  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Exclamation Problem!

    Please, I think it isn't a big deal, but still I am having problems because I am a beginner.
    Can someone write me a code that will do next: I want my text from textbox wich I enter in a dialogbox to enter itself into textbox in my form when I click OK. I hope my post isn't too much unclear!

  2. #2
    Join Date
    Aug 2009
    Posts
    100

    Re: Problem!

    Your post is perfectly clear: You want someone to do your work (or, more likely, your homework) for you.

    Have you tried doing this yourself yet? If so, can you share your code and what you think the problem is as to why it doesn't work? If you haven't tried to do this yourself yet, why not? How do you expect to learn if you don't at least try?

  3. #3
    Join Date
    Jun 2012
    Posts
    2

    Re: Problem!

    I've tried to do it. This is code I've enterd.

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Close()
    Form1.Enabled = True

    Form1.TextBox1.Text = TextBox1.Text


    Form1.TextBox2.Text = TextBox2.Text

    Form1.Focus()


    End Sub

    And still it doesn't wrok!

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,823

    Re: Problem!

    1: It is VB.Net code but you are posting under VB6
    2: You are closing the form on the second line before you get to most of your code
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Problem!

    And look:
    Form1.TextBox1.Text = TextBox1.Text
    You are assigning the text of the textbox to itself. This won't change anything.
    First you have to get the input from the dialog to a string variable like strText
    (Possibly that's your line Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Then you assign the string to the textbox like Form1.TextBox1.Text = Me.DialogResult
    Then you may do Me.Close. When you close the form, the contents of its variables get lost.

    However I'm not quite sure how the VB.NET Dialog works.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: Problem!

    This :
    Code:
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Close()
    Form1.Enabled = True
    
    Form1.TextBox1.Text = TextBox1.Text
    
    
    Form1.TextBox2.Text = TextBox2.Text
    
    Form1.Focus()
    will achieve absolutely nothing. zilch. nada.

    You say you want the text entered from a dialog box to be entered on a textbox.

    WHERE IS YOUR IF CONDITION????

    You can't say this :
    Code:
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Close()
    What SHOULD happen?
    You need to test IF OK was pressed, then commence with adding the text in the textboxes.

    ALso, you cannot Close the dialog box. By closing it, you dispose of it - that means you wipe the memory clear, so, in fact, there is NO text to bring over to the form.

    Form.Enabled - is very bad practise. You should show the form.

    This :

    Code:
    Form1.TextBox1.Text = TextBox1.Text
    
    
    Form1.TextBox2.Text = TextBox2.Text
    Is so CONFUSING. This is the problem when people do not learn how to properly name objects! I would suggest you give each form and each object on the form(s) proper names! It confuses everyone. I think that you have two improperly named forms, then on each form, you have textboxes named TextBox1 and TextBox2. Change that.

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,823

    Re: Problem!

    Quote Originally Posted by WoF View Post
    And look:
    Form1.TextBox1.Text = TextBox1.Text
    You are assigning the text of the textbox to itself. This won't change anything.
    I thought the same thing at first glance but after reading again I saw that these are textboxes on 2 different forms. Apparently the intention was that the code shown is intended to be the dialog form and is to return values to the calling form which of course should be handled much differently.

    Calling form
    Code:
            Dim frmTemp As New frmDialog
            frmTemp.ShowDialog()
    
            If frmTemp.DialogResult = Windows.Forms.DialogResult.OK Then
                textbox1.text = frmTemp.textbox1.text
                textbox2.text = frmTemp.textbox2.text
            End If
            frmTemp.Close()
    Code in ok button on frmdialog
    Code:
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Hide
    When the form is hidden control goes back to the calling form which will then grab the values from the dialog form and close it.

    And of course using this method there is no reference back to the callign form so it could be called from any form and work just fine.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Problem!

    Ah, yes. Ok. I didn't spot that.
    But then, the Me.Close, wouldn't it have destroyed the values anyway, right before they were assigned?

  9. #9
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,823

    Re: Problem!

    Quote Originally Posted by WoF View Post
    Ah, yes. Ok. I didn't spot that.
    But then, the Me.Close, wouldn't it have destroyed the values anyway, right before they were assigned?
    Yes, that would be my take on it though I can't say i have ever tried it
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Problem!

    Ok. I have not tried either because it is VB.Net. In VB6 we do not have Me.Close, but we have Unload Me and I'm sure that this will loose the contents of any variable local to that form.

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Talking Re: Problem!

    Quote Originally Posted by WoF View Post
    Ok. I have not tried either because it is VB.Net. In VB6 we do not have Me.Close, but we have Unload Me and I'm sure that this will loose the contents of any variable local to that form.
    I think this was missed somewhow

    Quote Originally Posted by HanneSThEGreaT View Post
    ALso, you cannot Close the dialog box. By closing it, you dispose of it - that means you wipe the memory clear, so, in fact, there is NO text to bring over to the form.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center