Open another page (not web) on C# - c#

I'm creating a native C# application and I need to do a simple thing:
Once the user clicks some certain button, another .cs file is opened (with its own design, code and stuff). If it is possible, I would like to know how to close the current form at the same time.
EDIT: what I exactly need:
namespace Mokesciai
{
public partial class Mokesciai : Form
{
public Mokesciai()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//write code here to open another page called "NewPage.cs" with its own subfiles "NewPage.Designer.cs" and
//"NewPage.resx", as shown in the solution explorer
}
}
}
The application is C# Windows application
EDIT2: what I want in the graphical way: http://sdrv.ms/JXKVEL
By clicking "Click me" I want to open the new form

private void button1_Click(object sender, EventArgs e)
{
YourSecondForm objForm=new YourSecondForm();
objForm.Show();
this.Close();
}
Assuming YourSecondForm is the name of your another form which you want to display on the button click event.

That is a form.
You can create a new instance of the form class, then call Show().

As far as i understand from your question, may be you want to do this. You can use Process.Start() to start any other application from your native app.
using System.Diagnostics;
string path=#"path to the app"
Process.Start(path);
OR
Create a new form place a multi line text box, then read the file using StreamReader & fill its result on the text box. For more information on how to use Stream Reader Check out this or this

Related

How do I use data from a combo box in another form c#

I'm writing a program in c# (visual studio, windows forms) which involves the user selecting a name from a combo box, and then clicking a button which brings them to a quiz on another form. I want a text file to be created at the end of the quiz showing the name selected and the quiz results, followed by a "~" symbol.
I honestly don't know where to start. I'm using stream reader.
This is the code I used for the combo box and the button that sends you to the next form
private void quizSelectPupilCB_SelectedIndexChanged(object sender, EventArgs e)
{
selectedClass = quizSelectPupilCB.SelectedItem.ToString();
}
private void quizStartBTN_Click(object sender, EventArgs e)
{
Quiz2 formQuiz2 = new Quiz2();
formQuiz2.Show();
this.Hide();
}
How do I use this data on another form?
I don't even know where to start. I tried looking up things like "how to access data from one form and put it in another c#" and similar things but everything I found was either not what I was looking for, or worded in a really confusing way.
As a variant you can save selectedItem to String/StringCollection, then to app settings(Settings.Default). And on another form get this value back.

How to refresh a user control every time a button is clicked

I have a series of menu options which are all individual user controls on a windows form application.
How do I refresh the user control so that if for example I added a new person to my txt file, when I click the Birthdays button, it preforms all the functions within the Birthday User control again on the file with the new person added.
What's happening now is when I add a new person to my txt file, The user controls don't refresh therefore the Data.updatedata() method isn't called and the data is not updated.
Is there a particular event or method that I could use in order to refresh the user control when clicked?
I have tried using birthdayUserControl1.refresh() in the main form
namespace Project
{
public partial class ChildrenUi : Form
{
public ChildrenUi()
{
InitializeComponent();
homeUserControl1.BringToFront();
}
private void button2_Click(object sender, EventArgs e)
{
birthdaysUserControl1.Refresh();
birthdaysUserControl1.BringToFront();
}
}
}
I have only just started learning about Winforms and came across Data Binding using XAML/XML files on similar questions regarding refreshing user controls however I don't know much about XAML/XML and I would imagine i'd have to redesign a good portion of my project to facilitate that. I'm using a text file.
Refreshing whole birthdaysUserControl1 won't refresh inner ListBox datasource, you need to manually refresh it.
private void button2_Click(object sender, EventArgs e)
{
birthdaysUserControl1.RefreshList();
}
And inside birthdaysUserControl1:
public void RefreshList()
{
listbox1.DataSource=null;
listbox1.DataSource=UpcominBdays;
}
To watch the contents of your textfile, you can use the System.IO.FileSystemWatcher class. The Changed-Event informs your application whenever the content of the watched file is changed.

Displaying a panel in another windows form on button click c#

I'm working on a c# program and I want a panel to appear on a form when a button is clicked in another. So when the add button is clicked on form2 the panel requesting the details for this to be possible will be displayed on form 1.
I currently have a static method set up in form1 which can be accessed from form2 - however due to panel.Show() being non static it won't allow me to use this in the function.
In Form1 I have:
public static void showPanel()
{
panel.Show()
}
In my second form I have the following:
private void btn_add_Click(object sender, EventArgs e)
{
form1.showPanel();
this.Hide();
}
I have tested with just having the static function show a message box which works. Is it possible to do it the way I want or do I need to take a few steps back and try a different technique?
Are we talking about a new instance of the second form if so you can try to instantiate the new form using:
Form newForm = new YourFormName(potential parameters);
newForm.showPanel();
newForm.Show();
If you want to execute the form on an open form you can give the first form a reference(field with instance) of the second form. Or you can try using: Application.OpenForms. if you give it [1] it'll give you the second open form probably. You can also use .OfType to get the correct form in case your form order isn't always the same.

