cant get my Value in Listview Virtualmode - c#

after clicking my value and pressing my OK_button I cant get the Value out of the listView to save it somewhere else. I cant use listView1.FindItemWithText because I don't have a text to search for.. Idk how to look for the clicked value after I pressed the OK_button
//Create dummy data to display
myData = new string[dataListSize];
for (int i = 0; i < dataListSize; i++)
{
myData[i] = String.Format("{0}", i);
}
}
private void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
e.Index = Array.FindIndex(myData, s => s == textBox1.Text.ToString());
}
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = new ListViewItem(myData[e.ItemIndex]);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
String MyString = textBox1.Text.ToString();
ListViewItem lvi = listView1.FindItemWithText(MyString.TrimEnd());
//Select the item found and scroll it into view.
if (lvi != null)
{
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(lvi.Index);
listView1.EnsureVisible(lvi.Index);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e){ }
private void OK_button_Click(object sender, EventArgs e)
{
try
{
// OK -> Daten übernehmen
int iCount = this.listView1.SelectedIndices.Count;
if (iCount != 1)
{
MessageBox.Show("Value is empty");
return;
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception)
{
//WriteProtokoll(ex.ToString(), 0);
Close();
}
}

I found out that I can get my value with:
string txt = listView1.FocusedItem.Text;

Related

Drag and drop image from a PictureBox to another PictureBox and check if is the target PictureBox is the correct one

I'm new to programming. I'm making an application for first-grade kids in Visual Studio 2019 WindowsForm and one level is about dragging a picture from a PictureBox in an empty PictureBox that has a label with a simple definition.
So if I have 2 PictureBox that show let say a dog and a chicken and 2 empty PictureBox (1 with label "bones" and one with label "grain"). I want to drag the dog picture into the PictureBox with the label "bones" and the chicken in the one with "grain" label and if correct I show a text "great job" and if not I show a text "try again".
I can drag the pictures but I can't find a method to check if is correct.
Can anybody help me with that?
Here is my code so far:
private void NivelulDoi_Load(object sender, EventArgs e)
{
customPictureBox1.AllowDrop = true;
customPictureBox2.AllowDrop = true;
customPictureBox3.AllowDrop = true;
}
private void customPictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
customPictureBox1.DoDragDrop(customPictureBox1.Image, DragDropEffects.Copy);
}
}
private void customPictureBox1_DragDrop(object sender, DragEventArgs e)
{
var data = e.Data.GetData(DataFormats.FileDrop);
if (data != null)
{
var fileNames = data as string[];
if (fileNames.Length > 0)
{
customPictureBox1.Image = Image.FromFile(fileNames[0]);
}
}
}
private void customPictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void customPictureBox2_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox2_DragDrop(object sender, DragEventArgs e)
{
customPictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
// figure out how to check if correct
/*if (customPictureBox2.Image == (Bitmap)e.Data.GetData(DataFormats.Bitmap, true))
{
label1.Text = "ai reusit";
}
else
{
label1.Text = "mai incearca";
}*/
}
private void customPictureBox3_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox3_DragDrop(object sender, DragEventArgs e)
{
customPictureBox3.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
private void customPictureBox4_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox4_DragDrop(object sender, DragEventArgs e)
{
customPictureBox4.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
private static void Dragging(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap) && (e.AllowedEffect & DragDropEffects.Copy) != 0)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}
Thank you.
A simple way is to define a variable to save the Name of "picturebox drag" and a dictionary to save "drag-drop" pair.
Here is the demo.
string pictureBoxFrom = "";
Dictionary<string, string> picDict = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
pictureBox2.AllowDrop = true;
pictureBox4.AllowDrop = true;
pictureBox1.MouseDown += pictureBox_MouseDown;
pictureBox3.MouseDown += pictureBox_MouseDown;
pictureBox2.DragEnter += pictureBox_DragEnter;
pictureBox2.DragDrop += pictureBox_DragDrop;
pictureBox4.DragEnter += pictureBox_DragEnter;
pictureBox4.DragDrop += pictureBox_DragDrop;
picDict.Add("pictureBox1", "pictureBox2"); // drag 1 to 2
picDict.Add("pictureBox3", "pictureBox4"); // drag 3 to 4
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
pictureBoxFrom = (sender as PictureBox).Name;
var img = (sender as PictureBox).Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move)
{
//(sender as PictureBox).Image = null;
}
}
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
private void pictureBox_DragDrop(object sender, DragEventArgs e)
{
if (picDict.ContainsKey(pictureBoxFrom) && picDict[pictureBoxFrom].Equals((sender as PictureBox).Name))
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
(sender as PictureBox).Image = bmp;
MessageBox.Show("great job");
}
else
{
MessageBox.Show("try again");
}
}

