I need to create a form using code.

Is there a way to do this.

like in the code i need to create a form with a name, caption and text box in it.

Recommended Answers

All 21 Replies

u can get idea from the attach file.

ye thanks that will come in handy

but i need to make a form like form1 or form2

dim f1 as new form
dim f2 as new Form1  'u must have this form in project

f1.show
f2.show

try this

Dim f1 As New Form1

  '  Dim f2 As New Form1 'u must have this form in project
    

      
Private Sub Command1_Click()
f1.Show
    '  f2.Show
End Sub

this is great

But i need something like this:

Private Sub menuName_Click()
Dim f1 As String
f1 = Int(100000 * rnd() + 1)
Dim f1 As New frm_New
f1.Show
End Sub

sorry thats not helping ether

Try the following. Is this what you required help on?

Option Explicit

Private newCtrl As Control


Private Sub menuName_Click()

Dim frmNew As New Form1
With frmNew
    .Show
    .Caption = "My New Form"
    .menuName.Visible = False 'You only want one instance of a form
End With

Set newCtrl = Form1.Controls.Add("vb.textbox", "txtTextBox", Form1)

newCtrl.Visible = True
newCtrl.Move 2000, 500, 1500, 250 'Change placement to whatever you need

menuName.Visible = False 'You only want one instance of a form
Call txtBoxName(Form1) 'Add random number to textbox
End Sub

Private Sub txtBoxName(frm As Form)

Dim strCount As String
Dim Control As Control

strCount = Int(100000 * Rnd() + 1)

 For Each Control In frm.Controls
        If TypeOf Control Is TextBox Then
            Control.Text = strCount
        End If
       
    Next Control
End Sub

yes and no

yes i need to make a form like that(but i already have the form made).

no i want multiple of the forms open

ill post all my code and what im doing.

Ok, so you need more than one form with one textbox added to each form, but you get an error that the textbox already exists?

How many textboxes do you need on each form and how many forms do you need to create?

I'm making a text editor like notepad but better because i'm using an mdiform so you can have multiple files open at once.

This is the code i got.

MDIForm:

Private Sub menuAbout_Click()
frm_About.Show
End Sub

Private Sub menuName_Click()
frm_New.Show
End Sub

Private Sub menuOpen_Click()
frm_open.Show
End Sub

Private Sub menuSave_Click()

' loads save as box
    MDIForm1.CommonDialog1.ShowSave
    On Error GoTo error
    filelocation = MDIForm1.CommonDialog1.FileName
    Me.ActiveForm.Caption = MDIForm1.CommonDialog1.FileTitle
' append saves over file if it assists
    Open filelocation For Output As #1
    Close #1
    Open filelocation For Append As #1
        Print #1, Me.ActiveForm.Text1.Text
    Close #1
GoTo endload
    
error:
errorreport (0)

endload:
End Sub

Function errorreport(error)
Dim intResponse As Integer
intResponse = MsgBox("There was an Error wile trying to load", vbExclamation, "Quit")
End Function

frm_open:

Private Sub Form_Load()
MDIForm1.CommonDialog1.ShowOpen
Me.Caption = MDIForm1.CommonDialog1.FileTitle
FileName = MDIForm1.CommonDialog1.FileName
On Error GoTo error

Open FileName For Input As #1
    
Do Until EOF(1)

        Input #1, Data
        Me.Text1.Text = Me.Text1.Text + Data + vbCrLf
    EOF (1)
    Loop
    Close #1
    FileName = ""
GoTo endload
    
error:
errorreport (0)

endload:
End Sub
Function errorreport(error)
Dim intResponse As Integer
intResponse = MsgBox("There was an Error wile trying to load", vbExclamation, "Quit")
End Function

Private Sub Form_Resize()
Text1.Height = Me.ScaleHeight
Text1.Width = Me.ScaleWidth
End Sub

frm_new:

Private Sub Form_Load()
Me.Caption = "text.txt"
End Sub

Private Sub Form_Resize()
Text1.Height = Me.ScaleHeight
Text1.Width = Me.ScaleWidth
End Sub

I've also got an about form but i don't think you need that.

So i need one text box on each form and up to 100 forms or so open at once (ill make a new version if someone needs more than that open at once).

If you want some screen shots just ask.

say me what do u want to do for creating form by code.
maybe ur way is wrong!

it properly is i only just started teaching myself this year.
But how else am i going to do it

u must create form template and create new from from this
like form2
dim a as new form2
form2.show

i have a template

ok
so use this
dim f as new yourtemplateformname

then
f.show

or change any prop
like f.tag
f.picture
,....

thanks i was wrong about it not working

sorry for wasting your time if.

thanks everyone
thanks so so mutch

check my sample
if you want create window on the fly you must use window api like
createwindowex,....

thanks i was wrong about it not working

sorry for wasting your time if.

thanks everyone
thanks so so mutch

thanks ill look at that

Sorry, went offline for a while. What exactly are you trying to create. If you tend on opening 100 pages or more, your system will be using enormous resources. Why slow the app down with unneccesary issues. Tell us what you plan on doing and we might be able to help here.

don't worry now i've got it to work thanks for your help tho

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.