TestStack.White panel click - c#

I want to create an autologin WinForms app with TestStack.White. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
Process[] process = Process.GetProcessesByName("XYapp");
TestStack.White.Application app = TestStack.White.Application.Attach(process[0].Id);
TestStack.White.UIItems.WindowItems.Window window = app.GetWindow("XYwindowName", TestStack.White.Factory.InitializeOption.NoCache);
TestStack.White.UIItems.Panel panel = window.Get<TestStack.White.UIItems.Panel>(TestStack.White.UIItems.Finders.SearchCriteria.ByText("Login"));
panel.Click();
...
}
The XY app main window has 10+ panels and one of them is the "Login" panel. When I click the button1, the XY app "Login" panel will be visible, so it works.
But my WinForms app freezes and I get the control back when I close the XY program but I want to run further.
In debug mode, the following error message is displayed:
"Managed Debugging Assistant 'DisconnectedContext' : 'Transition into COM context 0x15305d0 for this RuntimeCallableWrapper failed..."
I think I understand why I get this message, but I have no idea what the solution is.
Someone could help me?
thanks

I'm sorry for the stupid question, the solution is simply just make a new thread.
https://learn.microsoft.com/en-us/windows/desktop/winauto/uiauto-threading

Related

DisplayPromptAsync always displaying on main window

I'm studying to become a developer; as a formative project, I'm working on a desktop app to help me organize local amateur chess tournaments with MAUI. Basically, I want to be able to open multiple windows during the same execution, so that I can run various parallel tournaments on the same machine.
On the MainPage I placed a button that creates a new window that displays the NewTournamentPage
private void NewTournamentButton_Clicked(object sender, EventArgs e)
{
Application.Current.OpenWindow(new Window()
{
Page = new NewTournamentPage()
}) ;
}
in the NewTournamentPage I placed a button to add a new player to the tournament, and I want the window to freeze until the user inserts the player name, but without blocking the execution of the other windows. DisplayPromptAsync seems to be exactly what I'm looking for, so I did this:
public async void AddNewPlayer_Clicked(object sender, EventArgs e)
{
string newPlayerName = await this.DisplayPromptAsync("Add new Player", "Name:");
//code to add player to tournament
...
}
When I execute and click the NewTournamentButton multiple times, the windows are created with no problems, and they all work independently from each other, but when I press the AddNewPlayer button, the popup pops on the MainPage window, not on the one calling the method, and it takes and saves the input in newPlayerName; then, the same thing happens on all secondary windows, in order of creation, without updating newPlayerName. What is happening here? What am I missing?
Yes, it is just the case as you said. And I have created a new issue about this problem on github.
You can follow it up here: https://github.com/dotnet/maui/issues/7650.
Thanks for your feedback and support for maui very much.

Problem when running multiple instances of the same form

Here's the situation. I've been developing a serial communication application and I needed multiple instances of my main form to show up when I click on a ListView item. Everything was running fine before I added this code:
private void ListView_DeviceList_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = ListView_DeviceList.HitTest(e.Location);
if (hit.Item != null)
{
Form m = new Form1();
m.Show();
}
}
After I tried to run it and double clicked in my ListView new instance of my form opened up as expected. The problem was they were glued together and I couldn't see the first one. After I closed it and tried to run it again it didn't show up. I could see the application running in the task manager and there was the icon in the windows taskbar but no UI opened up... Sorry this might be really unclear but I don't know how to describe it differently.

Detect if WPF application is running or in Visual Studio windows visualizer

I have a WPF application which opens a popup when the main window is loaded. The problem is when I select the .xaml file in the solution explorer in Visual Studio 2013, the popup "pops" even when the application is not running. I suppose it is an intended behavior since the visualizer needs to execute the code in order to render the page layout, but for now I need to close it every time I load the page... I cannot temporarily disable this popup since it has some start logic for the application (selection of a location,...).
Here is the code of the popup trigger
public GeneralProcess() //usercontrol
{
InitializeComponent();
Loaded += GeneralProcess_Loaded;
}
void GeneralProcess_Loaded(object sender, RoutedEventArgs e)
{
var popup = new StationSelect();
popup.Owner = Window.GetWindow(this);
popup.ShowDialog();
}
Is there a way to know if the application is running or if I am in the visualizer, or is there a way to disable the Loadedevent just for visual studio ? The goal is to still be able to see the page for easy editing.
EDIT : this question is a duplicate. However this answer worked for me.
void GeneralProcess_Loaded(object sender, RoutedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(this))
return;
var popup = new StationSelect();
popup.Owner = Window.GetWindow(this);
popup.ShowDialog();
}

