הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, July 2, 2011 4:36 AM
Can I change the 'OK' text on button to 'Close' in VB.Net?
Please give some suggestion.
All replies (13)
Saturday, July 2, 2011 4:55 AM ✅Answered | 1 vote
no, you cannot edit the text on the buttons of a messagebox
you can either set the Text to something like "Do you want to close...?", in which case the OK would make sense (or better yet use the Yes and No buttons)
or, you can create your own MessageBox using another Windows Form
Saturday, July 2, 2011 7:23 AM ✅Answered | 1 vote
That is only possible as you create your own messagebox.
Be aware it is just a form, which is predefined in the Windows OS.
A form which is showed with showdialog does exactly the same.
Success
Cor
Saturday, July 2, 2011 8:24 PM ✅Answered | 1 vote
This is what I had in mind. I created a little test program with two forms - Form1 and a form called ConfirmClose.vb. The second form has a picturebox, a textbox, and two buttons (all left to their default names here):
In Form1, I have the following code:
Private Sub btn_TestNewDialogue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_TestNewDialogue.Click
' Button on Form1 to test the new "ConfirmClose" form as a dialogue (modal):
Dim confirm As New ConfirmClose
confirm.imageToDisplay = My.Resources.Mini_Breakfast_Icon
confirm.textToShow = "Are you certain that you want to close this program?" & vbCrLf & vbCrLf & _
"There are still processes running which will be aborted and will have to be done all over again if you choose to exit this program."
confirm.button1Text = "Yes" & vbCrLf & "Close This Program"
confirm.button2Text = "Nevermind" & vbCrLf & "I Had A Senior Moment"
confirm.ShowDialog()
' This is just so that you can see what the user
' chose in that dialogue:
If confirm.userButtonClickChoice.Contains("Yes") Then
MessageBox.Show("They chose to close the program.")
ElseIf confirm.userButtonClickChoice.Contains("Nevermind") Then
MessageBox.Show("The chose to continue running the program.")
Else
' They closed it without answering
MessageBox.Show("Another idiot at the wheel...")
End If
End Sub
The result looks like this:
Anyway ... that's my $0.02 worth.
:)
Saturday, July 2, 2011 8:27 PM ✅Answered | 1 vote
DOH!
I forgot to show the second form's code:
Public Class ConfirmClose
' What we'll pass in:
Public imageToDisplay As Image = Nothing
Public textToShow As String = ""
Public button1Text As String = ""
Public button2Text As String = ""
' What we'll pass back:
Public userButtonClickChoice As String = ""
Private Sub ConfirmClose_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = button1Text
Button2.Text = button2Text
' Make sure that you make button 1 the equivalent of "OK" and
' that you make button 2 the equivalent of "Cancel".
With TextBox1
.TabStop = False
.BorderStyle = BorderStyle.None
End With
If imageToDisplay IsNot Nothing Then
PictureBox1.Image = imageToDisplay
Else
PictureBox1.Image = Nothing
PictureBox1.Visible = False
End If
If textToShow IsNot Nothing AndAlso textToShow IsNot String.Empty Then
TextBox1.Text = textToShow
Else
TextBox1.Visible = False
End If
Button1.Focus()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Button 1 clicked
userButtonClickChoice = Button1.Text
Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Button 2 clicked
userButtonClickChoice = Button2.Text
Close()
End Sub
End Class
Saturday, July 2, 2011 6:57 PM
Jordan,
You might want to re-think how you're calling Form2 from Form1.
Saturday, July 2, 2011 7:27 PM | 1 vote
Jordan,
You might want to re-think how you're calling Form2 from Form1.
Hi Frank,
What exactly do you mean? ShowDialog() acts perfectly for making it a "message box" where form1 is unable to be used until the user completes Form2 (the message box).
To my knowledge, Show() is used to "open" a form where both forms can be used by focusing on a form. ShowDialog() works well for making the form act as a message box by forcing the user to complete whatever is needed to be done in the form.
You can use Show() but it isn't as professional when using the form as a message box.
Also, I'm not sure if you were meaning that it doesn't call correctly or load properly, as I just tested it on my end and it works fine.
If I said something inaccurate do correct me as I'd love to learn.
- Jordan
No, I didn't mean showing it modally with .ShowDialog, I meant that it's not in a method to dispose. As an example:
Dim messageToShow As New Form2
messageToShow.ShowDialoge()
The "OO proper" way is to use a constructor though, but I honestly think that's overkill :-o
Saturday, July 2, 2011 7:48 PM
Also when you meant disposing, the button on form2 would dispose that with Me.Close()?
No I think we're confused on terms here.
When you "create" a new variable (declare it) for instance, depending on how you give it scope (where you declare it), then once it's returned from the method (no longer in scope) then it is available to be disposed (eliminated - it no longer exists as far as the program is concerned).
Some while back, Tom explained how strings are a weird object really and don't necessarily obey this rule - at least not in actual compiled code - but for the most part, my statement is true.
In this case, we're talking about the instantiation of a form.
I was just thinking of something regarding this thread. Give me an hour or so and I'll post it. I've never tried it before, but I think it'll work. ;-)
Saturday, July 2, 2011 8:02 PM
Thank you Frank.
Although I'm confused on why it would be better to use that code versus mine? Why do you need to Dim it as that versus just using form2.showdialog()?
What is Form2? Isn't it a class in your project? Doesn't it take an Object, that is, an instantiated class for ShowDialog. The ability to do Form2.ShowDialog() in VB is an ugly feature of VB to OOP purists. It's called AutoInstantiation. VB Dims a New Form2 for you.
Saturday, July 2, 2011 8:11 PM | 2 votes
Jordan,
A form showed with ShowDialog is direct a modal form, it is one exceptions for which you can better as soon as possible do the method dispose to remove the unmanaged resources, but in this case you can simply use the using keyword instead of dim. That one does that removing of unmanaged resource intrinsic.
using myDialog as new DialogForm
myDialog.ShowDialog
End using
Success
Cor
Saturday, July 2, 2011 8:12 PM
VB Dims a New Form2 for you.
You're right John, now that I think of it - Deborah talked about that a while back when there were discussions about how one of the then upcoming .NET frameworks was being discussed.
Saturday, July 2, 2011 8:14 PM
Actually John, I think that VB creates a singleton default instance rather than creating a new instance as you wrote. If in code you later again used Form2.ShowDialog(), it reuses that same singleton instance.
--
Mike
Saturday, July 2, 2011 8:21 PM
Jordan,
A form showed with ShowDialog is direct a modal form, it is one exceptions for which you can better as soon as possible do the method dispose to remove the unmanaged resources, but in this case you can simply use the using keyword instead of dim. That one does that removing of unmanaged resource intrinsic.
using myDialog as new DialogForm
myDialog.ShowDialog
End using
Success
Cor
I never thought of a Using a clause for it - great idea Cor!
Saturday, July 2, 2011 8:39 PM | 1 vote
Actually John, I think that VB creates a singleton default instance rather than creating a new instance as you wrote. If in code you later again used Form2.ShowDialog(), it reuses that same singleton instance.
--
Mike
Yeh! That's really what it does. I guess I tried to simplify it too much. VB creates it once and it hangs around. Even more reason to avoid its use.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As EventArgs) Handles Button1.Click
Form2.ShowDialog()
Form2.Text = ""
End Sub
End Class
Class Form2
Inherits Form
Sub New()
Me.Text = "I have been auto-instantiated."
End Sub
End Class
Click the button twice.