Enable or visible textbox by radiobutton - vb.net

I want to enable or visible a textbox when the user check the radiobutton like a yes\no questions if the answer is no the textbox will be enable or visible.
Before I forget all these in gridview
Thank you

In your Grid's RowDataBound event find your controls first. Then set the visibility of the textbox with the condition you need.
Protected Sub gvTimeSlots_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvTimeSlots.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim RadioButton1 As System.Web.UI.WebControls.RadioButton = e.Row.FindControl("RadioButton1")
Dim TextBox1 As System.Web.UI.WebControls.TextBox = e.Row.FindControl("TextBox1")
If RadioButton1.Value="Yes" Then
TextBox1.Enabled = True
// If you want to set the visibility use
TextBox1.Visible = True
End If
End Sub

I used many solutions I found them in the internet & this one of them
Protected Sub reject_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim rbd As RadioButton = CType(sender, RadioButton)
Dim grdrow As GridViewRow = CType(rbd.NamingContainer, GridViewRow)
Dim reject As RadioButton = CType(grdrow.Cells(9).Controls(0).FindControl("reject"), RadioButton)
Dim t1 As TextBox = CType(grdrow.Cells(9).Controls(0).FindControl("t1"), TextBox)
If reject.Checked = True Then
t1.Visible = True
End Sub
when I check the radiobutton it will unchecked when AutoPostedBack True (for the 2 solutions)
my gridview name gvorders

Related

How to acces checked status of dynamicaly created Radio Buttons in VB

I create a variable amount of radio buttons in a Tab Page by reading the lines out of an array using:
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rbgen As RadioButton
Dim tab1 = 0
For y As Integer = 0 To Array.GetUpperBound(0) Step 1
If Array(y, 0) = "ABC" Then
rbgen = New RadioButton
rbgen.Name = "RButton" & Convert.ToString(y)
rbgen.Left = 10
rbgen.Top = ((tab1) * 30)
rbgen.Text = Array(y, 2)
rbgen.Size = New System.Drawing.Size(260, 40)
TabPage1.Controls.Add(rbgen)
tab1 = tab1 + 1
End If
Next
End Sub
When i click a "start" Button i need to run different code depending on the checked RadioButton. But how do i access the information of which radio Button is checked?
Thank you very much for your help!
You could try to add a Handler after you add the radio button:
AddHandler rbgen.CheckedChanged, AddressOf RadioBox_CheckedChanged
and then you need to add this sub
Private Sub RadioBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim RadioBox As RadioButton = TryCast(sender, RadioButton)
If RadioBox IsNot Nothing Then
MessageBox.Show(RadioBox.CheckState)
End If
End Sub
and if you want to check the status
For Each RadioBox In TabPage1.Controls.OfType(Of RadioButton)()
if Ctype(TabPage1.controls("RadioButton" & i), radiobutton).checked = true then
'your Code Here
end if
next
I could not test it yet but I hope I already helped you a little bit :)

Disabling button click but allowing MouseHover?

I have a button that checks a log file, and if the text doesn't match a specific string the button is disabled. I did this using:
button.enabled = False
This disables the control as intended - but I want to keep a MouseHover event enabled.
Is it possible to disable clicking?
Edit:
Changing text on hover:
Private Sub AIR_Button_MouseHover(sender As Object, e As EventArgs) Handles AIR_Button.MouseHover
Dim AIRfileReader As String
AIRfileReader = My.Computer.FileSystem.ReadAllText("X:\Logs\Air\AirConnectedUser.txt")
AIR_Button.Text = AIR_Button.Text.Replace("Air", AIRfileReader)
End Sub
Private Sub AIR_Button_MouseLeave(sender As Object, e As EventArgs) Handles AIR_Button.MouseLeave
Dim AIRfileReader As String
AIRfileReader = My.Computer.FileSystem.ReadAllText("X:\Logs\Air\AirConnectedUser.txt")
AIR_Button.Text = AIR_Button.Text.Replace(AIRfileReader, "Air")
End Sub
Disabling the button:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("X:\Logs\ConnectionState.txt")
If fileReader.Contains("AIR Account logged in") Then
AIR_Button.BackColor = Color.IndianRed
AIR_Button.Enabled = False
End Sub

Set the Progress Bar Value according to data entered?

