Reputation: 1337
I'm trying to pass file in FileChooser in Gtk# and them using second button read from this file. I can't pass FileChooser to function, which is triggered when second button is clicked.
namespace SharpTest{
internal static class SharpTest{
public static void Main(string[] args){
Application.Init();
var window = new Window("Sharp Test");
window.Resize(600, 400);
window.DeleteEvent += Window_Delete;
var fileButton = new FileChooserButton("Choose file", FileChooserAction.Open);
var scanButton = new Button("Scan file");
scanButton.SetSizeRequest(100, 50);
scanButton.Clicked += ScanFile;
var boxMain = new VBox();
boxMain.PackStart(fileButton, false, false, 5);
boxMain.PackStart(scanButton, false, false, 100);
window.Add(boxMain);
window.ShowAll();
Application.Run();
}
private static void Window_Delete(object o, DeleteEventArgs args){
Application.Quit ();
args.RetVal = true;
}
private static void ScanFile(object sender, EventArgs eventArgs){
//Read from file
}
}
}
Upvotes: 1
Views: 376
Reputation: 12212
The FileName property in FileChooserButton scanButton holds the name of the file chosen. The problem you face is that you cannot access the button from ScanFile(), since it is outside Main(), and scanButton is a local reference inside it.
Also, you are using an old-style fashion of creating event handlers. You can actually use lambdas for that purpose (the easiest option), and modify the parameters in the call to ScanFile() the way you like.
So, instead of:
scanButton.Clicked += ScanFile;
you can change it for:
scanButton.Clicked += (obj, evt) => ScanFile( fileButton.Filename );
which will do the trick, provided that you change ScanFile() to be:
private static void ScanFile(string fn)
{
// ... do something with the file name in fn...
}
With that lambda, you are creating an anonymous function that accepts an object obj (the sender of the event), and an EventArgs args object (the arguments of the event). You are doing nothing with that info, so you dismiss it, since you're just interested in the value of the FileName property in scanButton.
Hope this helps.
Upvotes: 1