Changing windows form size - visual-studio-2010

I am using VB 2010 to create a program that can access information stored in an Access database. Whenever I populate a list box to show the content of a database via data binding, it causes the form window and controls to resize themselves when run. It then alters the size of all other forms and controls opened while the program is running.
I am unsure as to how to stop it from doing this. When I remove the data binding, the form size reverts back to how it should be. How do I correct this? The code I'm using to do this is as follows. Is it the listbox or the code that's causing this unwanted resizing?
Private Sub C172_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'C172DataSet.C172' table. You can move, or remove it, as needed.
Me.C172TableAdapter.Fill(Me.C172DataSet.C172)

Related

OwnerForm loses focus into the background?

I'm facing a weird case in VB6, which is, when I show a form in respect with another OwnerForm, if I did this two/three times for example, when I close all the child forms, suddenly the OwnerForm disappear on background (lose focus and the previous application will be on top), and I will have to click on it on the task bar to show the form again!
A quick sample will be something like that:
Private Sub Command1_Click()
Command1.Enabled = False
Dim frm As New Form1
frm.Show 0, Form1
End Sub
Is there any solution for this?
Thanks.
You're showing the child form on top non-modally, ie, you can have any number of child forms of that type open. When you close them, sometimes the form / app that spawned them will lose focus and disappear behind other applications - as you are experiencing.
You might be able to hide the form being closed (use the _QueryUnload event and cancel the unload), hide, then set the ZOrder on the parent form (to bring it to the front), then carry on unloading the form that is being closed.
Let me know how you get on.

How to save an Excel file via VB 2010 without any dialogs (such as "save as")

I am trying to Save an Excel file Via VB 2010, and I have these questions
How can I disable the "save as" dialog? I tried things such as only "save" instead of "save as", but it doesen't work...
After I saved the file (using the save as) I can't Delete it... (I tried closing the excel file, Visual basic etc...) all i get is an error saying it is allready open in excel...
Is there a way to make VB show me the tips for writing the excel stuff (Ie - when I write messagebox. - it pops up "Show" for help. how can I enable this for excel code [worksheets.cells. ect.])
the connection:
Sub Connect()
' Connect to the excel file
oExcel = CreateObject("Excel.Application")
'Devine the workbook
oBook = oExcel.workbooks.open("e:\Words\Heb.xls")
End Sub
the saveas:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
oExcel.SaveAs(oExcel.Path & ".xls")
End Sub
Thanks a lot
I think Inafiziger has solved your main issue, it should be a vanilla Save.
As it was unclear to me exactly what your are doind (ie Visual Studio/VB/BA) then
On (1)
I thought it worth clarifying that you can use code inside the ThisWorkbook module to detect and handle a SaveAs if you are providing users with a choice. This Event detects the SaveAs and cancels it
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox "You cannot use SaveAs to save this file", , "Save Cancelled!"
Cancel = True
End If
End Sub
This code can be programmatically added to your target workbook but I doubt you would need to resort to this given you should be able to run the simple Save.
On (3)
You need to use Early Binding to get the benefit of intellisense. You are currently use late binding with oExcel = CreateObject("Excel.Application"). A commonly used approach is to write the code and get it working with early binding, then converting it to late binding for final code publication.
Conditional Compilation (see comment at bottom) can be used to switch between the two binding methods within the same code.
You should be saving the workbook. e.g. oBook.Save.
If you create a new file, you will need to use SaveAs with a valid filename in order to save it the first time.

How to clear the stack of forms properly in VB6

I have an application in VB6 for which I am implementing the Autologout functionality. Whenever the application is in the idle state for some time, the user will be logedout by closing all the forms that user has loaded.
I am using a FormStack and unloading all the forms that has been loaded. Inorder to close the forms in a proper way we are using SendKeys ESC and the form will unload with ESC key.
It is working fine in most of the cases, but the problem is if there is something like Keyboard, then with the ESC key the keyboard is getting unloaded, but once all the forms are unloaded, the code next to Keyboard is getting executed which has a reference to forms control and loading the form.
I can do the reference checks, but this functionality is implemented in many places, and it takes time to check everywhere.
I also thought to wait for some time around 500 ms to close every form, but felt it is not good to rely on timers.
Could anyone suggest a better idea to unload all the forms smoothly.
i used to do
dim i as integer
for i = forms.count -1 to 0 step -1
unload forms(i)
next
also you can create a interface an implement that in the form
IDispose
sub Close()
dim miclazz as IDispose
form each miclazz in forms
miclazz.Close()
next

GUI freezing after datagridviews have been updated using backgroundworkers

