Here is the whole code from form1 :
using System;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO.Compression;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Diagnostics.Tracing;
namespace BSCS_Launcher
{
public partial class Form1 : Form
{
private WebClient webClient = null;
const string basPath = #"cstrike";
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form6 f6 = new Form6();
f6.ShowDialog(); // Shows Form6 - Launchoptions
}
private void button1_Click(object sender, EventArgs e)
{
string path = #"steamlop.lop";
string cfgpath = #"cstrike/config.cfg";
FileAttributes attributes = File.GetAttributes(cfgpath);
if (File.Exists(path))
{
File.SetAttributes(cfgpath, File.GetAttributes(cfgpath) | FileAttributes.ReadOnly);
var str = File.ReadAllText(path);
System.Diagnostics.Process.Start("hl.exe", arguments: str);
}
else
{
string message = "Launch options not found. Please go into launch options and click double click Reset!!!";
string title = "Launch options error";
MessageBox.Show(message, title);
}
}
private void button3_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(); // Shows Form2 - Features
}
private void button4_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.ShowDialog(); // Shows Form3 - Options
}
private void button5_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://fastxhoster.com/");
}
private void button7_Click(object sender, EventArgs e)
{
Form5 f5 = new Form5();
f5.ShowDialog(); // Shows Form5 - Updatelauncher
// Is file downloading yet?
if (webClient != null)
{
return;
}
var dirdir = new DirectoryInfo($"{basPath}");
if (!dirdir.Exists)
{
webClient = new WebClient();
webClient.DownloadFileCompleted += FileDownloading;
webClient.DownloadFileAsync(new Uri("https://sipi-portfolio.000webhostapp.com/verzija.php"), $"{basPath}/version.php");
}
else
{
webClient = new WebClient();
webClient.DownloadFileCompleted += FileDownloading;
webClient.DownloadFileAsync(new Uri("https://sipi-portfolio.000webhostapp.com/verzija.php"), $"{basPath}/version.php");
} //Ovaj kod treba da se izvrsi tek kada je igracu skinut novi update ukoliko ga je bilo
}
private void FileDownloading(object sender, AsyncCompletedEventArgs e)
{
webClient = null;
string pathfile = $"{basPath}/verzija.php";
string pathfile2 = $"{basPath}/version.php";
var str = File.ReadAllText(pathfile);
var str2 = File.ReadAllText(pathfile2);
if (str != str2)
{
CloseProcessing();
string message = "We found an avilable update for this launcher!";
string title = "Update launcher";
MessageBox.Show(message, title);
}
else
{
CloseProcessing();
string message = "No new updates!";
string title = "Update launcher";
MessageBox.Show(message, title);
}
}
public void CloseProcessing()
{
Form5 f5 = new Form5();
f5.Close(); // Closes Form5 - Updatelauncher
}
}
}
Here you can see that I created code for download one file from one website, and it should check if those 2 files have the same context, if not it should do something and if yes it should do something again, in both situations, it should close form5, but it not closing... I tried many codes to resolve this but it not affects...
Thanks in advance...!
just Declare Form5 outside of methods (in the class) and for closing it don't rredefine it:
private void button7_Click(object sender, EventArgs e)
{
Form5 f5 = new Form5();
f5.ShowDialog(); // Shows Form5 - Updatelauncher
to
Form5 f5 = new Form5();
private void button7_Click(object sender, EventArgs e)
{
f5.ShowDialog(); // Shows Form5 - Updatelauncher
and
public void CloseProcessing()
{
//remove this line: //Form5 f5 = new Form5();
f5.Close(); // Closes Form5 - Updatelauncher
}
when you call Form5 f5 = new Form5(); in CloseProcessing you create a new instance of form5 instead of closing existing one.
Related
I created a little program to create a log file to record people's ID number, so far it runs good, no issues or errors, but recently I notice after running for three days it froze another program, until I force closed it. Can anyone take a look to the code to see if there is anything is wrong with it or to improve the code. Thank you.
The programs works with .NET Frameworks 3.5 and is for a Windows XP system, if is possible to make it work with a lower .NET Framework to reduce the installation of additional files.
MainWin form creates a fullscreen window to mask/cover some elements from other software. Is set as topmost to be always be on the top of everything. It has a transparent section with in a text file, then it minimize the window and finally activates a timer. When the timer finish, its maximaze the window again. It has a button to open the LoginWin form and a button to clear the data from the serieBox and return the cursor to the textbox.
LoginWin form is a window to input login information to open the LogFileWin form.
LogFileWin form is a window to read the saved data from the text file in a richTextBox, this data is from the MainWin form. It has a close button and a button to open FolderBrowserDialog to save the text file in another location or to a removable storage device.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LogSerie
{
public partial class MainWin : Form
{
public MainWin()
{
InitializeComponent();
}
private void MainWin_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void serieBox_KeyDown(object sender, KeyEventArgs e)
{
this.serieBox.MaxLength = 10;
if (e.KeyCode == Keys.Enter)
{
if ((serieBox.Text != ""))
{
if (serieBox.Text == "WLMANTO")
{
StreamWriter B = new StreamWriter("LogfileOperator.txt", true);
B.WriteLine(DateTime.Now + " " + label1.Text + " " + serieBox.Text);
B.Close();
serieBox.Clear();
this.WindowState = FormWindowState.Minimized;
timerManto.Enabled = true;
}
else
{
StreamWriter A = new StreamWriter("LogfileOperator.txt", true);
A.WriteLine(DateTime.Now + " " + label1.Text + " " + serieBox.Text);
A.Close();
serieBox.Clear();
this.WindowState = FormWindowState.Minimized;
timerOperador.Enabled = true;
}
}
}
}
private void timerOperador_Tick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
timerOperador.Enabled = false;
}
private void timerManto_Tick(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
timerManto.Enabled = false;
}
private void logButton_Click(object sender, EventArgs e)
{
LoginWin openForm = new LoginWin();
openForm.ShowDialog();
}
private void borrarBut_Click(object sender, EventArgs e)
{
serieBox.Clear();
serieBox.Select();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace LogSerie
{
public partial class LoginWin : Form
{
public LoginWin()
{
InitializeComponent();
}
private void LoginWin_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void entrarBut_Click(object sender, EventArgs e)
{
if ((usuBox.Text != "") && (contraBox.Text != ""))
{
if ((usuBox.Text == "ADMIN") && (contraBox.Text == "PASS"))
{
LogFileWin openForm = new LogFileWin();
openForm.TopMost = true;
openForm.ShowDialog();
usuBox.Clear();
contraBox.Clear();
this.Close();
}
else
{
MessageBox.Show("Login Incorrect", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void cancelBut_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace LogSerie
{
public partial class LogFileWin : Form
{
public LogFileWin()
{
InitializeComponent();
}
private void LogFileWin_Load(object sender, EventArgs e)
{
this.TopMost = true;
}
private void logfileButton_Click(object sender, EventArgs e)
{
string path = #"C:\LogfileOperator\LogfileOperator.txt";
StreamReader stream = new StreamReader(path);
string filedata = stream.ReadToEnd();
richTextBox1.Text = filedata.ToString();
stream.Close();
}
private void closeBut_Click(object sender, EventArgs e)
{
this.Close();
}
private void usbBut_Click(object sender, EventArgs e)
{
string fileName = "LogfileOperator.txt";
string sourcePath = #"C:\LogfileOperator";
using (FolderBrowserDialog ofd = new FolderBrowserDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo fileInfo = new FileInfo(fileName);
sourcePath = Path.Combine(ofd.SelectedPath, fileInfo.Name);
File.Copy(fileName, sourcePath, true);
MessageBox.Show("Logfile Saved", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
If you're forced to use this old OS and old framework, you're going to have weird bugs like this. You can maybe work around it by reducing resource usage.
You're constantly creating new copies of your forms LoginWin and LogFileWin, which require OS resources. Instead create one instance of each form and re-use them.
There's also not any exception handling in your code, so you could have problems if files don't exist, or permissions change, or all sort of things. You need to have exception handling and that will give you more information about problems when they occur.
To re-use a form, create an instance as a private field in your class:
public partial class LoginWin : Form
{
// store the LogFileWin form so that we can re-use it
private LogFileWin _logFileWin;
public LoginWin()
{
InitializeComponent();
_logFileWin = new LogFileWin(); { TopMost = true; }
}
private void LoginWin_Load(object s, EventArgs e) { TopMost = true; }
private void entrarBut_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(usuBox.Text) ||
string.IsNullOrEmpty(contraBox.Text))
{
return;
}
if ((usuBox.Text == "ADMIN") &&
(contraBox.Text == "PASS"))
{
// add a reset function to the form
// that makes it ready to display again
_logFileWin.Reset();
// show the dialog
_logFileWin.ShowDialog();
usuBox.Clear();
contraBox.Clear();
this.Close();
}
else
{
MessageBox.Show("Login Incorrect",
"Message",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void cancelBut_Click(object s, EventArgs e) { Close(); }
}
For exception handling:
public partial class LogFileWin : Form
{
public LogFileWin()
{
InitializeComponent();
}
private void LogFileWin_Load(object s, EventArgs e) { TopMost = true; }
// new function to reset the the dialog to be shown again
public void Reset() { richTextBox1.Text = string.Empty; }
private void logfileButton_Click(object s, EventArgs e)
{
var path = #"C:\LogfileOperator\LogfileOperator.txt";
try
{
// reading from a file can fail, so this
// needs to be wrapped in a try catch
using (var stream = new StreamReader(path))
{
richTextBox1.Text = stream.ReadToEnd();
}
}
catch (Exception ex)
{
var m = String.Format("Unable to read '{0}'; {1}",
path, ex.Message);
MessageBox.Show(message,
"File read error",
MessageBoxButtons.Ok,
MessageBoxIcon.Error);
}
}
private void closeBut_Click(object s, EventArgs e) { this.Close(); }
// we can re-use the folder browser dialog;
// don't need to create a new one every time
FolderBrowserDialog _ofd = new FolderBrowserDialog();
private void usbBut_Click(object sender, EventArgs e)
{
var sourceFileName = "LogfileOperator.txt";
var destFolder = #"C:\LogfileOperator";
var destFileName = string.Empty;
try
{
if (_ofd.ShowDialog() == DialogResult.OK)
{
destFileName = Path.Combine(_ofd.SelectedPath,
sourceFileName);
// every time you do anything with a
// file, it needs to be in a try/catch
// in case the file doesn't exist,
// or the user doesn't have permission.
File.Copy(sourceFileName, destFileName, true);
MessageBox.Show("Logfile Saved",
"Message",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
// show a message to the user informing them
// of the error and why it occurred.
var m = string.Format("Copy '{0}' to '{1}' failed; {2}",
sourceFileName,
destFileName,
ex.Message);
MessageBox.Show(m,
"File copy error",
MessageBoxButtons.Ok,
MessageBoxIcon.Error);
}
}
}
enter code here
namespace WindowsFormsApplication1
{
public partial class Mainmenu : Form
{
Sendingmail sm = new Sendingmail();
public Mainmenu()
{
InitializeComponent();
}
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
notifyIcon1.Icon = SystemIcons.Exclamation;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = "Patient medicine";
notifyIcon1.BalloonTipText = "Please be noted that a patient should take his medicine now" +
Environment.NewLine +
"click on the icon when medicine given";
notifyIcon1.ShowBalloonTip(20000);
notifyIcon1.Visible = true;
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Sendingmail sm = new Sendingmail();
sm.Show();
}
a notification will be displayed, when i click on the notification displayed, i want a form to be opened,i tried the mouseDoubleClick function as shown above, but it also didn't work.
Any help?
Did you declare the sm variable above?
Try declaring the form sm variable before the sm.Show(); in your mousedoubleclick.
on double click you should write code as below.
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Form2 sm = new Form2();
sm.Show();
}
//make sure you create new object of your SM form.
add following code inside button6_Click function at bottom.
notifyIcon1.Click += (sender, e) =>
{
Sendingmail sm = new Sendingmail();
sm.Show();
};
I have two winforms. frmMainMenu and frmLogin . In the frmMainMenu I have a menuStrip. Where I have kept some menu including logout option in one of them. So when I login , frmMainMenu comes to the front. I am just not doing anything, Just clicking on logout option frmMainMenu get closed and frmLogin form again shows up. So when click on red cross button of frmLogin form I get the exception error like below:
Unhandled exception has occourred in your application. If you click Continue, the applicaion will ignore this error and attempt to continue. If you click Quit, the application will close immediately.
Collection was modified; enumeration operation may not execute.
Code of frmMainMenu:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Restaurant_Management_System
{
public partial class frmMainMenu : Form
{
public frmMainMenu()
{
InitializeComponent();
}
ConnectionString cs = new ConnectionString();
private void productToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProduct frm = new frmProduct();
frm.Show();
}
private void categoryToolStripMenuItem_Click(object sender, EventArgs e)
{
frmCategory frm = new frmCategory();
frm.Show();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAbout frm = new frmAbout();
frm.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
ToolStripStatusLabel4.Text = System.DateTime.Now.ToString();
}
private void registrationToolStripMenuItem_Click(object sender, EventArgs e)
{
frmRegistration frm = new frmRegistration();
frm.Show();
}
private void loginDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
// frmLoginDetails frm = new frmLoginDetails();
frmPassVerify frm = new frmPassVerify();
frm.Show();
}
private void profileEntryToolStripMenuItem_Click(object sender, EventArgs e)
{
frmCustomers frm = new frmCustomers();
frm.Show();
}
private void billingToolStripMenuItem_Click(object sender, EventArgs e)
{
frmSales frm = new frmSales();
frm.Show();
}
private void customersToolStripMenuItem_Click(object sender, EventArgs e)
{
frmPassVerCus frm = new frmPassVerCus();
frm.Show();
}
private void salesToolStripMenuItem_Click(object sender, EventArgs e)
{
//frmSalesRecord1 frm = new frmSalesRecord1();
frmPassVerSale frm = new frmPassVerSale();
frm.Show();
}
private void typeToolStripMenuItem_Click(object sender, EventArgs e)
{
frmTypeReg frm = new frmTypeReg();
frm.Show();
}
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
frmInventory frm = new frmInventory();
frm.Show();
}
//I think here is the problem arises. on clicking the logout
private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmLogin frm = new frmLogin();
this.Dispose();
frm.Show();
}
}
}
code for frmLogin:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Restaurant_Management_System
{
public partial class frmLogin : Form
{
ConnectionString cs = new ConnectionString();
DataTable dt = new DataTable();
public frmLogin()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Focus();
return;
}
if (txtPassword.Text == "")
{
MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPassword.Focus();
return;
}
try
{
SqlConnection myConnection = default(SqlConnection);
myConnection = new SqlConnection(cs.DBConn);
SqlCommand myCommand = default(SqlCommand);
myCommand = new SqlCommand("SELECT Username,password FROM Registration WHERE Username = #username AND password = #UserPassword", myConnection);
SqlParameter uName = new SqlParameter("#username", SqlDbType.VarChar);
SqlParameter uPassword = new SqlParameter("#UserPassword", SqlDbType.VarChar);
//if(txtUserName.Text != "admin")
uName.Value = txtUserName.Text;
uPassword.Value = txtPassword.Text;
myCommand.Parameters.Add(uName);
myCommand.Parameters.Add(uPassword);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myReader.Read() == true)
{
int i;
ProgressBar1.Visible = true;
ProgressBar1.Maximum = 5000;
ProgressBar1.Minimum = 0;
ProgressBar1.Value = 4;
ProgressBar1.Step = 1;
for (i = 0; i <= 5000; i++)
{
ProgressBar1.PerformStep();
}
this.Hide();
if (txtUserName.Text == "admin")
{
frmMainMenu frm = new frmMainMenu();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
if (txtUserName.Text != "admin")
{
frmMainMenu1 frm = new frmMainMenu1();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
}
else
{
MessageBox.Show("Login is Failed...Try again !", "Login Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Clear();
txtPassword.Clear();
txtUserName.Focus();
}
if (myConnection.State == ConnectionState.Open)
{
myConnection.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
ProgressBar1.Visible = false;
txtUserName.Focus();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Dispose();
Application.Exit();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
frmChangePassword frm = new frmChangePassword();
frm.Show();
frm.txtUserName.Text = "";
frm.txtNewPassword.Text = "";
frm.txtOldPassword.Text = "";
frm.txtConfirmPassword.Text = "";
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
frmRecoveryPassword frm = new frmRecoveryPassword();
frm.txtEmail.Focus();
frm.Show();
}
}
}
How can I handle this exception ? I dont wanna see this error on closing.
thanks in advance :)
Assuming that below event is attached to LogOut Button. Remove/comment this.Dispose(); line
private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmLogin frm = new frmLogin();
this.Dispose(); //Comment This Line
frm.Show();
}
I have a system windows forms where i show the web browser control over the media-player control as shown in image below,
I want to make Transparent web browser control. I have try many things but i am not able to make transparency over web browser control, i have try :
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.FromArgb(0, 0, 0, 0);
this.TransparencyKey = Color.Red;
this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;*/
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
My code is :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DirectX.AudioVideoPlayback;
using System.Windows.Forms;
namespace Windows_Video
{
public partial class Form1 : Form
{
Video vdo;
public string mode = "play";
public string PlayingPosition, Duration;
public Form1()
{
InitializeComponent();
// VolumeTrackBar.Value = 4;
}
private void Form1_Load(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
}
axWindowsMediaPlayer1.URL = textBox1.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void button1_Click(object sender, EventArgs e)
{
/* OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
}*/
}
private void button2_Click(object sender, EventArgs e)
{
// axWindowsMediaPlayer1.URL = textBox1.Text;
// axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//this.BackColor = System.Drawing.Color.Transparent;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
}
}
}
you have to make another form with a web browser control set the opacity 50% in the form properties and then call on button or by some other method it will be work:
private void button1_Click(object sender, EventArgs e)
{
Form2 min = new Form2();
min.Show();
}
By this technique u can set the opacity over web browser control/full form
I get the default path from the registry for the Steam installation. But if someone has their games installed to a different folder, the user has to enter it in the Configure form. When the form gets closed, that path entered(from the folder browser or by typing the path in manually) should get saved to a string in the main form and should enable a different Combobox which turns on different buttons. I somehow managed to do the save to the mainform string, but the 2nd combobox doesn't seem to turn on. How can I do it correctly?
** MAIN FORM **
public string NewPath { get; set; }
private ConfigForm otherForm;
string InstallPath = (string)Registry.GetValue(#"HKEY_CURRENT_USER\SOFTWARE\Valve\Steam", "SteamPath", null);
private void PortalHammerButton_Click(object sender, EventArgs e)
{
Process.Start(InstallPath + #"\SteamApps\common\Portal\bin\hammer.exe");
}
private void Gamedropdown_SelectedIndexChanged(object sender, EventArgs e)
{
if (Gamedropdown.Text == "Portal") // When Portal is selected
{
// Enable the Portal SDK buttons
PortalHammerButton.Visible = true;
PortalModelViewerButton.Visible = true;
PortalFacePoserButton.Visible = true;
// Disable the CS:GO SDK buttons
csgoFacePoserButton.Visible = false;
csgoHammerButton.Visible = false;
csgoModelViewerButton.Visible = false;
}
else if (Gamedropdown.Text == "CS:GO") // When CS:GO is selected
{
// Disable Portal SDK buttons
PortalHammerButton.Visible = false;
PortalModelViewerButton.Visible = false;
PortalFacePoserButton.Visible = false;
// Enable CS:GO SDK buttons
csgoFacePoserButton.Visible = true;
csgoHammerButton.Visible = true;
csgoModelViewerButton.Visible = true;
}
}
private void ConfigureButton_Click(object sender, EventArgs e)
{
var configdialog = new ConfigForm();
configdialog.Show();
}
private void PortalDifferentHammerButton_Click(object sender, EventArgs e)
{
Process.Start(NewPath + #"\SteamApps\common\Portal\bin\hammer.exe");
}
private void NewDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
if (NewDropDown.Text == "Portal") // When Portal is selected
{
// Enable the Portal SDK buttons
PortalDifferentHammerButton.Visible = true;
PortalDifferentModelViewerButton.Visible = true;
PortalDifferentFacePoserButton.Visible = true;
// Disable the CS:GO SDK buttons
DifferentCSGOFaceposerButton.Visible = false;
DifferentCSGOHammerButton.Visible = false;
DifferentCSGOModelViewerButton.Visible = false;
}
else if (NewDropDown.Text == "CS:GO") // When CS:GO is selected
{
// Disable the Portal SDK buttons
PortalDifferentFacePoserButton.Visible = false;
PortalDifferentHammerButton.Visible = false;
PortalDifferentModelViewerButton.Visible = false;
// Enable the CS:GO SDK buttons
DifferentCSGOModelViewerButton.Visible = true;
DifferentCSGOHammerButton.Visible = true;
DifferentCSGOFaceposerButton.Visible = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.Close();
}
}
}
**CONFIGURE FORM**
public partial class ConfigForm : Form
{
public ConfigForm()
{
InitializeComponent();
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
public void DifferentFolderBrowseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string newpath = fbd.SelectedPath;
NewPathBox.Text = newpath;
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
public void CloseButton_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm1 = new Form1();
frm1.Gamedropdown.Visible = false;
frm1.NewDropDown.Visible = true;
}
}
}
Any help would be appriciated.
Look at your ConfigForm. Here's your problem:
public ConfigForm()
{
InitializeComponent();
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
What you're doing on your Form1 (which I'm guessing is your Main form) is creating a new instance of your ConfigForm and showing it. What you're doing in your ConfigForm is creating a new main form and setting the NewPath = to the value entered on your config form. The problem is this new Form1 is NOT the Form1 that created the ConfigForm. The Form1 that created your config form is not the one getting updated by your code, some arbitrary new Form1 that you create is the one getting updated. This is why your code isn't working as you expected.
Here's the approach I would take. Add a NewPath variable to your ConfigForm just like you have in Form1. Then, add a FormClosing method to FormConfig. Do something like this:
private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
{
NewPath = NewPathBox.Text;
}
Then, change your code on Form1 to this:
private void button1_Click(object sender, EventArgs e)
{
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.ShowDialog();
this.NewPath = cfgfrm.NewPath;
}
What this code is doing is creating and showing a new ConfigForm on your Form1 when you click button1. Then, when your user closes the FormConfig, the form saves the textbox value to the NewPath variable on the FormConfig. Then, once the form is closed, the code on Form1 resumes. Form1 then looks at the NewPath value that was saved when the user closed the FormConfig. Form1 grabs this new NewPath value and puts it in its own NewPath variable.
EDIT
To show/hide comboboxes:
private void button1_Click(object sender, EventArgs e)
{
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.ShowDialog();
this.NewPath = cfgfrm.NewPath;
Gamedropdown.Visible = false;
NewDropDown.Visible = true
}
I'm not sure why you are making the secondary form so complicated. You don't need a pointer to it after you use it and you should be closing instead of hiding. Try this:
class ConfigForm : Form
{
public string newPath = null;
public void CloseButton_Click(object sender, EventArgs e)
{
newPath = NewPathBox.Text;
}
public void CloseButton_Click(object sender, EventArgs e)
{
Close();
}
}
...and in your main form:
public partial class Form1 : Form
{
string steamPath = null; // set to starting path
private void ConfigureButton_Click(object sender, EventArgs e)
{
bool valueChanged = false;
using (ConfigForm form = new ConfigForm())
{
form.newPath = null;
form.ShowDialog();
if (form.newPath != null)
{
steamPath = form.newPath;
valueChanged = true;
}
}
if (valueChanged)
{
// here is where you would handle reloading and changing the ComboBoxes
}
}
}
This will more cleanly return the new string. Whatever you want to do as a result of having changed the path can be done after you have disposed of the config form (bringing it up with the "using" conditional automatically does the disposal for you)