using like operator in if operator in C# - c#-4.0

I have a textbox for search (that is, textBox1)
A user, for example, enters "aba" in textBox1.
"abandon" puts in datagridiew1.
The user clicks on datagriview1:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
richTextBox_MWE.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
if ("richTextBox_MWE.Text like '%" + textBox1.Text + "%'")
{
label5.BackColor = Color.Green;
}
}
I want if "abandon" is such as "aba" in textBox1, label5.BackColor becomes Green.

Simple way is use the textBox1(where actually filter content going to change) change event
if(!String.IsNullOrEmpty(richTextBox_MWE.Text) && richTextBox_MWE.Text.Trim().Contains(textBox1.Text.Trim()))
{
label5.BackColor = Color.Green;
}

You want to use some kind of mix of C# and sql :) You can use the String.Contains method to achieve what you want.
if(richTextBox_MWE.Text != null
&& richTextBox_MWE.Text.Contains(textBox1.Text.Trim())
{
...
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
richTextBox_MWE.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
if (!String.IsNullOrEmpty(richTextBox_MWE.Text) && !String.IsNullOrEmpty(textBox1.Text) && richTextBox_MWE.Text.Contains(textBox1.Text.Trim()))
{
label5.BackColor = Color.Green;
}
}
Here the contains will accept the value to be searched.

See snippet from my code. The txtProductCode is a text box that user is filling the product_code for searching in the list view.
string tmpProductCode = txtProductCode.Text.Trim();
string tmpProductCodePattern = "^" + Regex.Escape(tmpProductCode).Replace("%", ".*") + "$";
In my loop of product_code(s) the prodCode will contain the product_code value for each loop.
productCodeClause = false;
if (tmpProductCode.Equals(""))
{
productCodeClause = true;
}
else
{
if (Regex.IsMatch(prodCode, tmpProductCodePattern))
{
productCodeClause = true;
}
}
I hope this will be helpful.

Related

Desperately need Xamarin.IOS modal MessageBox like popup

Coding in Xamarin IOS. I have a drop down list type popup, where, if The end user types in a new value, I want to ask a yes/no question: Do You want to add a new row?
The control is inside a UIStackView, which is inside a container UIView, which is in turn inside another which is presented via segue. Xamarin demanded a UIPopoverController, which I implemented. Here is The code I have so far:
using System.Threading.Tasks;
using Foundation;
using UIKit;
namespace PTPED_Engine
{
public enum MessagePopupType
{
YesNo = 1,
OKCancel = 2,
OKOnly = 3
}
public enum PopupResultType
{
OK = 1,
Cancel = 2,
Yes = 3,
No = 4
}
public static class AlertPopups
{
static NSObject nsObject;
public static void Initialize(NSObject nsObject)
{
AlertPopups.nsObject = nsObject;
}
public static Task<PopupResultType> AskUser(UIViewController parent, UIView V, string strTitle, string strMsg, MessagePopupType mpt)
{
using (UIPopoverController pc = new UIPopoverController(parent))
{
// pc.ContentViewController
// method to show an OK/Cancel dialog box and return true for OK, or false for cancel
var taskCompletionSource = new TaskCompletionSource<PopupResultType>();
var alert = UIAlertController.Create(strTitle, strMsg, UIAlertControllerStyle.ActionSheet);
// set up button event handlers
if (mpt == MessagePopupType.OKCancel)
{
alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.OK)));
alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.Cancel)));
}
if (mpt == MessagePopupType.YesNo)
{
alert.AddAction(UIAlertAction.Create("Yes", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.Yes)));
alert.AddAction(UIAlertAction.Create("No", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.No)));
}
if (mpt == MessagePopupType.OKOnly)
{
alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult(PopupResultType.OK)));
}
// show it
nsObject.InvokeOnMainThread(() =>
{
pc.PresentFromRect(V.Bounds, V, UIPopoverArrowDirection.Any, true);
});
return taskCompletionSource.Task;
}
}
}
}
and I invoke it as follows:
LookupCombo.Completed += async (object sender, CompletedEventArgs e) =>
{
C1AutoComplete AC = (C1AutoComplete)sender;
if (AC.Text.Trim() != "")
{
string sColName = AC.AccessibilityIdentifier.Trim();
var ValuesVC = (List<Lookupcombo_Entry>)AC.ItemsSource;
var IsThisAHit = from Lookupcombo_Entry in ValuesVC
where Lookupcombo_Entry.sDispVal.ToUpper().Trim() == e.value.ToUpper().Trim()
select Lookupcombo_Entry.sMapVal;
if (!IsThisAHit.Any())
{
string sTitle = "";
string sFull = _RM.GetString(sColName);
if (sFull == null) { sFull = "???-" + sColName.Trim(); }
sTitle = " Add New " + sFull.Trim() + "?";
string sPPrompt = "Do you want to add a new " + sFull.Trim() + " named " + AC.Text.Trim() + " to the Database?";
var popupResult = await AlertPopups.AskUser(CurrentViewController(), V, sTitle, sPPrompt, MessagePopupType.YesNo);
}
}
};
CurrentViewController is defined like this:
private UIViewController CurrentViewController()
{
var window = UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
return vc;
}
This does nothing. It hangs The user interface.
This should be built in, but it is only built in in Xamarin.Forms, which I do not want to use.
I have no problem in doing this stuff with an await, but this is simply not working. Can anyone help?
Thanks!
You can just use the ACR UserDialogs library:
https://github.com/aritchie/userdialogs
This is a solution I provided a few years ago, I think it is an ugly hack, compared to your elegant approach. You did not say what part does not work exactly, that might help spot the problem.
Here is my solution from a few years back:
iphone UIAlertView Modal

