diff --git a/src/mvAppItem.cpp b/src/mvAppItem.cpp index d15b15536..71ea7ea24 100644 --- a/src/mvAppItem.cpp +++ b/src/mvAppItem.cpp @@ -2666,6 +2666,7 @@ DearPyGui::GetEntityParser(mvAppItemType type) args.push_back({ mvPyDataType::StringList, "items", mvArgType::POSITIONAL_ARG, "()", "A tuple of items to be shown in the listbox. Can consist of any combination of types. All items will be displayed as strings." }); args.push_back({ mvPyDataType::String, "default_value", mvArgType::KEYWORD_ARG, "''", "String value of the item that will be selected by default." }); args.push_back({ mvPyDataType::Integer, "num_items", mvArgType::KEYWORD_ARG, "3", "Expands the height of the listbox to show specified number of items." }); + args.push_back({ mvPyDataType::Bool, "index_items", mvArgType::KEYWORD_ARG, "False", "Set the user_data to the index of the clicked item and not the item itself" }); setup.about = "Adds a listbox. If height is not large enough to show all items a scroll bar will appear."; break; diff --git a/src/mvBasicWidgets.cpp b/src/mvBasicWidgets.cpp index 2d395392c..761f5d0f6 100644 --- a/src/mvBasicWidgets.cpp +++ b/src/mvBasicWidgets.cpp @@ -341,6 +341,7 @@ DearPyGui::fill_configuration_dict(const mvListboxConfig& inConfig, PyObject* ou return; PyDict_SetItemString(outDict, "items", mvPyObject(ToPyList(inConfig.names))); PyDict_SetItemString(outDict, "num_items", mvPyObject(ToPyInt(inConfig.itemsHeight))); + PyDict_SetItemString(outDict, "index", mvPyObject(ToPyBool(inConfig.sendindex))); } void @@ -1172,6 +1173,7 @@ DearPyGui::set_configuration(PyObject* inDict, mvListboxConfig& outConfig, mvApp return; if (PyObject* item = PyDict_GetItemString(inDict, "num_items")) outConfig.itemsHeight = ToInt(item); + if (PyObject* item = PyDict_GetItemString(inDict, "index")) outConfig.sendindex = ToBool(item); if (PyObject* item = PyDict_GetItemString(inDict, "items")) { outConfig.names = ToStringVect(item); @@ -4743,9 +4745,16 @@ DearPyGui::draw_listbox(ImDrawList *drawlist, mvAppItem &item, mvListboxConfig & if (ImGui::ListBox(item.info.internalLabel.c_str(), item.config.enabled ? &config.index : &config.disabledindex, config.charNames.data(), (int)config.names.size(), config.itemsHeight)) { - *config.value = config.names[config.index]; - config.disabled_value = config.names[config.index]; - auto value = *config.value; + if (config.sendindex) { + *config.value = std::to_string(config.index); + config.disabled_value = std::to_string(config.index); + } + else { + *config.value = config.names[config.index]; + config.disabled_value = config.names[config.index]; + } + + auto value = *config.value; if(item.config.alias.empty()) mvSubmitCallback([&item, value]() { diff --git a/src/mvBasicWidgets.h b/src/mvBasicWidgets.h index b66a6da64..94649ade9 100644 --- a/src/mvBasicWidgets.h +++ b/src/mvBasicWidgets.h @@ -426,6 +426,7 @@ struct mvListboxConfig int disabledindex = 0; std::shared_ptr value = std::make_shared(""); std::string disabled_value; + bool sendindex = false; }; struct mvRadioButtonConfig