Dendei
Dendei

Reputation: 571

Wix populate listbox

I am trying to fill in a ListBox with CustomAction and it's not going well. I try to figure out the session.Database.Tables but have no idea how to start.

I've created a listbox like this

<Control Id="ListBox1" Type="ListBox" Sorted="no"  Indirect="no" Property="LISTBOXVALUESONE" X="10" Y="50" Width="150" Height="180">
      <ListBox Property="LISTBOXVALUESONE">
        <ListItem Text="ARGHH!" Value="1"/>
      </ListBox>
    </Control>

But I cant see the property in my verbrose log or anything about an table so I guess I have to create an table in customAction and populate it? I see my ARGHH! in the list so it should exsist but how do I access the values? And add new ones?

Found more examples and stuff in C++ but i would like to make the CustomAction in C#

EDIT

Database db = session.Database;
string sqlInsertTemp = db.Tables["ListBox"].SqlInsertString + " TEMPORARY";
View view = db.OpenView(sqlInsertTemp );
view.Execute( new Record( new object[] { "LISTBOXVALUESONE", 2, "2", "One" } ));
view.Close();

Thanks to Christopher I got it to work with adding an value.

db.Tables["ListBox"] should remain the same and name the type not the id as i taught And on this line view.Execute( new Record( new object[] { "LISTBOXVALUESONE", 2, "2", "One" } )); you put your Listbox Property and then the placement of the value "one" we insert

The two "2"s is what I figure the placement we want it on and I already have an test value on 1 my "ARGHH!" so I put the new on 2 and dont know the details but...

I got an Table Update error and, one dublicate value error if i put 2,1 or 1,2 in the customaction!

Upvotes: 0

Views: 3808

Answers (2)

forest
forest

Reputation: 1

Add one record to list box:

private void AddRecordToListBox(string listBoxPropertyName, int index, string text, string value)
{
    View view = session.Database.OpenView("SELECT * FROM ListBox");
    view.Execute();

    Record record = session.Database.CreateRecord(4);

    record.SetString(1, listBoxPropertyName);
    record.SetInteger(2, index);
    record.SetString(3, value);
    record.SetString(4, text);

    view.Modify(ViewModifyMode.InsertTemporary, record);
    view.Close();
}

Fill ListBox:

private void FillListBox()
{
    var dict = SomeDict();

    int index = 1;
    foreach (var element in dict)
    {
        AddRecordToListBox(ListBoxName, index, element.Key, element.Value);
        index++;
    }
}

Clear ListBox

private void ClearListBox(string listBoxPropertyName)
{
    var command = String.Format("DELETE FROM ListBox WHERE ListBox.Property='{0}'", listBoxPropertyName);
    View view = session.Database.OpenView(command);
    view.Execute();
    view.Close();
}

Upvotes: 0

Christopher Painter
Christopher Painter

Reputation: 55591

I wrote a blog article about 5 years ago that might help you:

How DTF is going to help me become a better .NET Developer

You want to make sure your built MSI has a ListBox table otherwise the SQL won't work when it tries to generate the temp rows dynamically at runtime. If the ListBox element doesn't do this for you, the EnsureTable element will.

The actual C# looks something like:

Database db = session.Database;
string sqlInsertTemp = db.Tables["ListBox"].SqlInsertString + " TEMPORARY";
View view = db.OpenView(sqlInsertTemp );
view.Execute( new Record( new object[] { "TESTPROP", 1, "1", "One" } ));
view.Close();

Note this is an old code example and doesn't properly take advantage of using statements and IDisposable.

Upvotes: 3

Related Questions