Commands not all working in J2ME app

Part of my j2me project involves use of the filesystem, so I'm starting with a simple file browser. The current issue I'm facing is the implementation of the command processor. To wit:
<CODE>
public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("Exit")) {
notifyDestroyed();
}
// File retrieval code (or call to it) to go here later.
if (c.getLabel().equals("Enter")) {
current_name = getRidOfTrailingSlash(current_name)
+ "/" + mainscreen.getString(mainscreen.getSelectedIndex());
// + "/" + "MemoryCard/";
System.exit(0);
if (! setName(current_name)) {
dirlist = rootlist;
current_name = "/";
}
showDirs();
}
if (c.getLabel().equals("Up Dir")) {
if (current_name.equals("/")) return;
current_name = getRidOfTrailingSlash(current_name);
if (setName(current_name)) current_name = current_fc.getPath();
else current_name = "/";
if (current_name.equals("/")) dirlist = rootlist;
else {
try {
dirlist = current_fc.list();
} catch (IOException ioe) {}
}
showDirs();
}
if (c.getLabel().equals("Create")) {
System.exit(1);
// Create code to be added later
}
if (c.getLabel().equals("Delete")) {
System.exit(2);
// Delete code to be added later
}
} // End of command actions
</CODE>
I am working on debugging the code for "Enter", and plan to write the code for "Create" and "Delete" at a later date. So for now I have them exit to an expected Security exception.
The code that sets up the commands is as follows:
<CODE>
public String getRidOfTrailingSlash(String s) {
if (s.equals("/")) return "";
int len = s.length();
String end = s.substring(len - 1);
if (end.equals("/")) s = s.substring(len - 2);
return s;
}
public List makeFileList(Enumeration names, String[] cmds) {
int i,s,cmdtype;
String c;
List l = new List(current_name, List.EXCLUSIVE);
while (names.hasMoreElements()) {
c = (String) names.nextElement();
l.append(c, null);
}
s = cmds.length;
for (i=0; i<s; i++) {
c = cmds[i];
if (c.equalsIgnoreCase("Exit")) {
cmdtype = Command.EXIT;
} else
if (c.equalsIgnoreCase("Up Dir")) {
cmdtype = Command.BACK;
} else {
cmdtype = Command.BACK;
}
// I've also tried using ITEM, OK, and one or two other
// command types for the default. Same effect.
l.addCommand(new Command(c, cmdtype, 1));
}
return l;
}
private void showDirs() {
String commands[] = { "Enter", "Up Dir", "Create", "Delete", "Exit" };
mainscreen = makeFileList(dirlist, commands);
mainscreen.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainscreen);
}
</CODE>
My current issue is that the commands are not all being executed. "Exit" and "Up Dir" are working properly. However, the other three commands don't function at all in the Emulator. They show up in the menus, but selecting them does absolutely nothing when they should be routing the program flow through the System.exit() calls. If I select any of these any number of times, "Up Dir" and "Exit" still work afterward. Furthermore, the select function in the file list doesn't do anything either.
On the phone that I'm writing this for (An LG Rumor Reflex), the select function (checking and unchecking items in the list) works, as do the commands "Create", "Delete", "Up Dir", and "Exit". However, the "Enter" command doesn't even appear in the menu!
What am I doing wrong, and why the different behavior on the phone versus the Emulator?

CTreeCtrl: How to clear the focus of selected item

