Maybe be a stupid question but say i have an Winform App with 2 clickable buttons (button_1 and button_2), each containing a piece of code. If I open the app, I want to click button_1 then button_2, and close the application. What i am wandering is there any way to insert a method that will call button_1 then button_2 on load (and possibly close the app?) that could be inserted into say Form1_Load?
Don't "click the buttons" automatically. Invoke the logical actions which the buttons invoke automatically. If that logic is currently in the buttons' click events in the code-behind, refactor it into a common location.
For example, your click event might end up with something like:
protected void Button1_Click()
{
SomeObject.DoSomething();
}
Then you can invoke the same action from the form's load event:
protected void Form_Load()
{
SomeObject.DoSomething();
}
Following that, still in the form load event, you can then close the application as well:
protected void Form_Load()
{
SomeObject.DoSomething();
Application.Exit();
}
Though it seems really unnecessary to load up an entire form just to do something without user interaction and then exit. A console application would be much simpler:
static void Main()
{
SomeObject.DoSomething();
}
Then you don't have a UI to worry about, you don't have to attach code to events, you don't even have to forcibly close the application. It'll just execute the code and exit.
Related
In my app project I use this to display my splash screen image:
<SplashScreen Include="Images\SplashScreen.png" />
I also use a Mutex to prevent the application being opened more than once. However, even with the Mutex check in the App class constructor the splash screen is displayed before the Mutex check is performed.
Is there some way I can perform the check before the splash screen is displayed to prevent the user seeing it on the second instance?
You can define and show the splash screen programatically, which allows you to control when it is shown and what happens before and after. Add your image with BuildAction set to Resource.
public partial class App : Application
{
protected override void OnStartup(object sender, StartupEventArgs e)
{
// ...execute pre action here.
SplashScreen splashScreen = new SplashScreen(#"Images\SplashScreen.png");
splashScreen.Show(true);
base.OnStartup(e);
// ...other code and post action.
}
}
See the SplashScreen type for more info and please be aware that there two overloads for the Show method. The parameter determines, whether the spalsh screen is automatically closed after the application was loaded. You could pass false and use the Close method to determine yourself when it is closed and execute your action afterwards.
I have created a windows form hosted control. And I want to change focus on button click of Hosted Control to another application inside Unified Service Desk.
On Button Click event I am using below code,
private void button1_Click(object sender, EventArgs e)
{
FireRequestAction(new RequestActionEventArgs("CRM Global Manager",
"ShowTab", "test application"));
}
but for some reason it is not getting fired. If i perform any other operation inside this code block like for example displaying a messageBox it works fine. But unable to fire this action.
Never FireRequestAction. Instead, always FireEvent, and then define Event and Action Call records in the USD configuration (rather than in the Hosted Control code, as you have been attempting).
In this instance, call FireEvent and provide the event with a meaningful name. Next, write no further code. Proceed to the configuration. On your Hosted Control record, create a new Event record with an identical name. If applicable, relate it to your Configuration record. On this Event, add an Action Call to have the Global Manager do a ShowTab on your test application, and if applicable, relate this to your Configuration record as well.
Is there a way to do something like this?
Background:
I have a button click. When the button is clicked I try to show a "loading" message, run a bunch of code that does some UI work and then dismiss the "loading" message. It takes anywhere from a few seconds to 20 seconds usually. At the moment the loading message doesn't show at all and the UI freezes until the code in my button click is done.
I've read about Background Worker and dispatcher, but haven't been able to get it to work. I'm not sure if it's because the code in the button click calls all sorts of other code (including 3rd party stuff), but I haven't been able to get it to run correctly. It all still works, but it still freezes the UI and the loading message doesn't appear.
So, I am wondering if there is another way around this. Is it possible to set it up so that on the button click I only show the loading message and then a second or so later fire another event that executes my long running process? That way the UI will still freeze for some seconds, but at least it will show a "loading" message.
You should be able to do this with a Dispatcher.BeginInvoke call.
private void ButtonClick(object sender, EventArgs e)
{
ShowMyLoadingMessage(); // your code, not sure what it is
Dispatcher.BeginInvoke(() =>
{
CallMyLongRunningCode(); // again, not sure what this is
HideMyLoadingMessage();
}
}
I don't believe this is the best solution since it is running on the UI thread, but it should do what you are asking.
What is the long running code doing that takes 20 seconds?
I have a Win form application (VS 2010 / C#) and I'm trying to figure out how to refresh pages without a refresh button. Currently I can refresh a page (basically to reset the data bindings) with a refresh button containing code something like this (this.refresh() does not seem to work for some reason):
this.Hide();
AccountSettings AS = new AccountSettings();
AS.ShowDialog();
An example I have is a page with numerous settings including data grids with CellClick events. When I click a cell I can make changes to a database. I hit close to go back to the Settings page but the only way for me to see the changes are to refresh() the page via the button.
So the short of it is, is there any way to refresh a form page from another form page?
For instance, when I click the Save button or close the child window.
Maybe pass the original form as an argument to the second form:
Form2 frm2 = new Form2(this);
And in Form2:
Form1 frm1;
public Form2(Form1 frm1)
{
InitializeComponent();
this.frm1 = frm1;
}
And then have in Form2:
frm1.Update();
Refresh on winform controls repaints the control itself. I find it useful to create a method that just loads my controls with the proper data, and then call it as necessary. (Including Form load)
private void ResetData()
{
//code to update settings
}
If you are showing the form that is closing as a dialog also you can take advantage of that, and check the status of the dialog instead of just opening it.
Form2 dlg = new Form2();
if (dlg.ShowDialog == System.Windows.Forms.DialogResult.OK) {
//code that updates your data
ResetData();
}
If its not a dialog there are a few things you could do and how your application works would make one method better than others. Here is just one example.
If your changes are something you don't need access to data from the other window to update you can handle the closed event of the form you create.
Create a class level variable to hold the form that is opened, so that you can also remove the event handlers you create:
private Form2 frm;
To create an instance of the form, and add the close event handler:
frm = new Form2();
frm.FormClosed += OnForm2Closed;
The event handler method:
private void OnForm2Closed(object sender, FormClosedEventArgs e)
{
ResetData();
frm.FormClosed -= OnForm2Closed;
}
How do I prevent Multiple forms from opening?
I do .show on the form but the user can click the main form and the button again and another instance of form opens.
Two options, depending on what you need:
Use ShowDialog instead of Show, which will open a modal window. This is the obvious solution if you don't need your main form to be active while the child form is open.
Or keep track of the window you opened already in the main form and do nothing if it's already open. This will be needed if you want the user to be able to use the main form while the child form is already open, maybe to open other forms.
do something like:
SingleForm myform = null;
void ShowMyForm_Click(object sender, EventArgs e)
{ if (myform == null)
{
myform = new SingleForm();
}
myform.Show();
myform.BringToFront();
}
Force your form object to adhere to the singleton pattern
I prefer to use Generics and lazy loading to handle my forms. Since all of my forms inherit from a base class, I can use the same method to bring forms to the front, send them to the back, destroy them, start them, etc.
If you keep a form manager class that's responsible for managing any loaded forms, you can bring whatever form to the front that you want, or prevent specific forms from being able to come back unless certain criteria are met.
public void LoadForm<T>() where T : MyNameSpace.MyBaseForm
{
// Load all your code in this joint and just call it when you
// need a form. In here, you can determine if a copy of the form
// already exists and then bring it forward or not
}
Disable the main form until the child form goes away, or disable the button.
button_onClick(object Sender, EventArgs e)
{
Button btn = sender as Button;
btn.Enabled = false;
Form myform = new MyForm();
myform.Show();
}
Of course, you really should be using form.ShowDialog() rather than form.Show() if you want modal behavior.