I have a main window, inside there is a button which in a click-
open a dialog window to write a name save a name in a textbox and need to open another window.
I want it will open the new window at the main window-
but it is opened the window in the dialog window, allthoght I set the Owner to be the main window..
what should I do?
this is the code in a button of the dialog window:
Screen myScreen = new Screen (name, ViewModel, mainWindow);
myScreen.Owner = mainWindow;
myScreen.Show();
this.Close();
This is how you should do it:
In your first dialog, you must have a public variable,something like:
public string TextBoxContent;
And in the OK button event, you'll do something like:
this.TextBoxContent = TextBox.Text;
this.DialogResult = true;
this.Close();
In your main window you must do this:
string returnedString;
DialogWindow w = new DialogWindow ();
w.ShowDialog();
if (w.DialogResult.HasValue && w.DialogResult.Value)
returnedString=w.TextBoxContent;
And after that, you'll show your second dialog with the returned string. Hope you see the logic.
Ok you have three windows one is MainWindow which have a button to open another window name Dialog which have a textBox for input name and a button for pass that textBox input to another window name Screen. SO I have take a textBox in Screen window named textBoxName which will show the passed text, here is my solution
the main window button show dialog this way
private void buttonMainShowDialog_Click(object sender, RoutedEventArgs e)
{
Dialog dl = new Dialog();
dl.ShowDialog();
}
Constructor for Screen window as below which take text as a parameter by which we pass the Dialog window textBox text
public Screen(string text)
{
InitializeComponent();
this.textBoxName.Text = text;
}
and you need to call this screen window from dialog window as below
private void buttonDialogShowScreen_Click(object sender, RoutedEventArgs e)
{
Screen myScreen = new Screen(this.textBox.Text);
myScreen.Show();
this.Close();
}
its works fine, hope this help you
Related
I have a login form which starts a dialog of a lecturerWindow
When swapping windows it looks like that:
//LoginWindow to LecturerClient
this.Visibility = Visibility.Collapsed;
LecturerWindow lecturerClient = new LecturerWindow(self);
lecturerClient.Owner = this;
lecturerClient.ShowDialog();
this.Visibility = Visibility.Visible; // so when the lecturerClient dialogs exits - the login form will be visible
And also my LecturerWindow opens another dialog:
//LecturerClient To Session
Dispatcher.Invoke(() =>
{
Visibility = Visibility.Collapsed;
Session newSession = new Session(mySelf, Courses.Find(item => item.courseId == courses[1].ToString()));
newSession.Owner = this;
newSession.ShowDialog();
Visibility = Visibility.Visible;
});
The issue begins when my Session dialog closes and suddenly both my LoginWindow and my LecturerWindow goes Visible, it's like my LoginWindow thinks the session closing is the lecturerWindow closing
Thank you in advance!
I'm sure you have solved your issue already, but here's a solution for future readers:
Instead of hiding the windows in the app by setting window.Visibility = Visibility.Collapsed;, I would simply open the new window and then close the old window:
// in LoginWindow.xaml.cs
private void SubmitButton_OnClick(object sender, RoutedEventArgs e)
{
// create a new window to show
LecturerWindow newWindow = new LecturerWindow()
{
Title = "Lecturer Window",
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
// change the app main window to use the new window
Application.Current.MainWindow = newWindow;
newWindow.Show();
// close the LoginWindow
this.Close();
}
Note that if you close the current window before opening the new one, if there are no other windows open, the application will initiate shutdown. This behavior is determined by the Application.ShutdownMode property. By opening the new window before closing the current we avoid this (like shown above).
The same can be done in your LecturerClient to open the Session window.
When closing the LecturerClient window you can open a new LoginForm window again by subscribing to the Closing event:
// in LecturerWindow.xaml.cs
public LecturerWindow()
{
InitializeComponent();
Closing += OnClosing;
}
// Open the login form again when this window is closing
private void OnClosing(object sender, CancelEventArgs e)
{
// create a new window to show
LoginWindow newMainWindow = new LoginWindow()
{
Title = "Login form window",
WindowStartupLocation = WindowStartupLocation.CenterScreen
};
// change the app main window to use the new window
Application.Current.MainWindow = newMainWindow;
newMainWindow.Show();
// This window is already closing, so we don't need to call Close()
}
My program is basically reading barcodes, but I want to allow users to enter barcode manually, so I created a pop up window for that. After entering the barcode, I want the pop up window to disappear and send data to the main form when ENTER is hit, but I have no clue how this data can be passed to the main form.
Ok, this is pretty straightforward, I'll show you with an example. You have two forms: Form1 mainForm and Form 2 subForm.
mainForm calls subForm as follows:
using (Form2 subForm = new Form2())
{
if (subForm.ShowDialog() == DialogResult.OK)
{
string my_text = subForm.TextToReturn;
// Do stuff with my_text
}
}
In SubForm you will have something like this declared in the class scope:
public string TextToReturn;
private void button1_Click(object sender, EventArgs e)
{
TextToReturn = text_box.Text;
this.DialogResult = DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
mtp1Start = myTrackPanelss1.Start;
mtp1End = myTrackPanelss1.End;
button1.Enabled = false;
Animation_Radar_Preview ap = new Animation_Radar_Preview();
ap.FormClosing += new FormClosingEventHandler(ap_FormClosing);
ap.Show();
}
I want that the form ap will keep showing display even if i click on other button on the form behind it.
I have one big form and when i click this button a smaller form is open.
I want that the smaller form ap will keep showing stay on place when i click on the big form so the smaller form won't move to the back behind the big form.
Set Form.TopMost property to true:
ap.TopMost = true;
ap.Show();
try to show like this,
Animation_Radar_Preview ap = new Animation_Radar_Preview();
ap.FormClosing += new FormClosingEventHandler(ap_FormClosing);
ap.Show(this);
Assign the parameter this in Show() method. it will set the current form as a owner of child(ap).
I'm new to C# and could use some help.
What I have so far is a set of 8 windows forms I have created in C#, these forms have basic things like text boxes, labels, radio buttons, etc. Now that I have completed making all of these forms I want to have one additional form (called the Selector form) I can use to select one of the other 8 forms. At any given time, I want the Selector form to be on top of other windows and it will have 8 radio buttons (or regular buttons, doesn't matter). When one of the buttons is clicked, the current form (not the Selector form) should disappear and a new form should appear. The name of the button will be the name of the new form that appears.
I have seen a few examples and here is the code I have so far:
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
form1.Closed += (sender1, args) => this.Close();
form1.Show();
}
void Button2Click(object sender, EventArgs e)
{
// this.Hide();
var form2 = new CCARAdmin();
form2.Closed += (sender1, args) => this.Close();
form2.Show();
//Application.Run(new CCARAdmin());
}
Problem I am having is I don't want to hide the Selector form, which this does, and I don't know how to identify the other form that is open to close it and then open a different form.
From starting the program the logic would be like this:
Show Selector form
When a button is clicked on the Selector form, keep the Selector form on top and show the other form with the name of the button.
When a different button is clicked on the Selector form, close the previous form that was open (not the Selector form) and open the new form corresponding to the name of the button. Keep the Selector form on top.
When the Selector form is close, application stops.
Problem I am having is I don't want to hide the Selector form, which
this does, and I don't know how to identify the other form that is
open to close it and then open a different form.
Set Selector form TopMost to True to make it always on top. Or
you can use BringToFront after opening a new form
to know other forms that are open check this answer. Or you can define each From as a field in the Selector form, and check that.
selectorForm.TopMost = true ( this will help to keep the selector form always on top).
Create a form variable in your selector form to keep the reference of your currently opened form.
Sample code for 1 button click :
Form frm = null;
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
if (frm == null)
{
frm = form1;
}
else
{
frm.Close();
}
form1.Show();
this.TopMost = true;
frm = form1;
}
I resolved this by setting TopMost to true and then using the following code under each one of the buttons:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "FormSelector")
Application.OpenForms[i].Close();
}
var form = new TRAC();
if (radioButton9.Checked == true)
{
form.Show();
}
I try to get the following done:
A WPF application where i have multiple buttons where you can set a notification message.
Depending on the button, you can set different messages.
What i did, was on the message button i have put this code:
private void button1_Click(object sender, RoutedEventArgs e)
{
CounterMessage msgOne = new CounterMessage();
msgOne.ShowDialog();
}
This will open op a new WPF window here only is a textbox and an exit button.
On exit in this message window, it will save the message to a parameter.
But here is the trick.
I want to use this message window for multiple notifications, and it will display in the textbox any text content if there is already any on a string in the application.
So for example:
In the main app i have button A and B to set the notification on.
I click on button A, the showdialog pops up and in the textbox already have "you clicked button A"
If it was button B that has been clicked, it should display "you clicked button B"
So i should sent some extra info with the ShowDialog, so i can use the messagewindow for each one.
Could someone help me out a bit herE?
I must say i find it a bit hard do decently discribe what i want, so i hope i made myself clear enough.
EDIT
So hat i want is showing the content of a string parameter (to be exact: Properties.Settings.Default.XXX) into the textbox that is in the Countermessage window
I am not entirely sure what you are asking, but it sounds like you want something like this. I am assuming that CounterMessage is a Window, and that there is some binding mechanism or property that displays what the message is.
public class CounterMessage : Window
{
public CounterMessage(string message)
{
this.Message = message;
}
public string Message
{
get;
set;
}
}
Your button event would then be something along the lines of:
private void button1_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
CounterMessage msgOne = new CounterMessage(btn.Text);
msgOne.ShowDialog();
}
The point being that you don't send something to the ShowDialog method, but rather to the class that is the dialog itself. I also assume that the dialog does more than just displaying the message - otherwise, you would just use MessageBox.Show(....)
Button btn = (Button)sender;
Debug.WriteLine(btn.Name);