Basically I have two have applications, using same database. I want to pop up a window(may be radwindow) on second application by clicking the button on first application.
what is the better way. I was thinking to update some value on click on first app, and put the timer on second application which after every time period checks the value, if updated , popup the window.
Any help will be appreciated.
Thanks
you can save the pointer of the second application in your first application,when clicking the button on your first application,you can check the Visible property of the second application,and change the value of Visible property.Just like:
Form secondForm;//create a global var to save the pointer of the second application
private void Button1_Click(object sender, EventArgs e)
{
if (secondForm == null || secondForm.IsDisposed)
{
secondForm = new Form2();
secondForm.Visible = true;
}
else
{
secondForm.Visible = true;
}
}
Related
I have a button that loads a Form, as it gets lots of data from a database and takes a few seconds, I want to advise the user to wait.
When I click the button the button text does not change.
This is the button Click code I am using:
private void btnItemConfigForm_Click(object sender, EventArgs e)
{
var itemConfigBtnText = btnItemConfigForm.Text;
btnItemConfigForm.Text = "Waiting...";
ItemConfigForm form = new ItemConfigForm();
form.Show();
if (form.Created)
{
btnItemConfigForm.Text = itemConfigBtnText;
}
}
If I Comment out
if (form.Created)
{
btnItemConfigForm.Text = itemConfigBtnText;
}
Then the button text changes to waiting after the new form window is visible.
What am I missing to get the button text to change before the form window is visible.
the simple solution is to add this row:
btnItemConfigForm.Refresh();
after this row
btnItemConfigForm.Text = "Waiting...";
Otherwise the text of the button will be changed only when the function ends, this function will redraw the form display!
P.s.
If you want the form will not be blocked - you can use in asynchronic running to the function "Show" (or New) then you will need Event to notify the first form when the form will be loaded
sorry for my English... :)
Added
btnItemConfigForm.Update();
under
var itemConfigBtnText = btnItemConfigForm.Text;
btnItemConfigForm.Text = "Waiting...";
This updates the button Control before moving on to initialising and showing the form.
Since I don't have much reputation to post image, I am elaborating as a big question.
I have three windows forms in which the execution of events get increased each time the form is created.
1. Form1(MainForm)
Here I call the second form(SubForm) which contains a user control.
2. Form2(SubForm)
Here I call the second form(ChildForm) by clicking the user control.
3. Form3(ChildForm)
It contains an OK button.
Here is my problem.
First I open MainForm and click the button to open second form(SubForm).
Now in second form(SubForm) I click the user control which shows third form(ChildForm).
When I click OK button in the third form, the form gets closed.
Now I close the second form(SubForm) without closing first form(Main form) and click the button in the first form(MainForm) to open second form(SubForm) again.
Now I click user control in the second form(SubForm) and the third form(ChildForm) is opened.
Now, when I click OK in the third form(ChildForm), the event in the second Form(SubForm) is fired once again and third form(ChildForm) gets opened four times.
Now when I close the second form(SubForm) again and clicks the user control and take third form(ChildForm), the third form(ChildForm) gets opened three times etc etc.
Here is the code in first form(MainForm)
private void button1_Click(object sender, EventArgs e)
{
SubForm obj = new SubForm ();
obj.ShowDialog();
}
Here is the code in second form(SubForm) which contains user control
// Event generation
UserControl1.MouseUp += new EventHandler(this.Node_Click);
// Event that calls the ChildForm
private void Node_Click(object sender, EventArgs e)
{
ChildForm obj = new ChildForm();
obj.ShowDialog();
}
Here is the code in first form(MainForm)
private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}
Anyone knows why this is happening?
Hey I am not really sure what's happening but if you want the node_click to be applied only once just declare a bool var canEnter=true;
and do this
private void Node_Click(object sender, EventArgs e) {
if(canEnter) {
ChildForm obj = new ChildForm();
obj.ShowDialog();
canEnter=false;
}
}
and make event for the child form (closed event) and place in it
canEnter=true;
if I got you right I guess that's what you need to do :)
Since you are calling UserControl1.MouseUp, I think you have declared the event as static. So even though you close the second form(keeping first form unclosed), the event doesn't get cleared. For that you need to Unsubscribe the event.
Use Form_Closing event in the second form to Unsubscribe the event.
private void SubForm_FormClosing(object sender, FormClosingEventArgs e)
{
UserControl1.MouseUp -= new EventHandler(this.Node_Click);
}
I have a button that on click I would like to be disabled and it's background image to be changed to null here is the code I have that happens on button click
private void levelOne1001_Click(object sender, EventArgs e)
{
levelOne1001.Enabled = false;
levelOne1001.BackgroundImage = null;
scoreClass.genRandomNumber(100);
scoreClass.valOfQuestion = 100;
q1001 = true;
openQuestionForm();
}
And here is the code from openQuestionForm();
private void openQuestionForm()
{
QuestionForm qForm = new QuestionForm();
scoreClass.iCount++;
qForm.Show();
this.Hide();
}
And here is where I call this form back up
Level1Form l1Form = new Level1Form();
l1Form.Show();
How the process works is Button on Original form is clicked goes to a Question form, button on Question form is clicked it goes back to Original Form. But when I go back to the original form the button is still enabled and the image is still there. Is there any way to fix this?
EDIT: Forgot to say this was in WinForms
You are instantiating a new Level1Form, so it's returning to its default state, causing the button to return to its default state. There are a few possible approaches:
Add a parameter to Level1Form's constructor that indicates what state the button should be in, something like
Level1Form(bool enableButton) {
initComponent();
if(!enableButton) {
levelOne1001.Enabled = false;
levelOne1001.BackgroundImage = null;
}
}
Or, grab the same form again and reuse it. You will need to keep a reference to it somewhere and tell it to show itself again. Alternately, you can grab it out of Application.OpenForms
You're creating a new Level1Form instance, which has nothing to do with the existing instance that you modified.
You need to re-show the original instance.
You need to remember your initial form instance in a member outside the method and call show on it.
Level1Form l1Form;
private void FirstTimeCreate()
{
l1Form = new Level1Form();
}
private void Reshow()
{
l1Form.Show();
}
First add ImageList1 to your design page. Then click on the arrow on top of the imageList, then click "choose images" and add all the different kind of images that you want your button to change upon clicking on it. Then write the below code:
int im = 3;
private void levelOne1001_Click(object sender, EventArgs e)
{
levelOne1001.BackgroundImage = imageList1.Images[im];
switch (im)
{
case 0:
case 1:
case 2:
im++;
break;
default:
im =0; // assuming you have 3 images; if you are at image 3 and click then let it go to image with index 0 (which is the beginning).
break;
}
// levelOne1001.Enabled = false; // you can add this code above as you see fit
}
I have a countdown Timer form - on the first form the user will enter the countdown time - warning times, end message, etc. There are also two Radio buttons (Max/Min) and depending on which is selected they will open a new Max or Min form where the time will actually start to countdown. It is working fine and counting down as I expect. However, if I exit the Max or Min form and try to run again with new times I get the error. The code is below - note the comment out ShowDialog(this); was something I tried - it let me close and open the new forms ok but it did not actually start the countdown. UpdateLabels is the function that does the updating of Labels.
bool Max = rbMax.Checked;
if (Max == true)
{
//_Max.ShowDialog(this);
_Max.Show();
}
else
//_Min.ShowDialog(this);
_Min.Show();
UpdateLabels();
}
I also tried the following which I read online as a possible solution but it also did not work...
private void Max_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
this.Parent = null;
}
Can anyone help me out - I can post the UpdateLabels function if needed. I am pretty new to UI C# development so any help would be great. Thanks.
The problem is, that a closed form can not be used anymore (be reopened). Thats why the code you posted tries to stop closing and only hides your window. But for doing this, the Cancel-property must be set to true:
private void Max_FormClosing(object sender, FormClosingEventArgs e) {
this.Hide();
this.Parent = null;
e.Cancel=true;
}
To show the form after closing it this way, show it with the Show() method.
However this is probably only a workaround and you could solve the problem with another design.
Maybe it would be wise, to create a new instance of your form, every time you need it, instead of trying to reopen it every time. This also has the advantage that the form only requesires resources if it is really needed.
What you can do is add a following check before calling .Show method:
if(_Max == null || _Max.IsDisposed)
_Max = new MaxForm();
_Max.Show();
and similarly for _Min form
Whenever a form is closed, all its resources are freed. This means that you can't access the object any more, since it no longer exists - it's been freed and deleted from memory. To prevent that, you can cancel the closing of the form, and hide it instead (which will appear transparent to the user).
this.Hide();
e.Cancel=true;
An updated version of your code is as follows:
private void Max_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
this.Parent = null;
}
The solution is simple that instantiate the object of the called form in the button click event e.g.
private void buttonSetting_Click( object sender, EventArgs e )
{
***_setting = new SettingWindow();*** //When I need to show the settings window
_setting.Show();
}
create new instatnce if object is not available
if(frmRGB==nullptr || frmRGB.IsDisposed==true )
{
frmRGB= new Form();
}
Create Object inside button click event
like this
private void btn_supplier_order_Click(object sender, EventArgs e)
{
form_supplier_order so = new form_supplier_order();
so.Show();
}
I am working on a personal project in winforms just to gain some experience in it since I've never had the chance to work with it before. So, I'm quite the n00b when it comes to Winforms. This is the error I'm encountering:
In form BudgetTracker, I have a button called 'AddCat'. Below is the form's constructor and the button's click eventHandler:
public form_BudgetTracker()
{
InitializeComponent();
setEvents();
}
public void setEvents()
{
this.btn_AddCat.Click += new System.EventHandler(this.btn_AddCat_Click);
}
private void btn_AddCat_Click(object sender, EventArgs e)
{
form_NewCat NewCatForm = new form_NewCat();
var NewCatFormResult = NewCatForm.ShowDialog();
NewCatForm.Show();
}
In the NewCat form that comes up, I have a Cancel button. Code:
public form_NewCat()
{
InitializeComponent();
SetEvents();
}
private void SetEvents()
{
this.btn_Add.Click += new System.EventHandler(this.btn_Add_Click);
this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
The problem I'm facing is, when I click Add, the new form comes up. At this point, if I click Cancel, the form disappears but instantly a new instance of the form appears. I then click cancel again, and the form disappears.
What part of my code is making the form appear twice. I checked the contructors etc, but couldn't figure it out. Any help or pointers would be appreciated.
PS - As I mentioned, I'm new to winforms programming, so any cues or pointers would be appreciated as well.
private void btn_AddCat_Click(object sender, EventArgs e)
{
form_NewCat NewCatForm = new form_NewCat();
var NewCatFormResult = NewCatForm.ShowDialog(); // <-- opens the first time
NewCatForm.Show(); // <-- opens the second time
}
Judging from your code, you're simply showing the form twice!!!
form_NewCat NewCatForm = new form_NewCat();
var NewCatFormResult = NewCatForm.ShowDialog();
NewCatForm.Show();
The second line shows the form and blocks the method until DialogResult is set, then the third line shows the form without blocking the method.
Simply remove the third line!
Try stepping through the code using the F8 key instead of running it, or hitting F5. It will show you line by line what it's about to execute.
delete NewCatForm.Show();