How can Set the Progress Bar Value increasing/ decreasing according to data entered in each controls on a form ? I mean, I am working with number of controls, so go for a sample [Suppose I have four textboxes, Progress Bar Maximum 100, when entering txt_1 Progress value have to increase to 100/4= 25, if I remove data from txt_1 the Value should decreased to Zero.
Sorry for my language, I am not good in English.
Can any one help me, please..... Thank You.
The Firs thing you do is set the progressbar maximum in your formload event to the number of textboxes you are using.
Dim TextBoxNumber As Integer = 4
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Maximum = TextBoxNumber
End Sub
Then you need to make 2 events.
1. TextBox enter event."Private Sub TextBox_Enter(sender As Object, e As EventArgs)"
2. TextBox TextChanged event"Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)"
Then Link all the textboxes you want to use to the events.
In these events you will use the Sender.
Make a Dim and convert the sender to type Textbox to use its properties." Dim TextControl As TextBox = CType(sender, TextBox)"
In the Enter event you count the text that is entered in the textbox.
If the count is 0 you set the boolean to true and if its more than 0 you set the Boolean to False.
This Boolean you will use in the Text Change event.
Dim CheckTextBox As Boolean
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles TextBox4.Enter, TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
Dim TextControl As TextBox = CType(sender, TextBox)
If TextControl.Text.Count > 0 Then
CheckTextBox = False
Else
CheckTextBox = True
End If
End Sub
In the Text Change event you need to do a few things.
1.Use a if statement to check if the textbox is empty."This will only trigger if the user deletes the text in the textbox."
So if empty remove 1 from the progressbar and set CheckTextBox"Boolean" to True
"ProgressValue = ProgressValue - 1"
"CheckTextBox = True"
2.Then use a ElseIf to check if the CheckTextBox"Boolean" is True.
If true add 1 to progressbar and set Boolean to false.
"ProgressValue = ProgressValue + 1"
"CheckTextBox = False"
You need to set the Boolean to false because otherwise you will add 25 for every time you add somthing to the textbox.
Dim ProgressValue As Integer
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
Dim TextControl As TextBox = CType(sender, TextBox)
If TextControl.Text = "" Then
ProgressValue = ProgressValue - 1
CheckTextBox = True
ElseIf CheckTextBox = True Then
ProgressValue = ProgressValue + 1
CheckTextBox = False
End If
ProgressBar1.Value = ProgressValue
End Sub

Identify source chart/control from ContextMenuStrip Click

I have a form with numerous charts and have added a ContextMenuStrip when a chart is right clicked so the user can copy the chart image to the clipboard
Public Sub Chart_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, _
Chart2.MouseClick, Chart3.MouseClick, Chart4.MouseClick
If e.Button = MouseButtons.Right Then
Dim cmus As ContextMenuStrip = New ContextMenuStrip
Dim cms1 As ToolStripMenuItem = New ToolStripMenuItem("Copy as Image")
cms1.Tag = 0
cmus.Items.Add(cms1)
For Each c As ToolStripMenuItem In cmus.Items
AddHandler c.Click, AddressOf Chart_cMenu_Click
Next
cmus.Show(New Point(Control.MousePosition.X, Control.MousePosition.Y))
End If
End Sub
Then I would like to identify which chart was clicked on so that I can copy that chart to the clipboard.
This seems like a simple problem to me but I cannot figure out why no matter what I do trying to identify the Owner,Parent,SourceControl of the right click menu always returns me a Nothing value.
Public Sub Chart_cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim menuItem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim cms As ContextMenuStrip = CType(menuItem.Owner, ContextMenuStrip)
Dim _owner As Control = CType(cms.SourceControl, Chart)
Select Case menuItem.Text
Case "Copy as Image"
Dim ms As New System.IO.MemoryStream(100)
_owner.SaveImage(ms, ChartImageFormat.Bmp)
Dim bm As Bitmap = New Bitmap(ms)
Clipboard.SetImage(bm)
End Select
End Sub
Any ideas how to identify the source chart?
Have a messy solution. Going to leave this open in case a more elegant solution can be formed.
Add a name to your new ContextMenuStrip that reflects the Sender. This can then be used in the MouseClick method to find the origin
Public Sub Chart_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseClick, _
Chart2.MouseClick, Chart3.MouseClick, Chart4.MouseClick
If e.Button = MouseButtons.Right Then
Dim cmus As ContextMenuStrip = New ContextMenuStrip
'Add the name to menu
cmus.Name = sender.Name & "_CMS"
Dim cms1 As ToolStripMenuItem = New ToolStripMenuItem("Copy as Image")
cms1.Tag = 0
cmus.Items.Add(cms1)
For Each c As ToolStripMenuItem In cmus.Items
AddHandler c.Click, AddressOf Chart_cMenu_Click
Next
cmus.Show(New Point(Control.MousePosition.X, Control.MousePosition.Y))
End If
End Sub
Now find the control that corresponds to the new name
Public Sub Chart_cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim cms As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
Dim _owner As ContextMenuStrip = CType(cms.Owner, ContextMenuStrip)
'This is where you use the name attached to the ContextMenuStrip
Dim _chartname As String = Replace(_owner.Name, "_CMS", "")
Dim parentObject As Chart = Nothing
Try
parentObject = CType(Me.Controls.Find(_chartname, True)(0), Chart)
Catch ex As Exception
End Try
If Not parentObject Is Nothing Then
Select Case cms.Text
Case "Copy as Image"
Dim ms As New System.IO.MemoryStream(100)
parentObject.SaveImage(ms, ChartImageFormat.Bmp)
Dim bm As Bitmap = New Bitmap(ms)
Clipboard.SetImage(bm)
End Select
End If
End Sub
Still think there should be an easier solution than this using the likes of Parent,Owner and SourceControl
My VB is rusty, but the below code works fine for me.
Dim menu As New ContextMenuStrip()
menu.ItemClicked += New ToolStripItemClickedEventHandler(menu_ItemClicked)
menu.Items.Add("Save As Image")
chart1.ContextMenuStrip = menu
chart2.ContextMenuStrip = menu;
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs)
If e.ClickedItem.ToString() = "Save As Image" Then
Dim menu As ContextMenuStrip = TryCast(sender, ContextMenuStrip)
If menu IsNot Nothing AndAlso menu.SourceControl IsNot Nothing Then
Dim chart As Chart = TryCast(menu.SourceControl, Chart)
Dim dlg As New SaveFileDialog()
If chart IsNot Nothing AndAlso dlg.ShowDialog() = DialogResult.OK Then
chart.SaveImage(dlg.FileName, ChartImageFormat.Jpeg)
End If
End If
End If
End Sub

