I'm working on a project that will create new windows components. (If this is possible).
I have made a button that creates new forms when clicked in the MDI container.
Now I'm just wondering if someone can figure out a code to make it so when I click a button it creates a new button on the new created form.
Please help me, much appreciated.
If your wondering about my code here it is:
Code:
Dim count1 As Integer = 0
Private Sub NewButtonToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewButtonToolStripMenuItem.Click
Dim NewButton As New Button
NewButton.Text = ("Button ") & count1
NewButton.Show()
count += Val(1)
You need to add the button to the forms control array...Don't forget to set it's location and size also. And you need to add handlers as well....I am adding labels, but the code is the same for a button.
vb.net Code:
Dim lblbox As New Label
With lblbox
.Left = locx
.Top = locy
.Width = 30
.Height = 30
.BackColor = Color.White
.BorderStyle = BorderStyle.FixedSingle
.Name = "lblbox"
.Text = r.Next(0, 10)
.Tag = count
End With
Me.Controls.Add(lblbox)
AddHandler lblbox.Click, AddressOf lblbox_click
Private Sub lblbox_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Why will that not work? Were you getting a error message? That is the code I use to create labels on a form at runtime. You can change it a little to make any control you want.
locx and locy are varibles that I used to store the location of the control. You can replace them with the location that you want.
the .text = r.next(0,10) assigns a random value to the text property of the control. Since you are creating buttons, that would assign a random number between 1 and 10 and assign it to the caption of the button. You just replace the r.next(1,10) with the text you want to display on the button.
locx and locy are varibles that I used to store the location of the control. You can replace them with the location that you want.
the .text = r.next(0,10) assigns a random value to the text property of the control. Since you are creating buttons, that would assign a random number between 1 and 10 and assign it to the caption of the button. You just replace the r.next(1,10) with the text you want to display on the button.
looks like the button is on the new form, but the button_click eventhandler is in the mdi form?
hmmm maybe I'll have to check but in the meantime (sorry to through all these questions at you guys, but now i cant even debug...)
My error: title - (Invalid OperationException was unhandled)
System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: An error occurred creating the form. See Exception.InnerException for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'"
Source="ApriCoT - Krypton"
StackTrace:
at WindowsApplication1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at WindowsApplication1.My.MyProject.MyForms.get_Form1()
at WindowsApplication1.My.MyApplication.OnCreateMainForm() in C:\Users\Mark\Documents\Visual Studio 2008\Projects\ApriCoT - Krypton\ApriCoT - Krypton\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.InvalidOperationException
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'"
Source="ApriCoT - Krypton"
StackTrace:
at WindowsApplication1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at WindowsApplication1.My.MyProject.MyForms.get_newapp()
at WindowsApplication1.Form1..ctor() in C:\Users\Mark\Documents\Visual Studio 2008\Projects\ApriCoT - Krypton\ApriCoT - Krypton\Form1.vb:line 335
InnerException: System.InvalidOperationException
Message="The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'"
Source="ApriCoT - Krypton"
StackTrace:
at WindowsApplication1.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 180
at WindowsApplication1.My.MyProject.MyForms.get_Form1()
at WindowsApplication1.newapp.KryptonTextBox1_TextChanged(Object sender, EventArgs e) in C:\Users\Mark\Documents\Visual Studio 2008\Projects\ApriCoT - Krypton\ApriCoT - Krypton\newapp.vb:line 5
at System.Windows.Forms.Control.OnTextChanged(EventArgs e)
at ComponentFactory.Krypton.Toolkit.KryptonTextBox.k(Object A_0, EventArgs A_1)
at System.Windows.Forms.Control.OnTextChanged(EventArgs e)
at System.Windows.Forms.TextBoxBase.OnTextChanged(EventArgs e)
at System.Windows.Forms.Control.set_Text(String value)
at System.Windows.Forms.TextBoxBase.set_Text(String value)
at System.Windows.Forms.TextBox.set_Text(String value)
at ComponentFactory.Krypton.Toolkit.KryptonTextBox.set_Text(String value)
at WindowsApplication1.newapp.InitializeComponent() in C:\Users\Mark\Documents\Visual Studio 2008\Projects\ApriCoT - Krypton\ApriCoT - Krypton\newapp.Designer.vb:line 38
at WindowsApplication1.newapp..ctor()
InnerException:
does your error message look like this?
if you doubleclick on the error message it'll take you to the offending line
No, I have no actual errors in the application.
I click debug and it seems like it's about to load but then comes up with an exception box like I showed above
The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'"
If that is true, you should get a error in the IDE as paul posted. Can you post all the code that is creating the form and the buttons? We need to look at the code to find the problem.
If that is true, you should get a error in the IDE as paul posted. Can you post all the code that is creating the form and the buttons? We need to look at the code to find the problem.
My project has multiple forms but I'll just give your my form1 codes:
Public Class newapp
Private Sub KryptonTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonTextBox1.TextChanged
KryptonTextBox2.Text = (KryptonTextBox1.Text)
Form1.Text = (KryptonTextBox1.Text + (" - ApriCoT Krypton Application"))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
Form1.SaveAsToolStrip.Enabled = True
Form1.AddToolStrip.Enabled = True
Form1.EditTool.Enabled = True
Form1.ToolTitle.Enabled = True
Form1.AppTool.Enabled = True
If CheckBox1.Checked = True Then
MkDir("C:\" + KryptonTextBox2.Text)
Else
'Do nothing//////////////////////////
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub newapp_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Form1.Status.Text = ("Done.")
End Sub
Private Sub newapp_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End Sub
Private Sub newapp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub KryptonTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonTextBox2.TextChanged
Private Sub KryptonTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonTextBox1.TextChanged
KryptonTextBox2.Text = (KryptonTextBox1.Text)
Form1.Text = (KryptonTextBox1.Text + (" - ApriCoT Krypton Application"))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
Form1.SaveAsToolStrip.Enabled = True
Form1.AddToolStrip.Enabled = True
Form1.EditTool.Enabled = True
Form1.ToolTitle.Enabled = True
Form1.AppTool.Enabled = True
If CheckBox1.Checked = True Then
MkDir("C:\" + KryptonTextBox2.Text)
Else
'Do nothing//////////////////////////
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub newapp_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Form1.Status.Text = ("Done.")
End Sub
Private Sub newapp_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
End Sub
Private Sub newapp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub KryptonTextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonTextBox2.TextChanged
Please post the designer file. There is a button above the solution explorer that says "show all files." Click on that, go to form1, click the plus, and post all the code for the designer file.
If you need to refer to the form in the form_Load event, use me, do not use the form name
vb.net Code:
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Right way
me.label1.text = "Hello"
'Wrong way
form1.label1.text = "Hello"
End Sub
You cannot refer to the form by name, you have to use "me" Replace all the form1 with me and try to run the program again.
vb.net Code:
Form1.SaveAsToolStrip.Enabled = True
Form1.AddToolStrip.Enabled = True
Form1.EditTool.Enabled = True
Form1.ToolTitle.Enabled = True
Form1.AppTool.Enabled = True
And please wrap your code in either code tags or vbcode tags. It makes it easier to read.
I created a new app and put all your designer code in my project and it built and the form displayed. What problems are you having now? Please post the code you are having problems with (not the designer code, just the code that is not working)
well see thats what im not sure of...
There are no errors in the code...It's just when I debug the form doesn't display and it gives me the exception box with not trace of the exception.
I downloaded your app and I noticed that you are using ComponentFactory. Are you using the trial or did you pay for it? I'm pretty sure the demo expires, and if it does, that might be why you cannot build the app.
I downloaded your app and I noticed that you are using ComponentFactory. Are you using the trial or did you pay for it? I'm pretty sure the demo expires, and if it does, that might be why you cannot build the app.
Well it's a trial, but I made sure not to use any of their components in my app. So that's probably not the problem.
You are using the controls in the newapp, newconsoleapp, and newprocessflowapp forms. Remove them, put standard controls in, and try to build again.
thanks it worked!!!!!!! thank you soo much!!! this really helped!
Now I just need to figure out ONE more thing...you don't have to help if you don't want to but I'd be soo appreciative considering you've done so much already thanks.
My last questions is...I need to figure out how to remove the trial from DevComponents DotNetBar controls/components. Because everytime I try to debug, build or even just work on my project with those controls in it I get the error message of your trial period has expired. and the application closes along with visual basic 2008!
The only way to remove the trial is to buy the software. If you are not using any controls from the software, remove the reference to it from project and try to build again.
Well back to the other application.
My goal is to make it a GUI/Coding maker application.
Like visual basic itself! Ik this will be VERY hard, but I would at least like to know how to make my application generate buttons onto the form and be moved around aswell as other controls like textboxes and such.
here's how to add a button in code. if you want designer style functionality for moving + resizing your button, have a look at the move/resize controls link in my signature:
thanks soo much for the button code!! sadly I do not know where to extract your resize/moveable control to make it work with my project. and the other question I have is: Is there a way I can give the newly created button's codes. Like normal buttons?