How to close a form in UserControl

I created a UserControl with the buttons Save, Close and Cancel. I want to close the form without saving on the Cancel button, prompt a message to save on the Close button and Save without closing on the Save button. Normally, I would have used this.Close() on the Cancel button, but the UserControl doesn't have such an option. So I guess I have to set a property for that.
Scrolling down the "Questions that may already have your answer" section, I came across this question: How to close a ChildWindow from an UserControl button loaded inside it? I used the following C# code:
private void btnCancel_Click(object sender, EventArgs e)
{
ProjectInfo infoScreen = (ProjectInfo)this.Parent;
infoScreen.Close();
}
This does the job for one screen, but I wonder if I have to apply this code for all the screen I have? I think there should be a more efficient way. So my question is: Do I need to apply this code for every form I have, or is there another (more efficient) way?
you can use
((Form)this.TopLevelControl).Close();
you can use the FindForm method available for any control:
private void btnCancel_Click(object sender, EventArgs e)
{
Form tmp = this.FindForm();
tmp.Close();
tmp.Dispose();
}
Do not forget to Dispose the form to release resources.
Hope this helps.
You also can close one form in any part of the code using a remote thread:
MyNamespace.MyForm FormThread = (MyNamespace.MyForm)Application.OpenForms["MyForm"];
FormThread.Close();
I found the simple answer :) I all ready thought of something like that.
To close a WinForm in a ButtonClicked Event inside a UserControl use the following code:
private void btnCancel_Click(object sender, EventArgs e)
{
Form someForm = (Form)this.Parent;
someForm.Close();
}

C# Intercept Browse Button

Have a unique customer request which Im unsure how to tackle.
The customer has a webpage form with a browse button to select a file. When the browse button is clicked, instead of showing the local files, they want to pop-up a window with a textbox to enter a code. This code is then used to select a file from a local folder containing 1000 files each with their own code. They want to prevent the user from viewing the other files in that folder.
I did write a custom Windows form to mimic the webpage form but they already have the webpage online and would like to reuse it.
Any ideas how to intercept the browse button? I can use a C# Application with the web browser component, but can that intercept the browse button?
The only option that I can see working is using a C# Application with the web browser component. You can then use WebBrowser.ObjectForScripting to provide a method that can be called to trigger your custom picker window through Javscript, e.g:
window.external.ShowPickerWindow();
You then have two options:
Interrogate the DOM of the page once it's loaded and replace the button with one that triggers your picker window.
Have the customer change their page so it checks for the existance of a window.external.ShowPickerWindow method and basically does option (1) for you.
You can then have a method, perhaps called window.external.GetPickedCode() to pull the code out in the page.
Rob kinder steered me along the correct thinking track by saying "replace the button" which has lead me to a solution which works beautifully!
In short, I hide the browse button, insert a new button next to it that when clicked, opens a new window with a textbox. This textbox then sets a string value in the parent form which is used onSubmit to attach the file.
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement btnBrowse = wb.Document.GetElementById("fiPhoto");
if (btnBrowse != null)
{
HtmlElement newbtn = wb.Document.CreateElement("input");
newbtn.SetAttribute("id", "btnLoad");
newbtn.SetAttribute("type", "button");
newbtn.SetAttribute("value", "Load");
newbtn.Click += new HtmlElementEventHandler(newbtn_Click);
btnBrowse.Parent.AppendChild(newbtn);
btnBrowse.Style = "display:none";
}
HtmlElementCollection forms = wb.Document.Forms;
if (forms.Count > 0)
{
HtmlElement form = wb.Document.Forms[0];
form.AttachEventHandler("onsubmit", delegate(object o, EventArgs arg)
{
FormToMultipartPostData postData = new FormToMultipartPostData(wb, form);
postData.AddFile("photo", photo);
postData.Submit();
});
}
}
private void newbtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.ShowDialog();
}
FormToMultipartPostData is too big to post in here but it basically manually constructs the Content-Disposition to be posted
Don't show the actual file browser, imitate one which is showing only that one file in in.
Or since you know the file path when correct code is entered copy the file to temp folder you created and open file browser to browse that folder and it will be contain only that file.

Categories

Resources