Show Form Above System Tray - vb.net

I am working on a tool that docks primarily in the system tray. However, if the icon is clicked, the form opens for more options displayed on the form. (Not a context menu) However, I want to position the form directly above the system tray and have no clue how to go about doing this. This application will run on multiple user computers with varying screen resolutions, so hard coding a preset coordinate is not a viable solution.
I did search for FindWindow() for the system tray but could not find anything useful out of Google/Bing.

There is a Screen.WorkingArea property which allows you to get the primary screen "available resolution".
Just put your window top at working area height - window height and window left at working area width - window width (and adjust if necessary to get a bit of margin).
Update: Also, account for the taskbar position and size: How do I get the taskbar's position and size?

You could use this:
First Set The form TopMost
MyForm.TopMost = True
To show the form on top of taskbar on right side, here is example:
Private Sub SetFormPosition()
Dim leftpos As Long
Dim toppos As Long
leftpos = (My.Computer.Screen.WorkingArea.Right - 2) - Me.Width
toppos = (My.Computer.Screen.WorkingArea.Bottom - 2) - Me.Height
Me.Location = New Point(leftpos, toppos)
End Sub
You can play around and change Values. (-2) is close to edge, if you put bigger number you will be away from the edge and so on. You can also change the My.Computer.Screen.WorkingArea where you want to show the Form.
You can use the code on NotifyIcon_MouseClick Event
Private Sub NotifyIcon_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon.MouseClick
If e.Button = Windows.Forms.MouseButtons.Left Then
SetFormPosition()
MyForm.Show()
End If
End Sub

Related

Which is the most suitable way to build a graph digitizer?

I have to build a graph digitizer to make the user have a graph by points from an image. This could be an example of an imported graph
What I'd like is that the user graphically defines, at first, the X axis domain (in the picture from 0 to 2000) and the Y axis domain (in the picture from 0 to 180) and then picks some points on the curve and, once this procedure has ended, I need to have the points added to a Datagridview that I've already done. Could anyone suggest me where to start?
Edit: I've set the way to store the mouse position, when the mouse button is pressed:
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
If PictureBox1.Image IsNot Nothing Then
Form3.ListBox1.Items.Add(e.X)
End If
End Sub
I need to activate this sub when I press a specified button. Until I press the button, the mouse position, even if clicking inside the picturebox, must not be stored. If tried to put the picturebox sub inside the Button_click sub but It doesn't work.
Another event that I have to program is that - when pressing the mouse button inside the picturebox - on the first click, Form4 must be shown; at the second click, Form5 must be shown; at the third click, Form6 must be shown. From the fourth click (until the user has ended to pick points), the mouse position must be stored.

How to hide a DataGridViewButtonCell

I have a DataGridViewButtonCell in my DataGridView and I wanted to set the property Visible to True.
I have tried:
DataGridView1.Rows("number of row i want").Cells("number of cell i want").Visible = True
Unfortunately it says that the property visible is read only.
Here is the code:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'does not work
DataGridView1.Rows(e.RowIndex).Cells(6).Visible = True
End Sub
Does anyone knows how I can achieve this?
Thanks.
There is no actual way to hide a DataGridViewButtonCell. Currently I can only see two options:
Use padding to move the button over as shown here. I will provide similar VB.NET code
Set the Cell to a DataGridViewTextBoxCell and set the ReadOnly property to True
Use Padding:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
Dim columnWidth As Integer = DataGridView1.Columns(e.ColumnIndex).Width
Dim newDataGridViewCellStyle As New DataGridViewCellStyle With {.Padding = New Padding(columnWidth + 1, 0, 0, 0)}
DataGridView1.Rows(e.RowIndex).Cells(6).Style = newDataGridViewCellStyle
End If
End Sub
Use DataGridViewTextBoxCell:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If DataGridView1.Rows(e.RowIndex).Cells(6).GetType() Is GetType(DataGridViewButtonCell) Then
Dim newDataGridViewCell As New DataGridViewTextBoxCell
DataGridView1.Rows(e.RowIndex).Cells(6) = newDataGridViewCell
newDataGridViewCell.ReadOnly = True
End If
End Sub
Both of these should give you the effect of not showing the button.
This is really a perspective issue. From a programmer’s perspective, simply ignoring the button clicks on the buttons I want to disable is very easy to do and takes just a few lines of code.
From a user perspective, this situation would play out like this… the user clicks what appears to be a valid enabled button, and nothing happens. The user did not write the code for this… so at best the user will think the computer is not responding to the button click or at the worst… would think your coding skills are dubious!
The same situation happens if the button is missing. The user is not going to know why it is missing… but will most likely come to the same conclusion described above with a non-working button.
In another very simple approach, let say that all the buttons are enabled and we have a list of the button indexes we want to disable. The users presses one of the buttons, we check the disabled button list and if the clicked button is one that is disabled, simply display a message box to indicate why this button is disabled. This approach says to the user… “Here are a bunch of buttons, guess which ones are enabled”…
The DataGridViewDisableButtonCell and DataGridViewDisableButtonColumn wrappers solve all of the above issues… the button is visible so the user wont question where the button went if you set it to invisible and it is greyed out. “Greyed out” is something most users understand and will relieve the user of having to “guess” which buttons are enabled.
You can create a wrapper for two classes: the DataGridViewButtonCell and the DataGridViewButtonColumn.
The link How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control to the MS example is one I have used before using C#, however there is a VB implementation at the link also.
Below is a picture of the result of using the two wrappers described in the MS link. For testing, the picture below uses the check boxes to left of the button to disable the button on the right.
IMHO, using this strategy is user friendly. If you simply make the button invisible or read only, then the user is possibly going to think your code is messed up and not have a clear understanding of WHY the button is missing or doesn’t work. A disabled button indicates to the user that the button is not available for that item. An option would be to have a mouse roll-over indicating why the button is disabled.

