Event on a File Dialog c# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working in C# VS 2012. I want to be able to have an event for a FileDialog that once the user chooses a file some code is run. So it would be something like once the filedialog is closed the code will run. If anyone can lend any help that would be great.

Well, by default, showing the dialog is modal, i.e., your thread is effectively halted until the dialog is closed. So, just Show() it and any code after that call will be run after the window closes. You can get the chosen file(s) via the FileName property (or FileNames property if MultiSelect is set to true).

This is for WinForms:
using (OpenFileDialog dialog = new OpenFileDialog()) {
if (DialogResult.OK == dialog.ShowDialog()) {
// work with dialog.FileName
}
}

The FileDialog's ShowDialog method is blocking. This means that the thread it's executed and shown on will stop executing until the file has been returned. You can use the result to check if a file was selected.

This is the WPF way:
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
... your code here
}
More info: http://msdn.microsoft.com/en-us/library/cc221415(v=vs.95).aspx

Related

ShowDialog() displays no data but Show() does [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
Maybe this can't be answered without context but when I use Show():
TestView test = new TestView()
{
Owner = Application.Current.MainWindow
};
test.Show();
Everything works fine, the events are fired and my textbox/comboboxes are populated with data on the test view. But when I use ShowDialog() nothing is populated. Anyone know a reason why? I want a modal window.
This is by design.
As by documentation:
When a Window class is instantiated, it is not visible by default. ShowDialog shows the window, disables all other windows in the application, and returns only when the window is closed. This type of window is known as a modal window.
Modal windows are primarily used as dialog boxes. A dialog box is a special type of window that applications use to interact with users to complete tasks, such as opening files or printing documents. Dialog boxes commonly allow users to accept or cancel the task for which they were shown before the dialog box is closed.
Thus, the ShowDialog is blocking until the window is closed.
This means that dealing with events and similar, specifically from the Owner window will not work.
If you have additional questions, please show the relevant code with the events and how you populate them.
If for some reason you need a full blocking window - it is possible; but we will need more details.

Programatically Shows a Pop Yes No Pop up inside a code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Good Day Every one,
Can you help me produce a code to produce pop up window like return confirm? the thing is it should be inside a method not after a click.
if (a == "1")
{
//Page.ClientScript.RegisterStartupScript(this.GetType(), "messagebox", "<script>$(document).ready( function() { csscody.confirm('<br/><h1> Confirmation</h1><br/> We have currently existing Details for that Upload Proceed?<br/>',{onComplete: function(e){if(e){process();__doPostBack('ctl00$ContentPlaceHolder1$btnCommitSaving','');}}});return false;});</script>", false);
//ClientScript.RegisterStartupScript(typeof(string), "messagebox", "<script language='javascript'>alert('Continue Uploading?.');</script>");
// here a pop up message should appear if he press yes he will do the procedure if no the program will cancel the procedure
// the other one is just an alert with OK button not having Yes or no
}
i tried the code with // but its not working it just passes by it in debug mode.
im sorry but i have a very minimal understanding about javascripts. so please help me any reading materials for my future reference would be ideal but a code with explanation would be better Thank you so much!
**Edit
Honestly Speaking i really do not know why it is not working in debug mode it just passes in that statement without any pop up box that appears, im really sorry if i do not know about javascripts that's why im asking help to enlighten me, also to find a way around about this problem.
ClientScript.RegisterStartupScript
you should only do it on if you want to add the script on page first load and not after you postback and you can use javascript confirm
Page.ClientScript.RegisterStartupScript(this.GetType(),
"ButtonClickScript", "confirm('Please select date within the same month and year')", true);

How to get the listview loaded in the form only after the button is clicked? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
In my project, I need to get the listview containing the data from the back end, If I place the listview in the form and upon clicking a button I can load data, but the requirement is initially the listview should be hidden or not present there, only on clicking the button the listview should be viewed with data. Thank you.
Set the List View's property Visible = Falsein the properity window.
Within the button click event handler set it to true;
listView1.Visible = true;
You use the Visible Property. At the form load, you set it to false and then in the button_click
private void button1_Click(object sender, EventArgs e)
{
listView1.Visible = true;
}
I think You should use listView1.Hide(); initially in Form constructor
And on button click You should use listView1.show()

How do GroupBox's and RadioButton's work together? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could anyone explain me how the compiler knows to perform appropriate method when we select RadioButtons in this example ?
It's hard to say for sure what you're asking. I think you're asking how the system knows that it should execute the iconType_CheckChanged method when one of the icon radio buttons is clicked, and how it knows that, for example, the asteriskRadioButton changed.
The answer is in two parts. First, in creating the program in Windows Forms, you hooked up the CheckChanged event handler for each of the radio buttons. So the asteriskRadioButton CheckChanged method contains the value iconType_CheckChanged. That information is added to the partial class that you don't usually see. It's in your Form.Designer.cs file in the InitializeComponent method. It looks something like:
this.asteriskRadioButton.CheckChanged += iconType_CheckChanged
You don't typically see the Form.Designer.cs file. To view it, expand the form node in the Visual Studio Solution Explorer and you'll see the file listed:
The second part of the answer is that when you click the radio button (or when some code changes the state of the radio button), the underlying control machinery calls iconType_CheckChanged, passing a reference to the control that triggered the event in the Sender argument.

How to cancel properly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm going to create a scenario before I ask my question.
Say you have a form with a start button, then when the user presses it a message box pops up asking if they want to continue and the options are yes or no.
When the user presses no the program should stop what its doing, return to the form and clear the text boxes/combo boxes/etc.
Now here's my question, upon selecting "no" how does one go about stopping the program from going into further code? For the code to stop what it's doing and return to the form like nothing happened?
I hope I explained myself well enough...
Nevermind everyone I realised where I went wrong... Crisis averted. Thanks for all of your input however!
Normally you'll use a Modal form, means that your current method is blocked and will be blocked until the modal form returns.
You can try something like this:
DialogResult result = MessageBox.Show("Title", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
return;
If you want a custom form to show as modal, you should use ShowDialog() instead of Show(), ShowDialog will block the current execution of the executing method and will setup a temporary messageloop for handling window messages.
like:
using(FormQuestion form = new FormQuestion())
{
DialogResult result = form.ShowDialog();
// check the result.
}
Check here for more information: Displaying Modal and Modeless Windows Forms http://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
In general, .NET uses a cooperative cancellation model, so methods need to have a mechanism to notify that a cancellation is requested, and they need to explicitly cancel themselves.
This is handled by many portions of the framework via the CancellationTokenSource class and the CancellationToken struct.
For full details, see Cancellation in Managed Threads on MSDN.
That being said, if the method in question is using the MessageBox directly, it can just return or stop working if the result is DialogResult.No (in Windows Forms) or Window.DialogResult is unset or false (in WPF).

Categories