C# Datagridview: Ensure editing is done by hosted control only

I have a DataGridView with a DateTimePicker control hosted in one of the columns. Currently selecting a date assigns it to the value of the cell, but since I'm allowing the cell to be editable to utilize CellBeginEdit and CellEndEdit events, you can also type text into the column that may be invalid. How can I make it so editing the cell is enabled through the DateTimePicker only? See partial solution below:
DateTimePicker dtpVisit;
private void Form_Load(object sender, EventArgs e)
{
dtpVisit = new DateTimePicker();
dtpVisit.Format = DateTimePickerFormat.Short;
dtpVisit.Visible = false;
dtpVisit.Width = 100;
//DataGridView is called activities
activities.Controls.Add(dtpVisit);
dtpVisit.ValueChanged += this.dtpVisit_ValueChanged;
...
}
private void activities_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
if((activities.Focused) && (activities.CurrentCell.ColumnIndex == 5))
{
dtpVisit.Location = activities.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
dtpVisit.Visible = true;
dtpVisit.Focus();
if (activities.CurrentCell.Value != DBNull.Value)
{
dtpVisit.Value = (DateTime)activities.CurrentCell.Value;
}
else
{
dtpVisit.Value = DateTime.Today;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void activities_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if((activities.Focused) && (activities.CurrentCell.ColumnIndex == 5))
{
activities.CurrentCell.Value = dtpVisit.Value.Date;
dtpVisit.Visible = false;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dtpVisit_ValueChanged(object sender, EventArgs e)
{
activities.CurrentCell.Value = dtpVisit.Text;
}
private void activities_Scroll(object sender, ScrollEventArgs e)
{
dtpVisit.Visible = false;
}

Textbox autocomplete - input from custom keyboard on screen

I'm trying to use autocomplete. The input text comes from a custom made keyboard, made from a form.
I tried autocomplete feature from a simple textbox and text input from my keyboard and works fine. But when I input text from the custom keyboard, it doesn't work. The custom keyboard adds the input from a key listener Key_Click.
I tried adding an extra 'a' and adding the text as txtInput.Text += 'o'; but it didn't work.
Any ideas?
keyboard code:
public partial class frmTextInput : Form
{
public string input_Text { get; set; }
public frmTextInput(string TEXT,bool CTRL)
{
InitializeComponent();
AlternarTeclas(chkShift.Checked);
AgregarListenerTeclas();
var source = new AutoCompleteStringCollection();
List<string> box = Data.Data.SourcePatente();
foreach (var item in box)
{
source.Add(item);
}
txtInput.AutoCompleteCustomSource = source;
txtInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
private void btnSpace_Click(object sender, EventArgs e)
{
txtInput.Text = txtInput.Text + " ";
}
private void btnBorrar_Click(object sender, EventArgs e)
{
string str = txtInput.Text;
if (!string.IsNullOrEmpty(str))
{
txtInput.Text = str.TrimEnd(str[str.Length - 1]);
}
}
private void btnVolver_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnEnter_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
input_Text = txtInput.Text;
}
private void frmTextInput_Load(object sender, EventArgs e)
{
}
private void chkShift_CheckedChanged(object sender, EventArgs e)
{
AlternarTeclas(chkShift.Checked);
}
private void Key_Click(object sender, EventArgs e)
{
string key = sender.ToString();
if (chkShift.Checked)
{
key = key.ToUpper();
}
else
{
key = key.ToLower();
}
txtInput.Text = txtInput.Text + key.Substring(key.Length - 1);
}
private void AgregarListenerTeclas()
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length == 1 && c.Text != "←")
{
c.Click += Key_Click;
}
}
}
foreach (Control c in tabSymbol.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length == 1 && c.Text != "←")
{
c.Click += Key_Click;
}
}
}
}
private void AlternarTeclas(bool estaShiftApretado)
{
if (estaShiftApretado)
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length < 2)
{
c.Text = c.Text.ToUpper();
}
}
}
}
else
{
foreach (Control c in tabCaracteres.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Text.Length < 2)
{
c.Text = c.Text.ToLower();
}
}
}
}
}
private void btnSymbol_Click(object sender, EventArgs e)
{
tabTeclado.SelectTab(tabTeclado.SelectedIndex + 1);
}
private void btnTecAlfanumerico_Click(object sender, EventArgs e)
{
tabTeclado.SelectTab(tabTeclado.SelectedIndex - 1);
}
private void button1_Click(object sender, EventArgs e)
{
txtInput.Text += 'o';
}
}
txtInput.AutoCompleteCustomSource = source;
txtInput.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
This should be put in the designer file of your textbox, not here. Try that, and can you see your textbox text value change when you use the custom keyboard? What I understand is you catch click event for your custom keyboard and change txtInput.Text?
I made it work. First of all, it didn't work with textbox multiline.
Then, the correct way to input new chars was emulate the keyboard:
I was triying in function "Key_Click":
== txtInput.Text = txtInput.Text + key.Substring(key.Length - 1); ==> I don't work
Instead I used:
== txtInput.Focus(); // IMPORTANT
SendKeys.Send(key.Substring(key.Length - 1));

