Change text of selected textbox on button click - windows

I've got a problem that involves, on a button click event, changing the text of the currently selected textbox on the form. Is this possible? If so how do I go about doing it?
EDIT: I do not know the name of the textbox, but it will always be the currently selected or 'focused' textbox.

The main problem is that when you press the button, the Textbox is no longer the focus.
By solution appears here, You can do this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If lastTextBoxFocused IsNot Nothing Then
lastTextBoxFocused.Text = "Bla bla, bla!"
End If
End Sub
Dim lastTextBoxFocused As TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'find all TextBox's in the Form.
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is TextBox Then
'attach a lambda expression to each Enter event, to "remember" the last enter
AddHandler CType(Ctrl, TextBox).Enter, Sub(o, ev) lastTextBoxFocused = o
End If
Next
End Sub

Related

Form1.button2.text.. is not accessible in this context because its private

What I would like to do is after clicking the button14 which is login it would change all the button in all forms text to "sample" I already change the class where the button2 is from private to public but I still get an error
Public Sub button14_Click(sender As Object, e As EventArgs) Handles button14.Click
If textBox2.Text = "sample" And textBox3.Text = "****" Then
Form1.Show()
Me.Hide()
button2.Text = ("sample")
Form1.button2.Text = ("sample")
Else
MsgBox("Sorry, Username or password not found", MsgBoxStyle.OkOnly, "Invalid")
textBox2.Text = " "
textBox3.Text = " "
End If
End Sub
this is form 1
Public Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
Login.Show()
Me.Hide()
End Sub
Do people still write VB code? Anyway, the error message is quite clear. You need to make the button public (not just the class).
But, for security reasons, instead of making the button public...I would recommend you to expose a method to change the text of the button, after all, that's all you're doing. You can even mark this method with the Friend access modifier...I think it is the same as internal in C#?
My guess this is winform project.
You can change the access modifier of your button in the form designer. Select your button press F4 for the property grid, find Modifiers and change it to whatever.

How to I get (FindControl) the button of a GridAttachmentColumn in a RadGrid

I've got a RadGrid with a GridAttachmentColumn named "FileName". I'm trying to get (FindControl) the control out of the GridDataItem in the ItemCreated event. Specifically, I want the button control (or linkButton in this case). item.FindControl("FileName") always returns Nothing.
Protected Sub AttachmentsRadGrid_ItemCreated(sender As Object, e As GridItemEventArgs)
If TypeOf e.Item Is GridDataItem Then
Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
If item IsNot Nothing Then
Dim FileName = item.FindControl("FileName") 'Always Nothing
If FileName IsNot Nothing Then
'Do something with it
End If
End If
End If
End Sub
Dim button As LinkButton = TryCast(item("FileName").Controls(0), LinkButton)
OR
Dim FileName = item.FindControl("gac_FileName")
The first line of code might be Telerik's preference so I put that line first. Notice that the AttachmentColumn in read mode is basically just a linkbutton.
Notice in the 2nd example that "gac_" in item.FindControl("gac_FileName") is added to the front of the UniqueName of the column. I noticed it in Chrome DevTools when I inspected the element from the browser. I should note that "FileName" is the UniqueName of the column in case you didn't want to read through the code above.
Safer method, and Telrik's preferred method is to call the control by name rather than index...
Dim button As LinkButton = TryCast(item("FileName").Controls("gac_FileName"), LinkButton)

visual basic multiple toolstripmenuitems to perform the same on click