Am new to MFC, I want to replicate the exact Ctrl+Page Down and Ctrl+Page Up behavior to regular Page Down/Page Up keys without any supporting keys (Ctrl/Shift). I have been trying to clear the focus of item which is getting selected automatically on striking the keys Page Up and Page Down.
I've tried with this code but its not working:
case VK_NEXT: // pagedown
case VK_PRIOR: // pageup
lhItem = GetFocusedItem();
if (IsSelected(lhItem))
{
CTreeCtrl::SetItemState(lhItem, 0, TVIS_SELECTED);
}
break;
Can anyone please help me in solving it
The Code need to written in OnSelChanging & OnSelChanged Event Handler functions
void CTreeCtrl::OnSelchanging(NMHDR *pNMHDR, LRESULT *pResult)
{
HTREEITEM hNew = pNMTreeView->itemNew.hItem;
HTREEITEM hOld = pNMTreeView->itemOld.hItem;
m_bOldItemSelected = hOld && (CTreeCtrl::GetItemState(hOld, UINT(TVIS_SELECTED)) & TVIS_SELECTED);
if (GetSelectedCount() > 1)
{
if (m_bPgUpState || m_bPgDownState)
{
//Check the state of New Item
if ((pNMTreeView->itemNew.state & TVIS_SELECTED))
{
// If the item is selected, so make sure OnSelchanged()
// will "select" it !
m_bNewItemSelected = TRUE;
}
else if (!(pNMTreeView->itemNew.state & TVIS_SELECTED))
{
// The New item is not selected, so make sure OnSelchanged()
// will not "re-select" it !
m_bNewItemSelected = FALSE;
CTreeCtrl::SetItemState(hNew, UINT(~TVIS_SELECTED), UINT(TVIS_SELECTED));
}
}
}
void TreeCtrl::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM itemNew = pNMTreeView->itemNew.hItem;
HTREEITEM itemOld = pNMTreeView->itemOld.hItem;
if ((m_bPgUpState || m_bPgDownState) && (GetSelectedCount() > 1)
&& (pNMTreeView->itemOld.hItem != NULL || pNMTreeView->itemNew.hItem != NULL))
{
// It had the focus so Keep selection at old item
if (itemOld && m_bOldItemSelected)
{
CTreeCtrl::SetItemState(itemOld, UINT(TVIS_SELECTED), UINT(TVIS_SELECTED));
m_bOldItemSelected = FALSE;
}
else
{
// Do-not select the item if it is not selected
CTreeCtrl::SetItemState(itemOld, UINT(~TVIS_SELECTED), UINT(TVIS_SELECTED));
}
}
In this article you'll find solution for every thing about CTreeCtrl
Full-Featured Tree Control

How to get the integer value in TextBox?

I have one Text Box in windows application. This Text Box only allowed the integer value not string. Can anybody have solution ?
Convert it.
public int GetIntValue(TextBox tb)
{
try
{
return Convert.toInt32(tb.Text);
}
catch (Exception ex)
{
//This is called if the converting failed for some reason
}
return 0; //This should only return 0 if the textbox does not contain a valid integer value
}
Use it like this:
int number = GetIntValue(textBox1);
Hope this helps!
Use this.
int value = Convert.ToInt32(textBox1.Text);
You use this code and get your integer value.Thanks
I found a solution from C# How do I make a textbox that only accepts numbers
Hope it will help you.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}

MonoTouch Dialog elements are not updating/repainting themselves

I have the following in a Section:
_favElement = new StyledStringElement (string.Empty);
_favElement.Alignment = UITextAlignment.Center;
if (_room.IsFavourite) {
_favElement.Image = UIImage.FromBundle ("Images/thumbs_up.png");
_favElement.Caption = "Unmark as Favourite";
} else {
_favElement.Image = null;
_favElement.Caption = "Mark as Favourite";
}
_favElement.Tapped += favElement_Tapped;
Then when I press the element I want the following to happen:
private void favElement_Tapped ()
{
if (_room.IsFavourite) {
_favElement.Image = null;
_favElement.Caption = "Mark as Favourite";
} else {
_favElement.Image = UIImage.FromBundle ("Images/thumbs_up.png");
_favElement.Caption = "Unmark as Favourite";
}
_room.IsFavourite = !_room.IsFavourite;
}
However the image and text does not change in the actual element when the element is tapped. Is there a refresh method or something that must be called? I've also tried changing the Accessory on Tapped as well and nothing changes. The properties behind do reflect the correct values though.
An alternative to reloading the UITableView is to reload the Element using code like this (copied from Touch.Unit):
if (GetContainerTableView () != null) {
var root = GetImmediateRootElement ();
root.Reload (this, UITableViewRowAnimation.Fade);
}
assuming that your code is in DialogViewController,add this
this.ReloadData();
but in your case I recommend you to use BooleanImageElement

Resources