I have DateTimePicker on my form and I want it to only display TIME
I use the code below to do that and it worked
dtpTimeIn.Format = DateTimePickerFormat.Time;
dtpTimeIn.Value = DateTime.Now;
dtpTimeIn.ShowUpDown = true;
Now my problem is, I want it to be a running time wherein the seconds is continuously running
I used DateTime.Now but it's not working
Please help me to find another solution
This works for me. Here's my VB example. The seconds/time updates every timer1.tick.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtpTimeIn.Format = DateTimePickerFormat.Time
dtpTimeIn.Value = DateTime.Now
dtpTimeIn.ShowUpDown = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
dtpTimeIn.Value = DateTime.Now
End Sub
End Class
Related
I just have tried to get list of chats and I am not able to get it.
from Chat chat in _skype.chats select new Chat
It doesn't include all chats. I have create new one and can't see it in list.
This will show all chats, including hidden ones. For this reason, you may see duplicates of chats with single contacts only.
Imports SKYPE4COMLib
Public Class Form1
Dim skype As Skype
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each r As Chat In skype.Chats
'If topic = "", then it is an individual chat
If r.Topic <> "" Then
RichTextBox1.AppendText(r.Topic & vbCrLf)
Else
'If topic = "", then display the FriendlyName instead (individual chat)
RichTextBox1.AppendText(r.FriendlyName & vbCrLf)
End If
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
skype = New Skype()
skype.Attach(7, True)
End Sub
End Class
Imports AForge.Video
Imports AForge.Video.VFW
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim Stream As AForge.Video.MJPEGStream = New AForge.Video.MJPEGStream("http://192.168.1.250/mjpeg.cgi")
Stream.Login = ("admin")
Stream.Password = ("admin")
Stream.Start()
Dim st As String = ""
Catch ex As Exception
MsgBox(ex.Message.ToString(), MsgBoxStyle.OkOnly, "Error")
Exit Sub
End Try
End Sub
End Class
this is what i have done so far....to get stream started...
know i want know.
how can i show this stream in picture box? and how to get images from this stream.
The MJEGStream includes events. Declare your Stream object on class level as
Private WithEvents Stream As AForge.Video.MJPEGStream
Then add an event handler to the MJPEGStream.NewFrame event with a sub like
Private Sub Handle_NewFrame(sender as Object, e as AForge.Video.NewFrameEventArgs)
Picturebox1.Image = e.Frame
End Sub
Start the stream like you have done in your code.
Don't forget to initialize the Source object.
Source = New AForge.Video.MJPEGStream("http://192.168.1.250/mjpeg.cgi")
The eventargs variable contains the new frame as an image that you can then display in a picturebox.
It's been a while since I have worked with AForge, but it should be ok.
I've got an app using .net UIAutomation, it eventually runs out of memory and crashes just monitoring windows being shown and closed. Seemed easier to show this in VB than C# but happens the same either way. It appears to be a leak/pool in the underlying proxy objects. Most of the memory is not being shown as in use as .net memory.
Any ideas on how to get this to stop leaking and still monitor StructureChangedEvents?
Imports System.Windows.Automation
Public Class Form1
Delegate Sub AddListCallback(ByVal Text As String)
Dim UIAeventHandler As StructureChangedEventHandler
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BtnStartStop.Text = "Stop"
Subscribe()
End Sub
Private Sub BtnStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStartStop.Click
If "Start" = BtnStartStop.Text Then
BtnStartStop.Text = "Stop"
Subscribe()
Else
BtnStartStop.Text = "Start"
Unsubscribe()
lbEvents.Items.Clear()
GC.GetTotalMemory(True)
End If
End Sub
Public Sub Subscribe()
UIAeventHandler = New StructureChangedEventHandler(AddressOf OnUIAutomationEvent)
Automation.AddStructureChangedEventHandler(AutomationElement.RootElement, TreeScope.Descendants, UIAeventHandler)
End Sub
Public Sub Unsubscribe()
Automation.RemoveStructureChangedEventHandler(AutomationElement.RootElement, UIAeventHandler)
End Sub
''' <summary>
''' AutomationEventHandler delegate.
''' </summary>
''' <param name="src">Object that raised the event.</param>
''' <param name="e">Event arguments.</param>
Private Sub OnUIAutomationEvent(ByVal src As Object, ByVal e As StructureChangedEventArgs)
' Make sure the element still exists. Elements such as tooltips can disappear
' before the event is processed.
If e.StructureChangeType = StructureChangeType.ChildrenInvalidated Then
Exit Sub
End If
Dim sourceElement As AutomationElement
Try
sourceElement = DirectCast(src, AutomationElement)
Catch ex As ElementNotAvailableException
Exit Sub
End Try
' TODO Handle any other events that have been subscribed to.
Console.WriteLine( "Element : """ & sourceElement.Current.LocalizedControlType & """ """ & sourceElement.Current.Name _
& """ Struct Change Type : " & [Enum].GetName(GetType(StructureChangeType), e.StructureChangeType) _
& " Runtime ID : " & getString(e.GetRuntimeId()))
End Sub
Private Function getString(ByVal ints As Integer()) As String
getString = ""
For Each i As Integer In ints
getString = getString & " " & i.ToString
Next
End Function
End Class
I would do something like this when you unsubscribe:
Automation.RemoveAllEventHandlers();
UIAeventHandler = null;
UIAutomation will keep some threads alive as long as you've got an AutomationEventHandler object around. Its pretty much a black box to me, but the above has fixed all issues that I had.
#jaws is correct (there are some additional things to think about), but it is in C#. for VB you need to do the following:
Automation.RemoveStructureChangedEventHandler(AutomationElement.RootElement, UIAeventHandler)
UIAeventHandler = Nothing
Because you do not set the UIAeventHandler to nothing, when you unsubscribe then, you are re-assigning the event handler which is already in memory.
To think about. Notice that you are calling subscribe when the form loads and subscribe/unsubscribe with a button. When the form loads then, the event handler is assigned and should work fine, that is until you hit unsubscribe/subscribe (in that order). When you did not set the UIAeventHandler to Nothing then, you are causing a memory leak because you then set the UIAeventHandler to a NEW instance on subscribe. Therefore, you could also do the following as suggested by #jaws :
Imports System.Windows.Automation
Public Class Form1
Dim UIAeventHandler as StructureChangedEventHandler
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BtnStartStop.Text = "Stop"
Subscribe()
End Sub
Public Sub Subscribe()
UIAeventHandler = New StructureChangedEventHandler(AddressOf OnUIAutomationEvent)
Automation.AddStructureChangedEventHandler(AutomationElement.RootElement, TreeScope.Descendants, UIAeventHandler)
End Sub
Public Sub Unsubscribe()
Automation.RemoveStructureChangedEventHandler(AutomationElement.RootElement, UIAeventHandler)
UIAeventHandler = Nothing 'Here is the Important part!
End Sub
End Class
After finding this question and following the instructions there, plus following many of the links, I managed to get my app to appear as a system tray icon, and it disappears from the task bar when I minimize it.
BUT - weird behavior! When the form is open, the system tray icon works fine. But as soon as I minimize it, the tray icon stops responding to any kind of mouse click.
Other facts that may come into play: the form is started up by a thread spun off from the main thread, and it is opened with .ShowDialog(). There are also several other threads running in the background.
Any ideas what's going wrong?
Replace f.ShowDialog(); with Application.Run(f); where f is the variable holding your Form and it should work fine. The problem is that ShowDialog returns (and it's message loop ends) when you hide the form. Application.Run provides a proper message loop and your window works after it has been hidden and can be show again using the system tray icon.
Not too sure what your code looks like, but below is some code I pulled from one of our apps. Its written in VB but should not be too hard for you to convert. The key is to create an ApplicationContext class to host your code. This code has no problems showing a form and then closing it repeatedly or with minimizing or maximizing.
Public Class NotifyApplicationContext
Inherits ApplicationContext
Private components As System.ComponentModel.IContainer
Private Notify As System.Windows.Forms.NotifyIcon
Private Menu As System.Windows.Forms.ContextMenu
Private mnuForm As System.Windows.Forms.MenuItem
Private F As Form
Public Sub New()
InitializeContext()
End Sub
Private Sub InitializeContext()
Me.components = New System.ComponentModel.Container
Me.Notify = New System.Windows.Forms.NotifyIcon(Me.components)
SetupContextMenu()
Notify.ContextMenu = Me.Menu
Notify.Icon = New Icon("YourApplicationIcon", 16, 16)
Notify.Text = "Your Application Text"
Notify.Visible = True
F = New Form
F.Show()
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
Protected Overrides Sub ExitThreadCore()
MyBase.ExitThreadCore()
End Sub
Private Sub SetupContextMenu()
Me.Menu = New System.Windows.Forms.ContextMenu
Me.mnuForm = New System.Windows.Forms.MenuItem
Me.Menu.MenuItems.Add(mnuForm)
mnuForm.Index = 7
mnuForm.Text = "FormText"
AddHandler mnuForm.Click, AddressOf Me.mnuTemp_Click
End Sub
Private Sub mnuForm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If F IsNot Nothing Then
If F.Visible = True Then
F.Close()
F.Dispose()
F = Nothing
End If
Else
F = New Form
F.Show()
End If
Notify.Text = "Change Application Text Here"
End Sub
End Class
Public Class Startup
<STAThread()> _
Public Shared Sub Main()
Dim N As ApplicationContext = New NotifyApplicationContext
Application.Run(N)
End Sub
End Class
Is it possible to show/hide the scroll bar in a text box only when the line count in the text box is more than the number of lines displayed?
Consider using the RichTextBox -- it has that behavior built in.
Thanks dummy, it works! Here short version of dummy answer in c#
Call this code at the end of your SizeChanged and TextChanged handlers:
Size textBoxRect = TextRenderer.MeasureText(
this.YourTextBox.Text,
this.YourTextBox.Font,
new Size(this.YourTextBox.Width, int.MaxValue),
TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl);
try
{
this.YourTextBox.ScrollBars = textBoxRect.Height > this.YourTextBox.Height ?
ScrollBars.Vertical :
ScrollBars.None;
} catch (System.ComponentModel.Win32Exception)
{
// this sometimes throws a "failure to create window handle" error.
// This might happen if the TextBox is unvisible and/or
// too small to display a toolbar.
}
Public Class TextBoxScrollbarPlugin
Private WithEvents mTarget As TextBox
''' <summary>
''' After the Handle is created, mTarget.IsHandleCreated always returns
''' TRUE, even after HandleDestroyed is fired.
''' </summary>
''' <remarks></remarks>
Private mIsHandleCreated As Boolean = False
Public Sub New(item As TextBox)
mTarget = item
mIsHandleCreated = mTarget.IsHandleCreated
End Sub
Private Sub Update()
If Not mTarget.IsHandleCreated Then
Return
ElseIf Not mIsHandleCreated Then
Return
End If
Dim textBoxRect = TextRenderer.MeasureText(mTarget.Text,
mTarget.Font,
New Size(mTarget.Width, Integer.MaxValue),
TextFormatFlags.WordBreak + TextFormatFlags.TextBoxControl)
Try
If textBoxRect.Height > mTarget.Height Then
mTarget.ScrollBars = ScrollBars.Vertical
Else
mTarget.ScrollBars = ScrollBars.None
End If
Catch ex As System.ComponentModel.Win32Exception
'this sometimes throws a "failure to create window handle"
'error.
'This might happen if the TextBox is unvisible and/or
'to small to display a toolbar.
If mLog.IsWarnEnabled Then mLog.Warn("Update()", ex)
End Try
End Sub
Private Sub mTarget_HandleCreated(sender As Object, e As System.EventArgs) Handles mTarget.HandleCreated
mIsHandleCreated = True
End Sub
Private Sub mTarget_HandleDestroyed(sender As Object, e As System.EventArgs) Handles mTarget.HandleDestroyed
mIsHandleCreated = False
End Sub
Private Sub mTarget_SizeChanged(sender As Object, e As System.EventArgs) Handles mTarget.SizeChanged
Update()
End Sub
Private Sub mTarget_TextChanged(sender As Object, e As System.EventArgs) Handles mTarget.TextChanged
Update()
End Sub
End Class
Private mPlugins As New List(Of Object)
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mPlugins.Add(New TextBoxScrollbarPlugin(txtBoxOne))
mPlugins.Add(New TextBoxScrollbarPlugin(txtBoxTwo))
mPlugins.Add(New TextBoxScrollbarPlugin(txtBoxThree))
End Sub
I've got tnimas solution working in vb. Functions quite well as written and I've not seen the errors.
Private Sub TextBoxSizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
Dim textBoxRect As Size = TextRenderer.MeasureText(TextBox.Text, TextBox.Font, New Size(TextBox.Width, Integer.MaxValue), TextFormatFlags.WordBreak Or TextFormatFlags.TextBoxControl)
Try
TextBox.ScrollBar = If(textBoxRect.Height > TextBox.Height, ScrollBars.Vertical, ScrollBars.None)
Catch ex As Exception
'handle error
End Try
End Sub
I myself tried tnimas' solution but unable to catch the exception, so I use the WinApi to toggle the visible state of scrollbars instead like so:
Size textBoxRect = TextRenderer.MeasureText(YourTextBox.Text, YourTextBox.Font, new Size(YourTextBox.Width, int.MaxValue), TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl);
WinApi.ShowScrollBar(YourTextBox.Handle, (int)WinApi.ScrollBar.SB_VERT, textBoxRect.Height > YourTextBox.Height ? true : false);
This method does not cause the exception, though should note that hiding scrollbars like this will disable scroll messages, but this is fine if you're just hiding the scrollbars for when the text area can't be scrolled anyway.