Is there a way in which I can save details from a ListView that doesnt require me to use the save dialog box everytime and allows me to call it within a certain time span. So 'save' rather than 'save as' everytime.
You can use a DispatchTimer with a callback to a method to perform your save.
DispatcherTimer autosaveTimer = new DispatcherTimer(TimeSpan.FromSeconds(autosaveInterval), DispatcherPriority.Background, new EventHandler(DoAutoSave), Application.Current.Dispatcher);
private void DoAutoSave(object sender, EventArgs e)
{
// Enter save logic here...
}
Related
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.
I have a button on this Winforms app that when clicked, displays an HTML file in the browser module.
I need the button to display one file when it is clicked the first time and a different file any other time after that.
Here's the code that makes it open the first file:
private void button6_Click(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/mail/index.html", curDir));
}
Currently it displays file:///{0}/mail/index.html every time it is clicked. I want it to display file:///{0}/mail/index2.html for the second and every other time it is clicked. How would I go about structuring this? is there an event that only responds to the first action on a piece of code? I've looked online for a while and can't seem to find anything specific to this problem.
There is no event that only responds to the first action (you could theoretically create it yourself though). But it is much simpler to have just one Click event and inside it's event handler decide, which file to show - depending on value of boolean variable that "remembers" if it is first button click or not. Something like this:
// Private filed of form that keeps track of whether button was already clicked before or not
private bool IsFirstButtonClick = true;
private void button6_Click(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
// Display appropriate file depending on whether it is first time button click or not
if(IsFirstButtonClick)
{
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/mail/index.html", curDir));
// Set flag so that next time button is clicked, we know it was alredy clicked (at least once) before
IsFirstButtonClick = false;
}
else
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/mail/index2.html", curDir));
}
I'm working on a project and I'm in a situation where the administrator needs to accept new users into the system. I've got a form that only admins can access, which shows a list of all the waiting applicants. I've found out how to create buttons at run time and how to add an event handler for the click event, but the handler requires a method by the same name to run.
Obviously I can't just put code for a method inside a for loop, unless I'm mistaken. How would I give the program the ability to support an potentially infinite amount of applicants?
void AcceptUsersAdminLoad(object sender, EventArgs e)
{
//FOR LOOP - To be finished. Will read an xml file to find out # to loop.
Button newButton = new Button();
newButton.Click += new System.EventHandler(newButtonClick);
newButton.Text = "Accept";
Panel1.Controls.Add(newButton);
}
private void newButtonClick (Object sender, System.EventArgs e){
}
This works, but as I've said, only for one button. As relatively painless as it would be to copy the method and append it's name with a number a hundred times, I'd prefer to find a way with support for more.
You can use that same method for all of your buttons! The sender parameter will tell you which button is the source, simply cast it to a button. You can store an ID of some sort in the .Tag() property of the button so you know who you are working with (when you create them, assign it).
private void newButtonClick (Object sender, System.EventArgs e){
Button btn = (Button)sender;
// ... do something with "btn" in here ...
}
Answer to the titular question: You don't create methods in a loop. You will occasionally create anonymous methods in a loop, but save that for later :).
To do what you want though: When you generate these buttons, they should all be pointing to the same event handler. The logic you want to run is the same, but the data is different.
How you get the data to the function is not trivial, one (hackish) way to do it is to store the related object (or its index) in the Tag property of the button, which you can then retrieve via the sender argument of the event handler.
Assuming program logic changes a button tag based on something random - but using the UI thread.
Is the Button Tag value reliable to use in a click event? i.e. Will it be the same value as-at the time of the event handler as it was at the time of the click?
If not, what is the best method to pass an event specific parameter into a button click event that will be safe?
Update
Added an example as requested. (Remember this is just theoretical).
Windows.Forms.Timer timer = new Timer();
timer.Interval = 1;
timer.Tick += new EventHandler(timer_tick);
timer.Start();
void timer_tick(object sender, EventArgs e)
{
this.button.Tag = Random.NextInt(100).ToString();
}
void button_click(object sender, EventArgs e)
{
string s = (string)((Button)sender).Tag;
Console.WriteLine("Tag value as at button push: " + s);
}
Put another way, the question boils down to: can events be wedged into the GUI event queue that allow the state of the button to be changed between the button being pushed and the click event handling the push?
Assuming that you are using winforms.
IMHO you can use Tag property to pass control specific parameters but you must also remember that windows forms controls are not thread safe. To make a thread-safe call, you must use InvokeRequired. The following link gives an example to set text for TextBox in a thread-safe way but it should not be very different for Tag property.
http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx
Hope this helps.
I am trying to determine if my application is closed through clicking the "X" on the windows form, or if they clicked an "Exit" button I have on it. Right now I am using StackTrace.GetFrame(someIndex) to determine how, but i am looking for a more definitive way since it looks like these frame orders arent guaranteed. Is there a better way to make the distinction? This is a .NET 3.5 WinForm, and Im writing in C#.
Use a different event to handle your own "Exit" button click. In your own "Exit" event handler do your extra logic, or set some state variable, and then call the normal application close method.
Post some samples of how your events are wired up and I get give a more specific example. In general it would look something like this:
private void btnMyExit_Click(object sender, EventArgs e)
{
// TODO: add any special logic you want to execute when they click your own "Exit" button
doCustomExitWork();
}
public static void OnAppExit(object sender, EventArgs e)
{
doCustomExitWork();
}
private void doCustomExitWork()
{
// TODO: add any logic you want to always do when exiting the app, omit this whole method if you don't need it
}
Use the FormClosing event and query the FormClosingEventArgs for the enum CloseReason value.