WinForm freezes while opening a UserControl

I've a WinForm that contains Navigation menu that display UserControls. Now everything works fine except the form freezes when I try to open a Devexpress UserControl from another UserControl.
Here is the code I used:
private void btnOpenUserControl2_Click(object sender, EventArgs e)
{
UserControl2 uc2 = new UserControl2(ID);
this.Parent.Controls.Add(uc2);
uc2.Dock = DockStyle.Fill;
this.Hide();
uc2.Show();
}
What could be the possible reason?
The solution to this problem is to use the GridLookupEdit control in server mode instead. In this case, the gridLookupEdit will load only a small portion of data from the database and will continue loading data when you scroll the grid. To learn how to adjust the gridLookupEdit in server mode, please refer to Server Mode topic.
the form freezes is most likely one of two things:
in your UserControl2 you hndle an event ( for example form_Load), and in that event you have an infinity loop. the other reason is you have some handeled exception, or a long flow, very long flow, that cause your program to ran slow, so slow you think it's stuck.
in anyway it be very helpful if you share your UserControl2 code. but easier will be if you just pause the program while debugging it in VS when it get stuck and see where is it stucked. from there it should be fairly easy

Outlook New Mail Window opened from C# seems to have focus but my app still has it

I'm having a problem that I've been trying to solve for days now, but without luck!
On my Windows Forms Application I have a grid. One column contains an email address. When the user double clicks this column, I want to open a new E-Mail Window via Outlook automation. This window should have the focus and allow the user to type immediately.
Everything works fine, when:
I'm running my app from Visual Studio.
Or my app has the focus.
However, when I run my .exe and outlook has the focus when I double click the column, the following happens:
The new Mail window opens as expected
The cursor blinks in the new mail window (as expected)
when the user starts typing, the cursor still blinks in outlook but the typed text appears in the grid of my application, not in outlook.
I was able to reproduce the problem with a simple form that has a textbox on it.
I use the following code:
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
OpenOutlookMail(textBox1.Text);
}
private void OpenOutlookMail(string to)
{
MailItem item = OutlookApp.CreateItem(OlItemType.olMailItem) as MailItem;
item.To = to;
item.Subject = string.Empty;
item.Body = string.Empty;
item.Display();
}
protected Application OutlookApp
{
get
{
if (mOutlookApp == null)
{
mOutlookApp = new Application();
}
return mOutlookApp;
}
}
What i already tried was to
Activate my current form via this.Activate() before the call to OpenOutlookMail
Activate the MailItem Inspector Object
Activate the ActiveWindow and ActiveExplorer of Outlook via Automation
Using AutoIt as explained here Similar Problem with MS Word on the MSDN Forum
Any help would be appreciated!
I wrote about focusing a background window some time ago:
http://blog.sebastianbrand.com/2010/02/activate-form-in-background.html
private void label1_Click(object sender, EventArgs e)
{
// mainform.BringToFront(); // doesn't work
BeginInvoke(new VoidHandler(OtherFormToFront));
}
delegate void VoidHandler();
private void OtherFormToFront()
{
mainform.BringToFront(); // works
}
If you do have an handle of the bad window, give that a try.
You can try to use Dispatcher.BeginInvoke(...) with some low priority in your textBox1_MouseDoubleClick(...) method to call OpenOutlookMail(). It often helps to workaround focus management issues like this one.
I haven't been able to reproduce the problem with your code. I've used Microsoft.Office.Interop.Outlook version 14.0.0.0 and in every tests i've done the mail window get the focus.
As you state,
Everything works fine, when:
•I'm running my app from Visual Studio.
•Or my app has the focus.
Maybe trying to focus your form and/or making your application sleep before opening the mail window would work
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Focus();
System.Threading.Thread.Sleep(500);
OpenOutlookMail(textBox1.Text);
}
Interops often have weird behaviors. :s

Categories

Resources