namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
PongForm.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Can someone explain why I'm getting an error? I've had a look online and think it should work. I'm trying to change to a new form on button click.
In this function you should refer form, not PongForm:
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
Change "PongForm.Show();" to "form.Show().
To eloborate: you are attempting to call the class, not the instance you created.
to just add to what others have said. you probably don't want multiple of the same forms open. I cant comment or I would have done that instead. hope this solves your problem.
if (Application.OpenForms["PongForm"] != null)
{
Application.OpenForms["PongForm"].WindowState = FormWindowState.Normal;
Application.OpenForms["PongForm"].BringToFront();
}
else
{
PongForm form = new PongForm();
form.Show();
}
Related
I am trying to make a simple screen share application in C# and found this guide: https://www.c-sharpcorner.com/uploadfile/ulricht/how-to-create-a-simple-screen-sharing-application-in-C-Sharp/ and followed it but it doesn't work i tried it on the same computer and on two different PCs but nothing seems to work
//Host
public partial class ScreenShareHandler : Form
{
RDPSession x = new RDPSession();
public ScreenShareHandler()
{
InitializeComponent();
}
private void ScreenShareHandler_Load(object sender, EventArgs e)
{
}
private void Incoming(object Guest)
{
IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;//???
MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
}
private void button1_Click(object sender, EventArgs e)
{
x.OnAttendeeConnected += Incoming;
x.Open();
}
private void button2_Click(object sender, EventArgs e)
{
IRDPSRAPIInvitation Invitation = x.Invitations.CreateInvitation("Trial", "MyGroup", "", 10);
textBox1.Text = Invitation.ConnectionString;
}
private void button3_Click(object sender, EventArgs e)
{
x.Close();
x = null;
}
}
//Client
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string Invitation = textBox1.Text;// "";// Interaction.InputBox("Insert Invitation ConnectionString", "Attention");
axRDPViewer1.Connect(Invitation, "User1", "");
}
private void button2_Click(object sender, EventArgs e)
{
axRDPViewer1.Disconnect();
}
}
As written in my comments:
Have you hooked up the eventhandlers correctly? If you click on the button in the designer you can go to the Events Tab in the Property-window and check if the Click-event points to the right eventhandler. Another way to check if the correct handler is used is to put a breakpoint inside each handler. Then debug and check if you get into the right method when you click the button. If not you didn't hook up the Eventhandlers correctly.
I want to add an item to my combobox in form1 from form2. But if I press the button, form2 closes but the item is not added in form1. I hope you can help me! I found no solution for this problem in the internet.
Form 2:
public void button5_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.AddItem("Item");
this.Close();
}
Form 1:
public void AddItem(object item)
{
comboBox1.Items.Add(item);
}
First of all: You have to change comboBox access modifier to public. Then:
Form 2:
public void button5_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.AddItem("Item");
this.Hide(); // This will hide Form2 ("this." is redundant)
main.ShowDialog(); // This will show Form1
}
Reference the name property of your 'Form1' don't create a new instance. Then reference that forms combobox control.
Your original code creates a NEW instance. Meaning its creating a new version of Form1 and not accessing the original!
The code below should help
public void button5_Click(object sender, EventArgs e)
{
Form1 myForm = whateverYourFormsNamePropertyIs;
myForm.Combobox.AddItem("Item");
this.Close();
}
In form2...
Form1 f;
public Form2(Form1 parent)
{
InitializeComponent();
f = parent;
}
private void Add_Click(object sender, EventArgs e)
{
f.comboBox1.Items.Add("item");
}
In form1
public void AddItem(object item)
{
comboBox1.Items.Add(item);
Form2 f = new Form2(this);
f.Show();
}
orginal form
private void FormPeople_Load(object sender, EventArgs e)
{
populateComboBoxTitles();
}
public void populateComboBoxTitles()
{
comboBox2.Items.Clear();
comboBox2.Items.Add("mr");
comboBox2.Items.Add("miss");
}
private void button5_Click(object sender, EventArgs e)
{
FormAddTitle formAddTitle = new FormAddTitle(this);
formAddTitle.Show();
}
secondary form
FormPeople formPeople;
public FormAddTitle(FormPeople formPeople)
{
InitializeComponent();
this.formPeople = formPeople;
}
private void button1_Click(object sender, EventArgs e)
{
if (formPeople != null)
formPeople.populateComboBoxTitles();
}
I have created one loading form, but I want access the loading form and load the different form. I know there is a way to achieve this, but the way that I think is create another loading form and access that another loading form to load the different form, even though the loading form contents are same.
How can I achieve this?
Here is the code of Loading Form:
public Loading()
{
InitializeComponent();
this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000);
this.timer1.Tick += new EventHandler(this.CheckTimer);
}
private void Loading_Load(object sender, EventArgs e)
{
this.timer1.Start();
}
private void CheckTimer(object sender, EventArgs e)
{
uint timeLeft = 1;
timeLeft--;
if (timeLeft == 0)
{
this.timer1.Stop();
this.Hide();
AgeConfirmation _ageConfirmation = new AgeConfirmation();
_ageConfirmation.ShowDialog();
this.Close();
}
}
Above code is one loading form and load another form by the time is reached 0.
I have tried like this:
public class SystemManager
{
public static void LoadForm(Form _form = null, Form _loadForm = null)
{
_form.Hide();
_loadForm = new Form();
_loadForm.ShowDialog();
_form.Close();
}
}
and access it like this:
SystemManager.LoadForm(this, AgeConfirmation);
But it is throws the following error:
'System.Windows.Forms.AgeConfirmation' is a 'type' but is used like a 'variable'
My Question is: Create only one form (Loading Form), and access that Loading Form and by the time, the time reached 0, it will access different form.
Your answer much appreciated!
Thank you very much!
You should be using _ageConfirmation, which is the form object, not AgeConfirmation which is the form type.
i.e. SystemManager.LoadForm(this, _ageConfirmation);
Solved by myself! I create a getter and setter int value and by using switch case and the code will be like this:
public class UserInformation
{
public static int Value
{
get;
set;
}
}
public partial class Loading : Form
{
public Loading()
{
InitializeComponent();
this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000);
this.timer1.Tick += new EventHandler(this.CheckTimer);
}
private void Loading_Load(object sender, EventArgs e)
{
this.timer1.Start();
}
private void CheckTimer(object sender, EventArgs e)
{
uint timeLeft = 1;
timeLeft--;
if (timeLeft == 0)
{
this.timer1.Stop();
this.Hide();
switch (UserInformation.Value)
{
case 0:
AgeConfirmation _ageConfirmation = new AgeConfirmation();
_ageConfirmation.ShowDialog();
break;
case 1:
MainForm _mainForm = new MainForm();
_mainForm.ShowDialog();
break;
case 2:
Event _event = new Event();
_event.ShowDialog();
break;
}
this.Close();
}
}
}
private void AgeConfirmation_Load(object sender, EventArgs e)
{
UserInformation.Value = 1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
private void MainForm_Load(object sender, EventArgs e)
{
UserInformation.Value = 2;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
By the time the program runs, UserInformation.Value will be 0
Thank you very much for those who are replying to my question.
Change
_ageConfirmation.ShowDialog();
to
_ageConfirmation.Show();
I am have trouble using multiple windows in WPF and switching between them using a button. In theory my application should have 2 buttons, one forward and one back each on respectively changing the window to the previous and next window.
Unfortunately I get a Stackoverflow error, through my research I feel that it has something to do with me creating new windows that are creating the window again when the previous window is created, thus making a horrible loop. However I don't know where I can put the window creation code to stop this problem or if there are other ways to fix this.
Here is code for my windows:
First Window
public partial class Presenation_1 : Window
{
Presentation_2 p2 = new Presentation_2();
MainWindow M = new MainWindow();
public Presenation_1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
}
Second Window
public partial class Presentation_2 : Window
{
Presentation_3 p3 = new Presentation_3();
Presenation_1 p1 = new Presenation_1();
public Presentation_2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p1.Show();
}
}
Third Window
public partial class Presentation_3 : Window
{
Presentation_4 p4 = new Presentation_4();
Presentation_2 p2 = new Presentation_2();
public Presentation_3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
p4.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p2.Show();
}
}
Fourth Window
public partial class Presentation_4 : Window
{
Presentation_3 p3 = new Presentation_3();
MainWindow M = new MainWindow();
public Presentation_4()
{
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.Close();
M.Show();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.Close();
p3.Show();
}
}
Thanks in advance
Don't create your Windows before the button is clicked, you can create them in the event handler:
private void btnForward_Click(object sender, RoutedEventArgs e)
{
var p2 = new Presentation_2();
this.Close();
p2.Show();
}
When you create a windows, you create 2 other windows with
new Presentation_X()
This new windows is automaticaly show and itself open 2 other windows.
You can create this windows once in the Mainwindow (auto hide this one), pass the reference in constructor and not close these windows. Quick example (not tested) :
public partial class Presenation_X : Window
{
private Window preview;
private Window next;
public Presenation_X(Window w1, Window w2)
{
this.preview = w1;
this.next = w2;
InitializeComponent();
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
this.next.Show();
this.Hide();
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
this.preview.Show();
this.Hide();
}
}
I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.