hiding process icon to run in background

I have a programs path..like utorrent and it pid too. I have achieved these values programatically using vb.net. I just want to hide their icon form tray just to run them in background and if possible attach the process with a hotkey to call them back. Is there any way to achieve this.
Option Strict On
Option Explicit On
Option Infer Off
Imports TrayHelper
Public Class Form1
Dim x1, y1 As Single
Friend WithEvents lv As New ListView With {.Parent = Me, .Dock = DockStyle.Fill}
Private il As New ImageList
Dim nxt As Integer
Friend WithEvents mnuContextMenu As New ContextMenu() 'Moved this to be declared as global
Dim mnuItemHide As New MenuItem()
Dim mnuItemExit As New MenuItem()
Dim things As List(Of TrayButton) = TrayHelper.Tray.GetTrayButtons()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lv)
lv.View = View.Details
il.ColorDepth = ColorDepth.Depth32Bit
lv.SmallImageList = il
lv.Columns.Add("Button Text", 300, HorizontalAlignment.Left)
lv.Columns.Add("PID", 50, HorizontalAlignment.Left)
lv.Columns.Add("Process Path", 600, HorizontalAlignment.Left)
'Dim things As List(Of TrayButton) = TrayHelper.Tray.GetTrayButtons()
For Each b As TrayButton In things
If b.Icon IsNot Nothing Then
il.Images.Add(b.TrayIndex.ToString, b.Icon)
Else
' When we can't find an icon, the listview will display this form's one.
' You could try to grab the icon from the process path I suppose.
il.Images.Add(b.TrayIndex.ToString, Me.Icon)
End If
Dim lvi As New ListViewItem(b.Text)
lvi.SubItems.Add(b.ProcessIdentifier.ToString)
lvi.SubItems.Add(b.ProcessPath)
lvi.ImageKey = b.TrayIndex.ToString
lv.Items.Add(lvi)
Next
lv.MultiSelect = False
'lv.ContextMenu = mnuContextMenu 'Don`t need to add if done this way
lv.FullRowSelect = True 'Added this but, you don`t need it if you don`t want it
mnuItemHide.Text = "&Hide"
mnuItemExit.Text = "&Exit"
mnuContextMenu.MenuItems.Add(mnuItemHide)
mnuContextMenu.MenuItems.Add(mnuItemExit)
AddHandler mnuItemHide.Click, AddressOf Me.menuItem1_Click
End Sub
Private Sub lv_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lv.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
If lv.GetItemAt(e.X, e.Y) IsNot Nothing Then
lv.GetItemAt(e.X, e.Y).Selected = True
mnuContextMenu.Show(lv, New Point(e.X, e.Y))
mnuItemExit.Visible = True
mnuItemHide.Visible = True
End If
End If
End Sub
Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txtValue as String
txtValue = lv.FocusedItem.SubItems(2).Text
Kill(txtValue)
Dim txtValue1 As String
txtValue1 = lv.FocusedItem.SubItems(0).Text
MessageBox.Show(txtValue1 + " has been hidden")
End Sub
End Class
this is my code
To hide your form -> form1.visible=false
To hide your form from taskbar -> form1.ShowinTaskbar=false
then go to the form1 keydown event and put this...
If e.Control And e.KeyCode = Keys.Q Then ' ---> activate with Ctrl-Q
form1.visible=true
End If