When I am programming in VB.net, I usually use a normal methodology (I think).
When I have a form, and some buttons placed on it, and some functions under this buttons, I implement the functionality of this buttons in the moment the button is pressed, for example:
I press a button, inside the button I use nested If..else structure to do something in order of a given condition. When the condition is true, I obtain a specific result.
I have heard (and I am not able to find information) about another methodology, where this if..else structure is not done each time you press the button, but the structure only executes one time, and not need to pass throught the if..else structure more times. Is like the program knows the solution without doing this structure all the times the button is pressed. Only needs to do it the first one.
I don't know if someone understand my question, but if someone knows about what I am talking and could give me some useful information, I would be very grateful!
i don't even know if this methodology exists, but I heard about it and I would like to investigate..
I am investigating about the performance of the programs I am creating..
You can add a member-level variable to detect if the button click handler code has been executed.
Public Class Form1
Private isInitialized As Boolean
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do some stuff if this is the first time the button was clicked.
If Not isInitialized Then
'do some stuff...
isInitialized = True
End If
'Do some other stuff each time the button is pressed.
'do some stuff...
End Sub
End Class
Thanks for your answers!
Finally I understood how the methodology I've heard works.
It is quite simple, but my question was not well made, so now I know that my answer is too vague.
I didn't know, because of my few experience programming controls, that I can made my own control which I can use in my forms. With this, I can paint this control in the way I want. I am going to put an example that answer the problem I had:
A control that will be a clock, and this clock can be analogic or digital. Before I knew this, I had a button named 'Change Clock' which change the appareance of the clock between analogic and digital. And when I press that button, in the click event of the button, I start to paint the clock, either is analogic or digital.
Now I know that programming controls, I am able to made a control by creating a UserControl in my project, and inside the code of this new control I can use the paint event to paint the clock in order of a property. Something like this:
Private Sub AnalogClock_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
Select Case m_contenido
Case formatoReloj.analogico
'Paint analogic clock
Case formatoReloj.digital
'Paint digital clock
End Select
End Sub
and when I use this control in my form, I only need to change the property of the clock and not to paint everything each time I want to change it. That means that is already painted in the code of the control and I have not to use code in my form to do it..
I hope you understand now what I wanted..
Related
I went through events in Vb.net and i found they are really an amazing feature..but still confused about how to use them effectively.
The real doubt is that the functions and sub could be effective alternative for events. Whatever i do with events i can manage to do it via functions and methods. Wherever there is Raisevent, i can substitute it with a function or procesure. so then, what is the real benefit of events and in which cases i can use them instead of functions and procedures?
Let's see if you understand it with an example. You have the Button control. It defines a lot of events (Click,MouseDown,Keypress...). When you put a button in a Form, you decide there what are the events you want to manage. So in the form you can do something when a button is clicked, or not doing anything.
Imagine if this was done with regular methods. In that case, the button should have a reference to the form where the button is, and the form should have a Click method, is it using it or not.
The event driven programing makes very easy to define some Events to signal when something happens in a class, and is the class that creates the instance the one that decides if that event is relevant or not.
thank you for clarification, I got the point. The point I Missed was that : event is fired in the same class, but execution is done in a different class.
I mean, when I define a class, I can put RaiseEvent somewhere to recognize something, but I correlate the event with a sub in a different class.
so, in such a class called Wallet I can put a statement like :
If Dollars > 5 Then
RaiseEvent Above5()
End If
and in such different class called AllWallet :
Dim WithEvents myWallet as new Wallet
Sub myWallet_Above5() Handles myWallet.Above5
MsgBox("Dollars are more than 5")
End Sub
otherwise, I mean if events are fired in the same class they are defined in , the sub and functions can substitute events.
thanks to all
I have a textbox and when I scroll down my form with the mouse wheel and hit the textbox with the mouse, it stops scrolling.
Is there a way to avoid that ?
OK, If I understand you correctly, what you are searching is to keep mouse wheeling while passing on your TextBox. Is that correct ?
There is, I think, a way to achieve that. This code however has not been tested, so keep me informed if it worked.
Public Sub New()
InitializeComponents()
'Other inits here
AddHandler TextBox1.MouseWheel, AddressOf TBMouseWheel
End Sub
Private Sub TBMouseWheel(sender As Object, e As MouseEventArgs)
Me.OnMouseWheel(e)
End Sub
This way, when your textbox captures a MouseWheel event, it is passed on the form, which will handle it (I think). Sorry I don't have the opportunity to test it right now, but I believe this should do the trick.
If you just want the visual part, change the property "Cursor" of the textbox to "Arrow"
If you don't want a TextBox to be focused, you have a few solutions
Set the ReadOnly property to True
Nobody will be able to enter text in your TextBox, exept if you do it programatically. However, it will still be possible to click on it and if clicked you will see the blinking cursor (don't know the name in English). When the mover hovers it it will turn into a cursor.
This means this option allows the focus on the control, but it will not be possible to enter data.
Set the Enabled property to False
Again, it will not be possible to enter any data. Also, it will not be possible to click on it and the cursor will not change if you hover it.
This means this option disallows the focus on the control.
Put a Label instead
If nobody will ever be able to insert data in your TextBox, maybe it is better to put a Label there. If you choose option 1 or 2, it is because at some point you might allow user to change the text inside. But if it will only be modified by the program, Label is good enough.
Focus is never allowed on Labels.
I have a timer that each time displays a message box saying "Hello." I also have the code configured so whenever the window loses focus, it should stop the timer that keeps the boxes coming. However, they keep coming.
I have tried a similar thing in a similar program with way too long of code to post here, but what it did was it paused the first time, stop the timer, and when the timer was stopped again, it didn't work correctly. There was also some other code there that had a random element, that displayed a different prompt when a certain number was generated, but once it was generated, it kept using that same different prompt every time.
Is this a error of not enough time to process all the code and it "overlaps" some? I can delay the timer without that much different effects, but I think that my [lower end] CPU that it is running this program on, that with 1.6 GHz that it could handle a timer with a few message boxes. Though, VS is running at the same time, but I shouldn't have to export my code and close VS everytime that I need to test it.
If the problem is not enough time, is there a way that I can prevent my program from "multithreading" or whatever it is doing? It seems like a weird problem, but computers are very weird too. :P
Edit:
By "Focus" I mean the selected window that is the most apparent. For example, my browser is now "focused." I have been informed that the correct term is "selected." I must have been using the wrong type of event trigger... :P
It doesn't generate a lost-focus event because the form doesn't have the focus in the first place. A control on the form always gets the focus, like a Button or TextBox. You could use the Deactivate event instead.
Or just not display the message box when the Tick event fires again. Roughly:
Private ShowingMsgBox As Boolean
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'' Do stuff
''
If Not ShowingMsgBox Then
ShowingMsgBox = True
MsgBox("yada")
ShowingMsgBox = False
End If
End Sub
The underlying reason for this behavior is that MsgBox pumps a message loop. It keeps normal Windows messages getting delivered, like WM_PAINT that keeps the windows painted. And WM_TIMER, the one that generates the Tick event. The only kind of messages that it blocks are input events, mouse and keyboard messages. Otherwise the reason that Application.DoEvents() is so very dangerous. It does the same thing as MsgBox() does, without disabling input.
Create a new project with a Timer (Timer1) and write this code:
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If (Me.Focused) Then
MessageBox.Show("Hello")
End If
End Sub
If you put the mouse over your form, you would see that a message box will popup after the given Interval is over. If you don't click on the accept button and keep the mouse on the form, you would see that no further messages appear: Me.Focus is False. If you click on the accept button, the messages would start poping up; you don't even need to select the form (the focus is transferred automatically from the MessageBox to the Form).
Summary: the MessageBox does make the Form to lose the focus, although it is a kind of a "tricky" lost as far as will automatically come back after clicking on the accept button.
UPDATE: the proposed configuration does trigger a LostFocus event of the form:
Private Sub Form1_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
MsgBox("lost")
End Sub
Unlikely the other answers/comments, what I understood from your question is that you want to know the reason and if this is a normal behaviour, rather than getting a working solution to make the form to lose the focus (you are not even describing the exact conditions under which you want this to happen).
I just want to connect two arbitrary controls, so that if one is clicked, the other should act as though it's clicked - is this even remotely possible? it seems like it SHOULD be so easy, but the internet seems dry, unless I just don't know how to ask the question properly... I see a way to "click" a button control, but what if the target is not a button? - I don't know the name of any function that might be triggered by this control's click event, so I can't call it directly. I would guess there is some way of using Windows APIs, but I can't find anything that's nice, simple VB
Example
I click a Label control on the form. I want to handle that click event, run one line of code, then simulate a click event on an associated RadioButton control
Is this possible? How?
If you must, call (System.Windows.Forms.Controls.)Control.InvokeOnClick
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick%28v=vs.71%29.aspx
or even RadioButton.PerformClick
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.performclick.aspx
A better way would be to create a common Subroutine that would be called on click of either controls. This way clicking on controls will execute their own code which can differ, and some common code as well.
This is how you accomplish executing the same code regardless of which control event was fired.
Private Sub ClickMe()
'code to execute
End Sub
Private Sub label1_Click(...) ...
ClickMe()
End Sub
Private Sub rb_checked(...) ...
ClickMe()
End Sub
*edit: OK, so this is my real problem, below scenario happens only when the form is MDIChild.. thanks for anyone that could provide me with the code
I have a form with labels, panels, buttons etc. Where I'm having problem is, while form2 is my active window/form and I clicked on a control inside form1, the form1 does not activate itself. What I would like to happen is for form1 to activate even when it's not the form I clicked, only the control inside it (any control)..
I'm thinking that if I clicked a control on the form, there's an event fired on the form. If I could only know of that certain event, that would help - maybe (coz I could just add Me.activate on that event if it exists). I've tried searching for series of events when a control (ex. label) is clicked but to no avail. I hope that someone could help me with this one.
Thanks in advance.
*edit
i will just try to make my question more understandable..
How can I activate the form when only the control is clicked (say, label or textbox)? My forms does not activate or focused when I click inside it except the form itself..
I can do this on one control..
Private Sub Label1_Click - Handles Label1.Click
Me.Activate()
End Sub
But what if I have 20 controls (labels, buttons, textbox, combobox, etc)? See? =)
EDIT: this answer does not apply to MDI applications.
I think what you really want to know is which one of your forms is currently the foreground window (if any). The first thing you need to understand is that a form instance lives inside a window, but the window's behavior is controlled somewhere higher up. Similar to how a form instance is identified by a variable pointing to the instance, a window can be identified by what's known as a window handle.
Knowing this, the proper way to find out whether a form is the "active" form is to:
find out the window handles of the windows containing our instances of Form1 and Form2
find out the window handle of the foreground window (which can be any window)
compare the value found in step 2 to all of the values found in step 1
Perhaps you'd then like to fire an event if the foreground window changes, but I'll leave the actual implementation up to you. There are probably several ways to perform step 1 and 2, but I can't give any solutions off the top of my head. Hopefuly I've put you back on the right track.
EDIT
Alternatively, you can use the form's Containsfocus property. If its value is True, you can safely assume that your form is the foreground window. I didn't find out about this property until after I wrote my own implementation, which I'll show you anyway:
One module containing only a windows API call
Friend Module NativeMethods
Friend Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
End Module
Calling this method will return the window handle of the foreground window (if any).
One module containing the extension method for the Form class
Imports System.Runtime.CompilerServices
Public Module FormExtensions
<Extension>
Public Function IsForeground(f As Form) As Boolean
Return (f.Handle = NativeMethods.GetForegroundWindow)
End Function
End Module
Calling this method returns whether the specified form f has the same window handle as the foreground window.
Usage example
You could use a Timer that periodically checks whether a form is the foreground window.
Public Class Form1
Private WithEvents timer As New Timer With {.Enabled = True}
Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
If Me.IsForeground() Then
Console.WriteLine("this instance of Form1 is the foreground window")
End If
End Sub
End Class
Like I said before, you can use Me.ContainsFocus instead of my extension method and it will work just fine.
In non-MDI forms, the form is automatically activated when you click any control inside it.