|
I have researched this for two days and have been unable to find an answer. I'm certain I'm not the only person that wants to display more than one item in the mainform listbox of NAudio so hopefully someone here can tell me what I'm doing wrong. I need to output the results of an array to my listbox. If I use the below code then everything outputs but on a single line, instead of separate ones as I need the user to be able to select the results. I'm assuming that this is because the results are passed over in a single string despite the foreach loop.
mainform.cs
[ImportingConstructor]
public MainForm([ImportMany] IEnumerable<AudioPlugin> content)
{
InitializeComponent();
listBox.DisplayMember = "Name";
foreach (var listing in content)
{
listBox.Items.Add(listing);
}
}
recordingpanel.cs
[Export(typeof(INAudioPlugin))]
public class RecordingPanelPlugin : AudioPlugin
{
private string _customer { get; set; }
public void ConnectionString()
{
using (var conn = new SqlCeConnection("Data Source=MyDatabase.sdf;Password=pass;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT * FROM main", conn);
SqlCeDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
_customer += (string)(reader["CustomerName"]);
}
}
}
public string Name
{
get
{
ConnectionString();
return _customer;
}
}
public Control CreatePanel()
{
return new RecordingPanel();
}
}
I also tried the above using a struct, List and array with the same results each time. I'm going on the assumption that this INAudioPlugin is changing the data type during ImportingConstructor. So I tried to use the query directly in the mainform.cs. I was able to get the listbox appearing correctly but I cannot get the listbox_SelectedIndexChanged to function propery as that fails at the following line since the listbox contents are string and not INAudioPlugin it looks like. I can't explicitly convert the type either.
var plugin = (INAudioPlugin)listbox.SelectedItem;
I am completely out of ideas at this point, I hope someone can offer me a solution. I have a feeling the second route I tried is going to be where I need to go with this but I can't figure out how to call my recordingpanel.cs if I do so because of the plugin error above. Below is the code I used for the second route I tried.
mainform.cs
[ImportingConstructor]
public MainForm([ImportMany] IEnumerable<INAudioPlugin> content)
{
InitializeComponent();
using (var conn = new SqlCeConnection("Data Source=MyDatabase.sdf;Password=pass;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT * FROM main", conn);
SqlCeDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
_customer = (string)(reader["CustomerName"]);
listbox.Items.Add(_customer);
}
}
}
private INAudioPlugin currentPlugin;
private void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
var plugin = (INAudioPlugin)listbox.SelectedItem;
if (plugin != currentPlugin)
{
this.currentPlugin = plugin;
DisposeCurrentListing();
var control = plugin.CreatePanel();
control.Dock = DockStyle.Fill;
contentPanel.Controls.Add(control);
}
}
|
|
|
|
|
Caveat: I haven't used NAudio. These suggestions are based on the apparent use of MEF for the plugins. I have not tried any of this. (I am not an MEF guru.)
First: some things that seem clearly wrong.
RecordingPanelPlugin.ConnectionString() (besides having a very misleading name) collects all of the CustomerName column values into a single string. There's nothing keeping them as individual values.
The RecordingPanelPlugin.Name property just returns all of the names concatenated together.
MEF is Importing and creating a single instance of your RecordingPanelPlugin .
You seem to need a "parent" class which creates all of the instances of the RecordingPanelPlugin you want.
Create an interface that MEF can use to instantiate the class that creates the individual instances of RecordingPanelPlugin :
public interface IPluginFactory
{
List<INAudioPlugin> GetAudioPlugins();
}
Simplify the class RecordingPanelPlugin :
[PartNotDiscoverable]
[Export(typeof(INAudioPlugin))]
public class RecordingPanelPlugin : AudioPlugin
{
public RecordingPanelPlugin (string name)
{
this.Name = name;
}
public string Name { get; private set; }
public Control CreatePanel()
{
return new RecordingPanel();
}
}
Then create a class which implements IPluginFactory for the RecordingPanelPlugin :
[Export(typeof(IPluginFactory))]
public class RecordingPanelPluginFactory
{
private public List<INAudioPlugin> AllRecordingPanelPlugins;
public List<INAudioPlugin> GetAudioPlugins()
{
if (AllRecordingPanelPlugins == null)
{
AllRecordingPanelPlugins = new List<INAudioPlugin>();
using (var conn = new SqlCeConnection("Data Source=MyDatabase.sdf;Password=pass;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT * FROM main", conn);
SqlCeDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
AllRecordingPanelPlugins.Add(new RecordingPanelPlugin((string)(reader["CustomerName"])));
}
}
}
return AllRecordingPanelPlugins;
}
}
Now change the MainForm to have MEF give it all of the IPluginFactory s:
[ImportingConstructor]
public MainForm([ImportMany] IEnumerable<IPluginFactory> factories)
{
InitializeComponent();
listBox.DisplayMember = "Name";
foreach (var factory in factories)
{
foreach (var plugin in factory.GetAudioPlugins())
{
listBox.Items.Add(plugin);
}
}
}
Good luck!
(Remember my caveat above! )
A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
|
|
|
|
|
That option didn't work either as the plugin was expecting a different variable type and I wasn't able to implicitly or explicitly convert. So, I decided after almost three days, it was time to rethink my logic. What I decided to do was put my query in my mainform which then displayed the listbox correctly. Then I modified my SelectedIndexChanged event in my mainform so instead of referencing the plugin directly I'm passing the listbox variable to the recordingpanel.cs instead. Everything is working great as expected using my new logic.
Thanks for your response and suggestion though.
|
|
|
|
|
how can I create circular shape button? plz reply
|
|
|
|
|
What technology? WPF? ASP.NET? Windows Forms?
|
|
|
|
|
|
why cant you use an image and click can put click events for that..
more over i think for the circle shaped button we need a lot of discussion about the libraries
|
|
|
|
|
Well, I would normally use the power of WPF to do this. The person who asked the question, however, has no idea that you replied.
|
|
|
|
|
you mean my answer should be elaborated??
|
|
|
|
|
I mean you should give your answer to the person who actually asked the question.
|
|
|
|
|
common friend here we post this to be seen by everyone..
|
|
|
|
|
We do. However, as you are posting against a question that someone has asked, the polite thing to do is to reply to them so that they get notified that there is an answer. By replying to me, I see the notification, but the person who asked the question doesn't.
|
|
|
|
|
OK ok my friend sorry for u and for not replying to him
|
|
|
|
|
Google is your friend; be nice and visit him often...
There are a huge number of examples out there of round buttons, some good, some bad.
A quick google restricted to just this site found nearly 4000 examples: round button c# site:codeproject.com[^]
Try this: Custom Button Control with Gradient Colors and Extra Image (VB.NET)[^] It's VB, but you can just use the code in a DLL.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
how to disable combobox if another combobox is selected in c# windows form application
diviya murali
|
|
|
|
|
The simplistic answer would be to set the Enabled state of the ComboBox from the other ComboBox by handling the GotFocus and LostFocus events on that ComboBox .
|
|
|
|
|
You can write it in Select_Index_Change of First DropDown
EX:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Enabled = false;
}
|
|
|
|
|
That doesn't solve the OP's problem.
|
|
|
|
|
As Pete said, it does not help the OP with his problem. He wants to disable the combo box when it is selected, not when an item is selected...
"I've seen more information on a frickin' sticky note!" - Dave Kreskowiak
|
|
|
|
|
Note the OP is intending to do this for c# windows form application and not for ASP.NET Applications.
|
|
|
|
|
you can use the Click event of the second combobox to enable/disable the first combobox
private void comboBox2_Click(object sender, EventArgs e)
{
comboBox1.Enabled = false;
}
http://freaksofdeveloper.blogspot.co.at/
|
|
|
|
|
Is this event fired if the user tabs into the combobox?
|
|
|
|
|
The simplistic answer would be to set the Enabled state of the ComboBox from the other ComboBox by handling the GotFocus and LostFocus events on that ComboBox.
|
|
|
|
|
|
It's a good answer. Take you long to come up with it?
|
|
|
|