I am new here. So apologize in advance if not wording question properly.
I am developing an application (VS2013, Visual Basic) that using multiple menu items in the MenuStrip. When item is clicked the identical function is called - the Tab is created and appropriate form is loaded.
i.e.
Private Sub XXXMNU0039_Click(sender As Object, e As EventArgs) Handles XXXMNU0039.Click
Dim f1 As New frm_B05_01_SalesQuotes
Dim imgnm As String = "XXXMNU0039"
Call XXXTabPages("Sales Quotes", f1, imgnm)
End Sub
Private Sub XXXMNU0040_Click(sender As Object, e As EventArgs) Handles XXXMNU0040.Click
Dim f1 As New frm_B05_03_SalesQuotesReports
Dim imgnm As String = "XXXMNU_Reports"
Call XXXTabPages("Sales Quotes Reports", f1, imgnm)
End Sub
....
I am wondering is there is a way to create a "global" default "on click event" for all menu items that will accomplish the same thing by default. I have all the relevant information for each menu item stored in a table and hope to avoid creating "on click" for each item.
Thanks in advance.
You could have the same handler for the click event of your ToolStripMenuItems.
Just add, after the first Handles XXXMNU0039.Click the event to handle for another ToolStripMenuItem and so on
Oviously, then problem is how to differentiate the various ToolStripMenuItem that calls the same event handler. But in the event arguments there is the Sender object that represent the current ToolStripMenuItem that has called the event.
Just DirectCast to a ToolStripMenuItem and read its name to pass the correct parameter to the XXXTablPages method
Private Sub menuItemHandler_Click(sender As Object, e As EventArgs) _
Handles XXXMNU0039.Click, XXXMNU0040.Click
Dim f1 As New frm_B05_01_SalesQuotes
Dim itm = DirectCast(sender, ToolStripMenuItem)
Dim imgnm As String = item.Name
Call XXXTabPages("Sales Quotes", f1, imgnm)
End Sub

Add/remove items from array that goes into combobox

This is what needs to be done.
This project / assigenment will maintain a list of movie titles in an array. The user can add and remove movie titles from the list, clear and print the list.
The form will contain a combo box initialized with the following entries: A Dependent's Pay, An American Resident, Dancing With Fools, Home By Yourself, and Ghost Man.
There will be six buttons on the form. An add button that will add an entry to the list using an input box. The user may not add a duplicate title to the list. A remove button that will remove the selected item off the list. An error is displayed if the user tries to add a blank title or remove a title without first selecting one. A clear all button will remove all the entries from the list. A count button will display in a message box the number of movie titles can be displayed. The print button will display the ‘Movie List’ report in a print preview window. Include your name on the report. And finally, an exit button.
This is what I have so far:
Public Class frmlist
Dim movielist(100) As String
Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub frmlist_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
movielist(0) = "A Dependent's Pay"
movielist(1) = "An American Resident"
movielist(2) = "Dancing With Fools"
movielist(3) = "Home By Yourself"
movielist(4) = "Ghost Man"
cboxlist.Items.AddRange(movielist)
End Sub
Private Sub AddToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AddToolStripMenuItem.Click
movielist(5) = InputBox("Enter a movie title: ", "Movies")
cboxlist.Items.AddRange(movielist)
End Sub
End Class

ModalPopupExtender + ASP.NET AJAX: Can't page grid

I'm trying to page and sort my DataGrid which is inside a ModalPopupExtender but I can't page it in any way, already tried with <Triggers>, put the UpdatePanel inside, outside, in the middle, and it does NOT work. Modal popup does not get closed but the grid just disappears. Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
SqlServerDS.SelectCommand = "SELECT * FROM emp WHERE name LIKE '%" & txtSearchName.Text & "%'"
BindData()
End Sub
Private Sub BindData()
grdSearch.DataSource = SqlServerDS
grdSearch.DataBind()
End Sub
Private Sub grdBuscaPaciente_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdSearch.PageIndexChanging
grdSearch.PageIndex = e.NewPageIndex
BindData()
End Sub
Inside the Designer, this is the code h:
<modalpopupextender>
</modalpopupextender>
<panel>
<updatepanel>
<gridview>
</gridview>
</updatepanel>
</panel>
Tommy is right, so what you just have to do is re-show the popup.
After the BindData() in the PageIndexChanging event show the panel again with the Show() method of the popup extender.
This code is in c# but is pretty much the same.
gvHorarioPB.DataSource = (DataTable)Session["Prueba"];
gvHorarioPB.PageIndex = e.NewPageIndex;
gvHorarioPB.DataBind();
//mpePB is my modalpopupextender
this.mpePB.Show();
If you are using the .NET AJAX toolkit, keep in mind that each time you click someting (paging, sorting, etc.), the page performs a postback, even if it looks AJAX-y. Meaning that you will need to rebind the data each time. Try removing the IfPostback in your page load and see what that does for you.

Resources