Jet Jet - 2 months ago 9
C# Question

How to declare string with value from other form

What I am trying to do is get the value of

uid
from
MainForm
and declare it to
level
in Form1.

MainForm

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SOF_Op_Center
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
error.Visible = false;
}
void loginClick(object sender, EventArgs e)
{
if (agent.Text == "user" && aid.Text == "pass")
{
this.Hide();
Form1 frm = new Form1();
frm.Show();
}
else if (agent.Text == "user2" && aid.Text == "pass2")
{
this.Hide();
Form1 frm = new Form1();
frm.Show();
}
else
{
error.Visible = true;
}

}
}
}


Form1

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace SOF_Op_Center
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string server = "server";
public string user = "user";
public string pwd = "pass";
private void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + server + "/public_html/data/" + fileInf.Name;
FtpWebRequest reqFTP;

// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + server + "/public_html/intel/" + fileInf.Name));

// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(user, pwd);

// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;

// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// Specify the data transfer type.
reqFTP.UseBinary = true;

// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;

// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();

try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();

// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);

// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}

// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error uploading data.");
}
}
private void Download(string filePath, string fileName)
{
FtpWebRequest reqFTP;
try
{
//filePath = <<The full path where the file is to be created.>>,
//fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>>
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + server + "/public_html/intel/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(user, pwd);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}

ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + server + "/public_html/intel/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(user, pwd);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
//MessageBox.Show(reader.ReadToEnd());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
//MessageBox.Show(response.StatusDescription);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
void UploadClick(object sender, EventArgs e)
{
OpenFileDialog opFilDlg = new OpenFileDialog();
if (opFilDlg.ShowDialog() == DialogResult.OK)
{
Upload(opFilDlg.FileName);
}
}
void DownloadClick(object sender, EventArgs e)
{
FolderBrowserDialog fldDlg = new FolderBrowserDialog();
if (txtUpload.Text.Trim().Length > 0)
{
if (fldDlg.ShowDialog() == DialogResult.OK)
{
Download(fldDlg.SelectedPath, txtUpload.Text.Trim());
}
}
else
{
MessageBox.Show("Please enter the File name to download");
}
}
void ShowIntelClick(object sender, EventArgs e)
{
string[] filenames = GetFileList();
lstFiles.Items.Clear();
foreach (string filename in filenames)
{
lstFiles.Items.Add(filename);
}
}
}
}
//}

Answer

You can define the constructor for Form1 to take a string and then pass uid as this value when this in MainForm:

Form1:

public partial class Form1 : Form
{
    string Level;

    public Form1(string uid)
    {
        Level = uid;
        InitializeComponent();
    }
}

MainForm:

public partial class MainForm : Form
{
    string uid = "UID";

    public MainForm()
    {
        InitializeComponent();
    }
    void loginClick(object sender, EventArgs e)
    {
        var frm = new Form1(uid);
        this.Hide();
        frm.Show();
    }
}
Comments