Tooltip display in VB.NET

Using either the Designer properties or by programming it directly, I cannot seem to get a tool tip to display the way I thought I should be able when I hover over the control.
For instance, although I can display the text I want, no matter what I set as the AutoPopDelay (I want 30 seconds) or as the background color (I want a Yellow), the tool tip always comes up for the default 5 seconds on a gray background only.
Below is the sub that I programmed. What am I missing?:
Private Sub lblUploadFileTypeHelp_MouseHover(sender As Object, e As EventArgs) Handles lblUploadFileTypeHelp.MouseHover
ToolTip1.OwnerDraw = True
ToolTip1.IsBalloon = True
ToolTip1.BackColor = Color.LemonChiffon
ToolTip1.AutoPopDelay = 30000
ToolTip1.Show("Sample text to display", lblUploadFileTypeHelp)
End Sub
IsBalloon is being taken into consideration before OwnerDraw.
You would need to handle your own drawing to get a custom display.
Have a look at the MSDN code sample MSDN Tooltip

Toggling panel visibility isn't working when they are stacked

I have a form that has two views. These views are controlled by radio buttons on top of the form.
Here is the program:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc1.PNG
Notice how the Radio button for Number Converter is selected.
Here is what it looks like when you select the Text Converter radio button:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc2.PNG
That isn't right. I have it set to hide the panel containing the number converter and show the one containing the text converter when you click that one. It hides the number converter but doesn't show the text converter.
Here is a picture of the text converter panel:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc4.PNG
Here is the relevant code:
Private Sub frmCalculator_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
rdoNumberConverter.Checked = True
End Sub
Private Sub rdoTextConverter_Click(sender As Object, e As System.EventArgs) Handles rdoTextConverter.Click
pnlTextConverter.Visible = True
pnlNumberConverter.Visible = False
End Sub
Private Sub rdoNumberConverter_Click(sender As Object, e As System.EventArgs) Handles rdoNumberConverter.Click
pnlNumberConverter.Visible = True
pnlTextConverter.Visible = False
End Sub
Everything seems right and I can't figure out why the text converter doesn't show up. I've determined that it has something to do with the fact that both of the panels are right on top of each other because when I move them apart, the visibility toggling works perfectly.
Here are the supporting pictures:
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc5.PNG
http://dl.dropbox.com/u/41629841/DataCalculator/DataCalc6.PNG
So how do I make it work when they are on top of each other?
I tried using BringToFront() and SendToBack() to make sure the visible panel is in the front and it didn't make a difference.
Make sure the TextConverter panel isn't "inside" the NumberConverter panel.
From the designer, move them into different places so that they do not overlap at all.
Then in code, move them into place:
textConverterPanel.Location = numConvertPanel.Location
Your visible, not visible toggling should work then.
The issue is the panels becoming embedded, as pointed out by #LarsTech. This occurs if you use the GUI to move them to the same location.
If you want to overlap them at design time, create the second panel in a different location. Then in the Properties of the panel in the final location, copy the Location, and paste it into the Location property of the second panel. This will move it to the proper location in the Designer without embedding one into another. This can be repeated for as many additional panels as needed.

Winform Textbox CanGrow?

I don't find a CanGrow property on the Textbox control. This is common in some other controls, and what it does is expand the control to acomodate more data. Anyway to get this feature in the TextBox?
Well, I came up with this:
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox.TextChanged
'check to see if textbox has text
If (TextBox.TextLength > 0) Then
'resize height of textbox by count of lines (plus add some padding)
TextBox.ClientSize = New Size(TextBox.ClientSize.Width, Convert.ToInt32((TextBox.Lines.Length * TextBox.Font.Height) + (TextBox.Font.Height * 0.5)))
Else
'resize to one line height (plus padding)
TextBox.ClientSize = New Size(TextBox.ClientSize.Width, Convert.ToInt32(TextBox.Font.Height + (TextBox.Font.Height * 0.5)))
End If
End Sub
Note: it doesn't work with word-warp.
I'm not familiar with CanGrow. Are you looking for Anchor property perhaps?
Anyway to get this feature in the
TextBox?
Well, yes, but, you may need to look into doing this manually. The Graphic.MeasureString() function may be what you are looking for in order to set the width properly.
Keep in mind that MeasureSting may have issues measuring multiline strings.
If you set the anchor properties to top,left,bottom,right then the control will grow as the form resizes.
I think a better option is to use docking though. I usually set up a panel layout with one docked to client, then I put the control I want resized in the panel docked to client, and set the control to dock to client as well.