I have made an application which runs three backgroundworkers simultaneously. It basically updates three datagridviews, which it is doing spot on. My problem is if I press maximizebox button or anywhere in any of the datagridview the program hangs for quite a long time. I am able to use the horizontal scroll but not vertical scrolls. I have tried Backgroundworker_runworkercompleted and it fires as required after threads have updated their respective datagridviews. Is it a normal behaviour or am i doing something wrong any suggestions would be helpful.
P.S: I have run the whole program using step method and their is no infinite loop in the code.
Thanks in advance
Jhon
Place this line of code just before when you are calling Backgroundworker1.RunWorkerAsync() to temporarily disable your datagridview scroll bars.
DataGridView1.ScrollBars = ScrollBars.None
Now Re-enable scroll bars of your datagridview by adding this line of code in BackgroundWorker1_RunWorkerCompleted event.
DataGridView1.ScrollBars = ScrollBars.Both
This will never make your application unresponding.
It sounds like you are still blocking the UI thread somehow. It may be helpful for you to post some code snippets. Also, what is the CPU utilization of your process? If the cpu usage is high, you may be starving the UI thread somehow.
Okay I have found the solution to my problem, while working out sequential elimination of the perceived trouble spots, I called my datagridview outside of the backgroundworker.dowork event and voila that solved the problem.
Moral of the story "NEVER UPDATE A DATA GRID VIEW FROM WITHIN THE BACKGROUNDWORKER THREAD" specially when you don't know what are you doing wrong :). I hope it will help someone in future.
You have to make sure that you are updating the datagridviews from the UI thread.
I had the same problem. When my dataGridViews were being updated from the UI thread, they worked fine. When I tried to add a series of rows to them from the backgroundWorker, they became unresponsive.
To correct this, I added the rows to a dataTable instead. When I kick off the backgroundWorker, I set the .datasource property of the dataGridView to nothing. When the worker completes, I set it back to the dataTable again. This both forces the dataGridView to update itself, and severs the synchronous connection between what it happening in the worker thread, and what is being displayed in the UI, which seems to alleviate the unresponsiveness when the worker completes.
Sample usage:
Private Sub button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If bgWorker1.IsBusy <> True Then
dataGridView1.DataSource = Nothing
bgWorker1.RunWorkerAsync()
End If
End Sub
Private Sub bgWorker_DoWork(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bgWorker1.DoWork
dataTable1.Rows.Add("data")
End Sub
Private Sub bgWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgWorker1.RunWorkerCompleted
dataGridView1.DataSource = dataTable1
dataGridView1.Refresh()
End Sub
It's too late to answer to #Jhon but it seems like it might help other people.
In my case, I was using a BindingSource and the UI only froze if there where so many records that scrollbars had to appear.
When you use a background worker, task or thread to do the work, you have to update your UI controls on the main UI thread.
To do that you can Invoke a method in the corresponding thread.
A simple example:
myDataGridView.Invoke((MethodInvoker)delegate { myBindingSource.DataSource = myData; });
But i prefer this solution:
InvokeIfRequired

Disable column in MSHFlexGrid in VB6.0

How do I disable particular columns in MSHFlexgrid in VB6.0? I don't want my user to edit the values in a particular column.
I don't think the MSHFlexGrid control allows users to edit its data in the first place. Therefore, in effect, all columns are disabled. Job done :)
In fact, you have to add custom code to enable updating e.g. add an appropriate control (textbox, combo, date picker, etc) that does allow editing, hide it at design time, then at run time detect which grid cell should have focus, move and size the control to fit the cell then make it visible then handle events to validate the input then write the contents back to the recordset...
...or you could purchase a third party control that does all this out of the box. The MSHFlexGrid that ships with VB6 is essentially a cut-down version of VSFlexGrid Pro, which I've used and thought was quite good. It has a different way of handling hierarchical data by creating groups (rather than bands) which is superior, IMO. The best thing that can be said about the MSHFlexGrid is that it is easy to bind to a hierarchical ADO recordset to simply display the results but not good if you want to do nice formatting or make the grid editable. The VSFlexGrid Pro, if you can afford it, has more power e.g. you can create data source classes to handle binding to custom data structures (ships with VB6 examples of this including ADO recordset binding) which would be invaluable IMO if you intend to make your hierarchical grid editable.
'A Shortcut way is here... NOT in a proper way. But you can try
'if you need to lock the first 3 columns please use this code:
msf2=name of MSFlexGrid
Private Sub msF2_EnterCell()
With msF2
If msF2.Col = 0 Or msF2.Col = 1 Or msF2.Col = 2 Then
msF2.Col = 3
End If
End With
End Sub

Resources