listview in childform - vb.net

I have 3 forms, one frmMain - main form, second is frmUserType- childform, and the last frmCreateUserType. In the main form I have a menu item to open my frmUserType, In this form I have a button to open another form which is my frmCreateUserType, In this form I have a button to add records then update the listview in frmUserType. The problem is the listview will not access with my add button control in frmCreateUserType. I tried not to used mdiparent declaration for my frmMain and frmUserType as children and it works, so meaning that the problem is showing my frmUserType as childform?I am using vb.net 2008
Any suggestion would greatly appreciated
Thans in advance
Code to open my second form (frmUserType)
Dim frmChildUserType As New frmUserType
frmChildUserType.MdiParent = Me
frmChildUserType.WindowState = FormWindowState.Maximized
frmChildUserType.Show()
Code for my add button to update the listview in frmUserType
frmUserType.lsvUserType.Items.Clear()
FillListViewUserType("SELECT * FROM pr_user_type", frmUserType.lsvUserType)

You're creating a new instance as:
Dim frmChildUserType As New frmUserType
But in your code:
frmUserType.lsvUserType.Items.Clear()
FillListViewUserType("SELECT * FROM pr_user_type", frmUserType.lsvUserType)
You're not accessing that instance but is instead using the default instance of frmUserType. So I think you're updating a different instance of the ListView than what you think.
If you change your code to instead be:
frmChildUserType.lsvUserType.Items.Clear()
FillListViewUserType("SELECT * FROM pr_user_type", frmChildUserType.lsvUserType)
I think it would work as you expect.
If you don't know what a default instance is, you can find a blog about them here (I think they're a bad idea).

Related

Me.controls.Item on a VB.net Form is returning System.nullReferenceException 'object reference not set...'

I've created a vb net form that uses an array list to load data into various controls. (ultimately this is to interface with the Autodesk Inventor API, but I'm not failing the API part)
My first form(1) works correctly. I needed to break my form into multiple forms due to too much information. In Visual Studio 2022 I copied my Form1, pasted it and renamed it to Form2 then changed the references to Form1 located in Form2.designer.vb. To switch between Forms, I created a 'MainMenu' form to call either Form1 or Form2
By all manner of double checking, my method of calling my Form1 or Form2 are a copy paste (other than form names)
On my new form2, I cannot access any controls using the Me.Controls.Item method. I use a button to run the population of my textbox controls, so they exist.
works:
Me.textbox1.text = "some string"
Me.text = "form name string"
doesn't work:
Me.Controls.Item("textbox1").text = "any string"
In debug, mousing over the error line I get:
Me = {Projectname.Form1...} Me.Controls = {System.Windows.Forms.Form.ControlCollection} Me.Controls.Item("textbox1") = Nothing
In the Autos window, I can see my textbox1 in Me.Controls > Owner > textbox1 (and my 'some string' value)
I can also see my control in the Locals Me > textbox1
I guess I did something wrong when Copy/Pasting my Form1 to Form2
I created a new Form3 and copy pasted the controls and the Code from my Form2 and I have the same issue.
I have crated a new Form4 and created a new control (textbox) without a copy paste and things seem to work on that form, so far.
I'm sure somebody could fix this in seconds, but I'm tapped out. My Form2 (i could have sworn it worked before) has a lot of time into formatting multiple layers of tablelayoutpanels and would really like to not re-create my form2 from scratch.
If Me.Controls.Item("textbox1") is Nothing then there is no control whose Name property is "textbox1" and whose direct Parent is the form. As you can see, there are two conditions that could cause that to be the case. The control you want does not have its Name property set to "textbox1" and/or its direct Parent is some other control, e.g. a Panel.
Personally, I would scrap that form and create a new one the conventional way. Obviously you have done something wrong so who knows how far-reaching the issue(s) is?

Add item to listbox from another form

I'm trying to add items to listbox from another form but the listbox doesn't seem to update.
I have 3 form : frm1, frm2, frm3
this my code in frm1 to open frm2:
Using frm As New frm2
frm.ShowDialog()
End Using
frm2 has a listbox named list_xxx
code in frm3:
Private Sub add_item()
frm2.list_xxx.add("aaaa")
End Sub
i want to add item to frm2 from frm3. but no success the listbox still empty.
how to fix it?
You are almost certainly referring to the default instance of frm2 in that second code snippet but you aren't displaying the default instance in the first code snippet. If you display one form and then add items to a ListBox on another form, there should be no surprise that you can't see those items. Either use the default instance everywhere or not at all. The more correct option is not at all, but that would require other changes too. The easier option is everywhere, which means changing the first code snippet to this:
frm2.ShowDialog()
No Using statement because you're not creating an object.

