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

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.

Related

How to transfer the binded data in the Main window to another window in WPF? [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 2 years ago.
Improve this question
I have a window that displays a list of Employees. When I select one Employee and click on a button, another window should pop up displaying the details of the selected Employee. But since the new window creates a new instance of my View Model, it does not know which Employee I clicked. I know how to display the details in the same window using a Datagrid or listview but now I am trying to learn navigation in WPF and I really want to know how data is transferred in WPF between different windows. Right now I am using a single View Model for both the windows.
Inside the constructor of both the windows, I wrote this.DataContext = new EmployeeViewModel();
There are two patterns with MVVM. View first and VM first. You are using View first and are finding the issues with it.
VM first is more common and means that the VMs are in charge, and windows and views are just created to wrap the VMs. See my previous answer to see how to open a new Window for a VM in a VM first world.

why does showactivated not work (wpf) [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 8 years ago.
Improve this question
My project has a button which when pressed executes the following code:
dim mywin as New MyDialogWindow()
mywin.showactivated = false
mywin.show()
My dialog window pops up, although showactivated was set to false.
I looked into Microsoft help and found:
Dim window As New AWindow()
window.ShowActivated = False
window.Show()
My code seems to be the same as Microsoft's code.
Does somebody know what's wrong with showactivated?
EDIT:
I posed this question with code (above) AND behaviour (pop up of my window), which I thought was strange because I erroneously believed that "showactivated" means the window is put into view. Meanwhile I was told the true meaning of showactivated and my question was answered.
ShowActivated does not hide the Window; it will prevent the Window from receiving focus.
From the MSDN:
When a window with its ShowActivated property set to false is opened,
the window is not activated and its Activated event is not raised
until a user manually activates the window by selecting it. After the
window is selected, it activates and deactivates normally.
So the Activated event is not raised. The Activated event causes a window to become the foreground window and receive focus.

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).

Screen-scraping a windows application in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I need to scrape data from a windows application to run a query in another program. Does anyone know of a good starting point for me to do this in .NET?
You may want to look into the WM_GETTEXT message. This can be used to read text from other windows -- it's an archaic part of the Windows API, and if you're in C#, you'll need to p/invoke for it.
Check out this page for an example of doing this in C#.
Basically, you first FindControlEx() to get the handle of the window that you want (by caption).
Second, you recursively enumerate the controls on that window with EnumChildWindows() to find all of the window's child controls, and all of those children's children until you have a complete map of the target form.
Here is a selected portion of Theta-ga's excellent explanation from Google Answers:
To get the contents of any textbox or listbox control, all we need is it's window handle. If you have already obtained the window handle then move to part 2 of the explaination.
PART 1: Obtaining the control handle
To obtain the handle of a control, we first obtain the handle of it?s parent window. We can do this by using the Win32 FindControlEx() method. This method takes in the window caption (such as 'Calculator') and/or its class name, and return its handle.
Once we have the parent window handle, we can call the Win32 EnumChildWindows method. This method takes in a callback method, which it calls with the handle of every child control it finds for the specified parent. For eg., if we call this method with the handle of the Calculator window, it will call the callback method with the handle of the textbox control, and then again with the handles of each of the buttons on the Calculator window, and so on.
Since we are only interested in the handle of the textbox control, we can check the class of the window in the callback method. The Win32 method GetClassName() can be used for this. This method takes in a window handle and provides us with a string containing the class name. So a textbox belongs to the ?Edit? class, a listbox to the 'ListBox' class and so on. Once you have determined that you have the handle for the right control, you can read its contents.
PART 2: Reading the contents of a control
You can read in the contents of a control by using the Win32 SendMessage() function, and using it to pass the WM_GETTEXT message to the target control. This will give you the text content of the control. This method will work for a textbox, button, or static control.
However, the above approach will fail if you try to read the contents of a listbox. To get the contents of a listbox, we need to first use SendMessage() with the LB_GETCOUNT message to get the count of list items. Then we need to call SendMessage() with the LB_GETTEXT message for each item in the list.

Categories