How to focus ListView's TextCell by button?

I need to focus TextCells one by one via a button click.
I tried listView.ScrollTo.
private void Button_Clicked_1(object sender, EventArgs e)
{
listViewJson.ItemTapped += ListViewJson_ItemTapped;
}
private void ListViewJson_ItemTapped(object sender, ItemTappedEventArgs e)
{
var focusing = e.Item;
listViewJson.ScrollTo(focusing, ScrollToPosition.MakeVisible, true);
}
Firstly, try to define an index to detect which cell to be selected. Then change the index via button click like:
int selectedIndex = 0;
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
when the TextCell is selected the ListView's ItemSelected event will fire, you can put your code in it like:
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
Here is my code behind for you referring to:
ObservableCollection<string> dataList = new ObservableCollection<string>();
int selectedIndex = 0;
public MainPage()
{
InitializeComponent();
for (int i=0; i<10; i++)
{
dataList.Add("item" + i);
}
myListView.ItemsSource = dataList;
myListView.ItemSelected += MyListView_ItemSelected;
MyBtn.Clicked += MyBtn_Clicked;
}
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}

Change Label's Text when Button is clicked

I am new to C#. I need the text of lblBalance to remain as it is when the btnNew is clicked, while it changes according to some calculatios when btnCalc is clicked. Here is my attempt so far.
FIGURED IT OUT, Thanks!
private void btnReset_Click(object sender, EventArgs e)
{
//Reset balance to 0.
balance = 0m;
lblBalance.Text = "";
tbDate.Text = "";
//Call the setupForm procedure.
setupForm();
}
private void setupForm()
{
//Setupform done once to reduce amount of times code must be entered.
//Code to clear these entries and set radio and checkboxes to false.
tbDate.Text = "";
tbAmount.Text = "";
rDeposit.Checked = false;
rWithdrawal.Checked = false;
rFee.Checked = false;
chkBank.Checked = false;
//Return focus to the date textbox
tbDate.Focus();
}
private void btnNew_Click(object sender, EventArgs e)
{
//Clear form, but retain balance when clicked.
setupForm();
}
private void tbDate_TextChanged(object sender, EventArgs e)
{
}
private void lblBalance_Click(object sender, EventArgs e)
{
}
private void btnCalc_Click(object sender, EventArgs e)
{
decimal Amount;
Amount = decimal.Parse(tbAmount.Text);
if ((rDeposit.Checked == true) && (chkBank.Checked == true))
{
Decimal.TryParse(lblBalance.Text, out balance);
lblBalance.Text = Convert.ToString(balance + Amount);
}
else if ((rWithdrawal.Checked == true) && (chkBank.Checked == true))
{
Decimal.TryParse(lblBalance.Text, out balance);
lblBalance.Text = Convert.ToString(balance - Amount);
}
else if ((rFee.Checked == true) && (chkBank.Checked == true))
{
Decimal.TryParse(lblBalance.Text, out balance);
lblBalance.Text = Convert.ToString(balance - Amount);
}
if ((rDeposit.Checked == false) && (rWithdrawal.Checked == false) && (rFee.Checked == false))
{
MessageBox.Show("ERROR: You must select Deposit, Withdrawal, or Service Fee.");
}
}
private void rDeposit_CheckedChanged(object sender, EventArgs e)
{
}
}
}
change:
lblBalance.Text += balance.ToString();
to
lblBalance.Text = balance.ToString();
Inside your btnNew_Click event

Categories