Custom dialogue box in vb 2008

Steve36445

Well-known member
Joined
Dec 23, 2007
Messages
58
Programming Experience
10+
Can you help me please?

I would like to generate some custom dialog boxes that I can call from anywhere in my project.

The easiest example I can give is of a form that waits for two numbers to be entered then returns their sum. I want to call it like a function ie.


sum = add_two_numbers( " Enter the numbers you want to add then click ‘add’”)

This is not what I actually want to do, that is far more complex, this just a simple way to explain what I want to do. If I can do this I can do what I really want.

Essentially I want forms that will accept some input, process it and return a string. A bit like an input inputbox, an openfiledialog box or a fontdialog box. I also need to pass strings to it.

I can do it if I use a function declared in a module which calls the form and then returns the value once it is calculated but isn't there a simpler more elegant method?



Thanks,

Steve

PS
 
form.ShowDialog() method is what you use to show a form as a dialog. A default dialog form template can be added with "Add new" and select the "Dialog", it is a standard form for some settings that is common for dialog forms.
 
Thanks John,

I,ve found that but it returns an integer & not a string. That would be OK for some purposes if I could find out how to pass strings to the form. e.g. for sending prompts.


Regards,

Steve
 
VB.NET:
but it returns an integer & not a string.
ShowDialog function? No, it returns a DialogResult value.

You can write any methods and properties you want to add to your form class, and interact with these.
 
Yes John, I'm sure some people can.

The DialogResult appears to be an interger not a string.

Can you give me any specific help on how to pass strings to a form and if possible return strings from a form as outlined in my original post?

The easiest example I can give is of a form that waits for two numbers to be entered then returns their sum. I want to call it like a function ie.


sum = add_two_numbers("Enter the numbers you want to add then click ‘add’”)

Regards,

Steve
 
I've tried it again,

If I try to execute the following code-

VB.NET:
Me.DialogResult = "How about that then"

It gives an error

Conversion from string "How about that then" to type 'Integer' is not valid


Regards,

Steve
 
How about you go for a dirtier approach, and make a new Window Form, and just use labels, buttons and Text Boxes to get what you want?

That should get rid of the integer/string problem...
 
Thaks Hexaro,

Yes, you're right, there are several ways around it and I already have the code working, but I want to learn the 'proper' way. It can be done with the 'inputbox' and so I would think there must be a way of doing it.

Regards,

Steve.
 
Here's the code sample in help for ShowDialog method:
VB.NET:
Public Sub ShowMyDialogBox()
    Dim testDialog As New Form2()

    ' Show testDialog as a modal dialog and determine if DialogResult = OK.
    If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
        ' Read the contents of testDialog's TextBox.
        txtResult.Text = testDialog.TextBox1.Text
    Else
        txtResult.Text = "Cancelled"
    End If
    testDialog.Dispose()
End Sub
Does it help you?

General advice, if you still haven't read a beginner book in VB.Net programming do that first, it should teach you the basics of classes, data types and methods. If you still don't know how to use help invest the time you need to learn this as it's the best help for helping yourself.
 
Thanks John,

In case it helps anyone else.


This what I found, How does it seem to you?

My interpretation ( please correct me if I'm wrong) is-

You can't pass variables to or return them from a form , it’s just a container for the methods. You can, however, pass variables to and return a variable from a function. So declare a function on a form that makes it visible, waits for it to do what it must, then returns a value.

Example

Requires two labels, a button and a textbox. Names as default.


VB.NET:
Public Class input_box
    Dim input_complete As Boolean

    Function show_input_box(ByVal Prompt1, ByVal Prompt2)
        Label1.Text = Prompt1
        Label2.Text = Prompt2
        Me.Visible = True

        Do While input_complete = False
            Application.DoEvents()
        Loop
        Return TextBox1.Text

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.input_complete = True
    End Sub
End Class
This can be called with-
VB.NET:
Dim IB As New input_box
        Label1.Text = IB.show_input_box(Promp1, Promp2)
IB.Close()

Thanks,


Steve
 
You can't pass variables to or return them from a form , it’s just a container for the methods. You can, however, pass variables to and return a variable from a function. So declare a function on a form that makes it visible, waits for it to do what it must, then returns a value.
No, a form is just a class like any other class. No special rules apply.
Do While input_complete = False
Application.DoEvents()
Loop
Don't do this, use ShowDialog.
 
Back
Top