Currently I am working with the ShowDialog() method and trying to figure out how it is supposed to work. I have a form, testDialog, that has a text box that will take an input string. I followed the code on the MSDN page as follows:
string Range;
testDialog specRange = new testDialog();
if (specRange.ShowDialog(this) == DialogResult.OK)
{
Range = specRange.txtPageRange.Text;
}
else
{
Range = "";
}
specRange.Dispose();
The thing I can't find any information on and that I can't figure out is, how do I enter the text and get it to submit? I put buttons on the form but they didn't show up when I ran the program. I enter the text into the text box but I can't hit enter or anything, my only option is to close the form.
Is there something I'm missing that I need to add so that I can hit enter or click an Okay button after entering the text?
From msdn :
The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. This value is then returned by this method. You can use this return value to determine how to process the actions that occurred in the dialog box. For example, if the dialog box was closed and returned the DialogResult.Cancel value through this method, you could prevent code following the call to ShowDialog from executing.
The simplest method to do that is to add a Button "Ok" to your testDialog and change its property DialogResult to Ok. So when you click on it, it will return DialogResult.ok and you will enter your if.
Related
I have a dialog box that I display over my main form (not an MDI)
I call it this way:
dlgPets dlgPet = new dlgPets();
dlgPet.Show();
Once on the dialog, the user will click a button and that is where i verify what was entered. If it fails, I just return
What happens is the dialog box goes away.
I have also tried adding:
Dialog result = dlgPet.ShowDialog();
if (result == DialogResult.OK)
{
return;
}
I need to stay on the dlgPet dialog box until the user clicks the cancel button.
Thanks in advance
Mark
In my opinion you should use
dlgPet.TopMost = true;
I have a form with 4 textboxes. This form is viewed inside a split container panel.
I can enter the values inside the text box but after the value, I click on the text I entered to modify a value but it is not letting me click, there is no cursor coming on the text box, the only thing I can do is backspace or select all and delete.
I cant click anywhere on the middle of the text entered.
If I check the focus I see it is false.
Can someone tell me what could be the problem and how to set the focus to the text box?
To show the form on the panel this is what I am doing
splitContainerControl1.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
splitContainerControl1.Panel2.Controls.Clear();
myform.TopLevel = false;
myform.FormBorderStyle = FormBorderStyle.SizableToolWindow;
splitContainerControl1.Panel2.Controls.Add(myform);
myform.Show();
I have tried the below, but not working, I see focus still false.
myform.Focus();
myform.textbox_latitude.Select();
Thanks,
Try using this.ActiveControl:
this.ActiveControl = textbox_latitude;
Or if you want to focus it on the child form:
myform.ActiveControl = textbox_latitude;
I have a mainform where you can open another window and change options. One of the options is to copy highlighted text to the clipboard. if the user doesn't highlight text and clicks btnCopy then I want a message to be shown that no text was highlighted. When the user selects 'ok' I want the messagebox to close but I want the 'options' window to stay open.
Right now when the user clicks 'ok' both the message box and 'options' window closes. Why is the 'options' window closing?
Here is my code:
private void btnCopy_Click(object sender, EventArgs e)
{
string copySearch = txtSavedSearches.SelectedText;
if (copySearch == "")
{
DialogResult dialog = MessageBox.Show("You did not select anything to copy. Please select the query to copy.", "Copy search", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Clipboard.SetText(copySearch);
this.Close();
}
}
You obviously set the DialogResult property of btnCopy to something different than DialogResult.None.
If - in a Form that is not the application's main window - a Button is clicked that has the DialogResult property set (to something different than None), this click causes the Form to close and the calling ShowDialog() method to return that DialogResult.
Find out where you set that property and remove it.
From MSDN (Button.DialogResult):
If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked
Why is the 'options' window closing?
The following line will cause the options form to close:
this.Close();
You don't need to do anything to close a MessageBox; it goes away by itself when the user clicks OK, and then your code resumes running from the point where MessageBox.Show was called. MessageBox.Show is a method that returns a value denoting which button the user clicked to get the box to go away (the value varies depending on a) which buttons you chose to show as part of the call to .Show(...) and b) which button the user clicked
Right now when the user clicks 'ok' both the message box and 'options' window closes. Why is the 'options' window closing?
This cannot be, as the message box is shown in the do-if-true part of the IF, and the call to close the options form is called in the ELSE (do if false) part. These two parts cannot run in succession, they must be one or the other. Either your option form closes without a messagebox showing, or a messagebox shows and your form doesn't close
-
Edit:
Renee believes you have set this property:
on your btnCopy button to be something other than None
And then you have also opened your options form like this:
OptionsForm f = new OptionsForm();
f.ShowDialog();
These two things combined will conspire to cause your form options to close any time that btnCopy is clicked (unless the clickevent is canceled)
My intention is to make a pop up window (using another form) and show it when the delete file event is triggered. On this pop up form user should enter a correct password and then click OK button, this pop up disappears and the deletion operation proceeds. What I did is setting a string variable "Result" in the pop up form(form 2). When clicking the OK button, if the password entered is correct the "Result" is set for example "true". In my main form I made like:
if(form2.Result=="true"){ // deletion operation}
However this method doesn't work. When I entered the correct password and click OK in form2, nothing happens in my main form. Anybody got any idea how could I in main form detect the button click event in form2? Something like "if(form2.button.click==true)".
Thank you
You are not supposed to detect a button click on the second form, you should restructure your flow.
There are two common ways to do this. The easiest for you to implement would be instead of calling form2.Show() which you are probably doing right now, you should call form2.ShowDialog() which will halt the execution of the method in form1 until form2 is closed
So in form1 you'd have something like
private void DeleteSomething() {
if (form2.ShowDialog() == DialogResult.Ok) {
if (form2.result == true)
doDelete();
}
}
Note that checking for the DialogResult is a common practice, and unless you assign the property on Form2 specifying which button will act as the "Ok" button, you need to manually set the dialog result inside form2 like Sayse mentioned in his reply with
this.DialogResult = DialogResult.OK;
In your case, using the dialog result is not strictly necessary, but it's a common practice to implement support for different dialog results (ok, cancel, etc)
The whole other method is to not treat form2 as a dialog, but have form1 pass a reference to itslef to form2
then in form2 you can call a method on form1 to "authorize" the delete
so form1 would have one private method "confirmDelete" which would just show form2
and another public method "doDelete" which would be called by form2 when needed.
Unrelated note on your code:
If you are going to use a variable to indicate weather the authentication was succsefull, don't use a string with the value of "true", use a boolean.
If you wanted to return 3 or more states (eg, Result could be "Ok" "User Canceled" "Incorrect password" "Inssuficcient privileges", etc) then use an Enumeration.
ShowDialog() method returns a DialogResult which you can set to indicate how the form exited
using(var form2 = MyFormName())
{
if(form2.ShowDialog() == DialogResult.OK)
//success -- You can access form2 properties also.
}
Inside your button_ok event on the form, set the DialogResult to OK
this.DialogResult = DialogResult.OK;
I'm currently making a simple C# game for a college assignment, wondering if it's possible to have radio or checkbox (or something similar) inputs upon loading, in an external window.
I'm currently using this to get a username:
public Form1()
{
InitializeComponent();
Initialize();
GlobalVar.Username = Microsoft.VisualBasic.Interaction.InputBox("Welcome to EasiGame, Please enter your username.", "Welcome", "Player1", -1, -1);
label4.Text = GlobalVar.Username;
}
This references VB, and that works great, however could I include radio buttons or something in this box, or a separate box, to grab user input for a difficulty setting.
Thanks in advance!
You would have implement your own form. Assuming you're using a recent version of Visual Studio:
Click Project > Add Windows Form.
Set the FormBorderStyle property of the Form control to FixedDialog to prevent
the user from resizing the form.
Set the MinimizeBox and MaximizeBox properties to false.
Set the StartPosition property to CenterScreen or CenterParent.
Add a button to the form with the text OK and set the DialogResult
property to OK.
Add a button to the form with the text Cancel and set the DialogResult
property to Cancel.
Change the AcceptButton property of the form to reference the OK button.
Change the CancelButton property of the form to reference the Cancel button.
Add a TextBox and a RadioButton or CheckBox control to the form.
Press the F7 key to open the code view for the form.
Implement a property of type string that returns the Text property of
the TextBox control.
You'll also need to come up with a way of getting the difficulty from the
form; I'll let you figure this part out for yourself :)
To show the form, create an instance and call the ShowDialog() method.
This will block the calling method until the user has clicked a button.
The ShowDialog method will return a DialogResult, which can be used to
determine which button was clicked.