Refreshing form from timer in another form

I have a problem with the snippet of the code I have got to extend and improve. It is not my original code and I cannot change the logic that much of it, just to be clear.
I have one main form called MDIServer, which has a timer set for every second. In Timer.Tick I have some other code which works fine (timer is running okay). Newly, I had to check there, if one form is Active and if so, change some stuff (labels text and tags) in that form and refresh it.
I add there this code:
If IsActiveForm("frmName") Then
frmName.ChangeSomething()
End If
The Sub ChangeSomething is, how you can see, located in the form I want to refresh and do the changes. In that function I simply change the label text and tags of few controls.
My question is: Form is not refreshing => labels are not visible changed, why?
I think I tried already almost anything with Refresh() function in the ChangeSomething() function or in the timer after I called this function. Also I tried to add there new timer (in frmName) and do the changes there, which works perfectly with
Label.Text = "something new"
Label.Refresh()
So I guess problem is somewhere with the refreshing form from Timer in different form. I also tried to do it with my own InvokeReguired() function etc...
P.S. When I am debugging the code, labels and tags are changing and every single function which has to be called, is called, but it is just not visible on the form itself.
EDIT Info
formName is not declared in MDIServer explicitely and in this case and many other cases, forms are used as default instances. Timer is from System.Windows.Forms.Timer. Also MDIServer is not a MDIParent of the formName and I cannot use Me.ActiveMdiChild Is. Lets just say, these two forms are not dependent on each other in any way.. and everything is done through name of the form (default instance, so nothing like Dim frm As Form and frm = frmName).
I would be really glad for any tip or anything :D
Thanks guys,
Vojta
So, I fixed my problem after some research and the problem was (expected) that I am not calling the subroutine ChangeSOmething() for one specific instance of the form frmName. So I had to change my code, that I will call it exactly for the instance which is active and visible.
New code looks like this:
Dim frmCollection = Windows.Forms.Application.OpenForms
Dim listfrmname = frmCollection.OfType(Of frmName).ToList()
If listfrmName.Count > 0 Then
Dim tmpFrm As frmName = listVZT15.Last()
tmpFrm.ChangeSomething()
End If
I also could not use combination of frmCollection.OfType(Of frmName).Any and frmCollection.Item("frmName"), because when I was closing the form and opening again, it created new and new instances (I dont know, why it is not closing the old one, but like I said, it is not my code). So the logic is, to list all open forms of the needed type, and then take the last instance from that list and for that instance call the subroutine. Also Me.Refresh() is placed in the subroutine ChangeSomething() itself.
Thanks everyone for help, it surely helped me to understand, how the instances works here.

setting the startup position of child form in vb.net

I have a Mdi parent container and i am using menu item to open child forms in MDI Parent Form.
Here is the code for opening the child form
Dim childform1 as new Form3
childform1.MDIParent = me
childform1.show()
The above code is working fine. The problem is with the startup position of the child form. i.e. child form doesn't open at required position(just right below the menubar) instead it opens at position randomly. like sometimes near the required position and other times where it wishes.. lol is there any standard way to position it. I tried MDI Child Form Start Position Problem answer by calculating and its positioned near by where i want. But I want to look for a standard way for doing this.
This should do it:
childform1.location = new point(x, y)
Inside the childform, you can add this:
Me.StartPosition = FormStartPosition.Manual
Then you can set location before saying childform1.show()

How to make a form always stay on top of another form

How to make a form always stay on top of another form.
Also both form's enabled property must be true
I don't wanna make use of topmost property.
Edit 1 :
Another similar question in C# says you can use Form.Owner Property to do the trick , how to make use of this property ?
Edit 2 : The Owner Property works fine untill I try to open it the second time.
This is the error message I get
I believe you need the frm.ShowDialog() instead of frm.Show()
frm is the other form you need to show over your current form and instead of using Show, this will make it as a dialog form over your current form (however you won't be able to select the parent form or the form behind it unless you close the frm form
EDIT
To enable edit on both forms
Form2 frm = new Form2();
frm.Owner = this;
frm.Show();
Hope this helps you out.